Self-created C language function to insert array

Guiding idea: simulate the insert operation of list in python, and quickly insert new elements into the C program array (one-dimensional)

Actual parameters:
①The address of the first element of the array (pointer variable): To operate on an array, you need to find the address of the array in the memory.
②The position to be inserted (integer variable): Use the subscript method to record, that is, from Start counting at 0
③ The inserted value (variable with the same type as the inserted array), temporarily restrict the inserted array to an integer array

Drafting function: insert_arr(int * pArr, int loca, int val);

Implementation method: starting from the subscript of the inserted element, move one bit backward in sequence
Do pArr[n] = pArr[n-1];
While n-1> loca;
[Robustness problem: It should be judged whether loca is negative or exceeds
Number of array elements] If loca <0 or loca> ​​n;
Return false;
End;
a new question arises: how does insert_arr judge whether loca is out of bounds, that is, where does the value of n come from?
One more parameter should be passed to the insert_arr function: len (the length of the array)
since then, the function is updated to

insert_arr(int * pArr, int len, int loca, int val);

Try to write the function:
int insert_arr(int * pArr, int len, int loca, int val)
{ If(loca> ​​0 && loca <= len-1)//array subscript maximum value = array length -1 { Int count ; For(count = len-1; count >= loca; count–) { *pArr[count] = *pArr[count-1]; / A new problem arises: this operation is and only if the last element of the array is stored When it is a garbage number, it will not affect the storage of the required array. But when the last element of the array is also the element we need, this will delete it (to be precise, it will be covered by the penultimate element), leave it alone for the time being, and implement the main code first / } *pArr[loca] = val;//Overwrite the original value of val, that is, insert } Else { Return -1;//This value is used to mark insertion failure } }














The actual code is written as follows:

# include <stdio.h>
# include <stdlib.h>
int insert_arr(int * pArr,int len, int loca, int val);
void prt_arr(int * pArr, int len);
int main(void)
{
    
    
    int fake_length, location, value;
    printf("Please input the length of the array u wanna have:");
    scanf("%d",&fake_length);
    int *Arr = (int *)malloc(sizeof(int) * fake_length);
    for(int count=0; count<fake_length; count++)
    {
    
    
        Arr[count] = count;
    }
    //initialize the array
    
    if(Arr != NULL)
    {
    
    
        printf("Please input the value u wanna insert:");
        scanf("%d",&value);
        printf("Please input the location u wanna insert:");
        scanf("%d",&location);
        int status = insert_arr(Arr, fake_length, location, value);
        if(status == -1)
        {
    
    
            printf("Function ran failed!\nPlease check the data u input!\n");
        }
        else if(status == 1)
        {
    
    
            printf("Function ran sucessfully!\nCheers!!!\n");
            prt_arr(Arr, fake_length);
        }
        else
        {
    
    
            printf("There is an unknow problem...\n");
        }
    }
    else
    {
    
    
        printf("Sooooorry~System denied ur application of memory!\n");
    }
}
int insert_arr(int *pArr, int len, int loca, int val)
{
    
    
    if(loca >= 0 && loca <= len - 1)
    {
    
    
        int count;
        for(count = len - 1; count > loca; count--)
        {
    
    
            pArr[count] = pArr[count-1];
            //review the point used as array without of '*'
        }
        pArr[loca] = val;
        return 1;
    }
    else
    {
    
    
        return -1;
    }
}
void prt_arr(int * pArr, int len)
{
    
    
    for(int count = 0; count<len; count++)
    {
    
    
        printf("%d\t",pArr[count]);
    }
    printf("\n");
    printf("Thanks for u used\n");
    free(pArr);
}

A new problem arises: the last element of the array is erased [to be resolved]

Guess you like

Origin blog.csdn.net/weixin_43888800/article/details/113116710