Insertion And Deletion In Array.

 

Today I am going to discuss with you how to insert and delete a new element in an array in C. In my last post, I discussed array in c in detail.

If you don’t know about array data structure you can check this post. However, insertion in an array is sometimes easy and sometimes difficult compared to other data structures in C. Deletion in an array instead of the last position is really difficult and time-consuming in an array. We are going to learn every insertion and deletion process in an array throughout this post. Insertion and deletion from the last position of an array are easy but insertion and deletion from any position are difficult.

 

 

Let’s take an integer array as an example and give the name of the array antor and give the size of the array 100. So, declaration for that array will be: int antor[100];

We will use this array throughout the whole post for insertion in the array as well as for deletion in an array.

Insertion in an Array

 

 

 

 

 

Insertion In Array:

 

As we haven’t taken any element in our array yet so the array is currently empty. We have allocated 100 positions for this array. So, we can take up to 100 elements into this array. To take an element in an empty array we have to insert the element at the 0-th index of the array. So, let’s take 15 in the 1st position which is at the 0-th index of the array antor.

 

antor[0]=15;

 

“=” is called the assignment operator in c which is used to assign something to a variable. If you don’t know the data types and operators in c you can check this post.

 

However, now our array contains one element that is 15 at the 0-th index. Let’s take 6 random element 10,20,30,40,50,60. We can use a for loop to take these elements. So, our code will be:

#include<stdio.h>

int main()

{

    int i;

    int antor[100];

    antor[0]=15;

    for(i=1;i<=6;i++)

    {

        antor[i]=i*10;

    }

    for(i=0;i<=6;i++)

    {

        printf(“%d “,antor[i]);

    }

    return 0;

}

 

So, we have created an array having 7 elements in the array. Now if you need to insert an element at the end of the array just you can write antor[7]=your_element. So, if you already have n number of elements in an array and you want to put a new element in the array just write:

array_name[n]=value;

Complexity: O(1)

Let’s insert a new element 80 at the last position in our array. So, antor[7]=80.

 

 

 

 

 

 

 

 

Insertion Of New Element In Any Position In An Array:  

To insert an element at the k-th position of the array where k is any position instead of the last position of the array you have to shift all the elements from the k-th position to the last position in 1 step right. means the k-th current element should be moved at the k+1 position and the k+1-th element at the k+2 position and so on till the n-th element at the n+1-th position. Then the k-th position will be vacant and you can easily insert the new element at the k-th position by writing antor[k]=value.

Algorithm:

Step-1: Set i=n-1 to k. [last position to k-th position]

Step-2: place antor[i+1]=antor[i].

Step-3: decrease the index by 1 i.e. i–;

Step-4: Put antor[k]=value and increase number of elements by 1 i.e. n++;

 

Program for insertion an element at the k-th position in the array antor:

#include<stdio.h>

int main()

{

    int i,n=0;

    int antor[100];

    antor[0]=15;

    n++;

    for(i=1; i<=6; i++)

    {

        antor[i]=i*10;

        n++;

    }

    printf(“Enter the Position where to insertn”);

    int k;

    scanf(“%d”,&k);

    printf(“Enter the value to insertn”);

    int val;

    scanf(“%d”,&val);

    for(i=n-1; i>=k; i–)

    {

        antor[i+1]=antor[i];

    }

    antor[k]=val;

    n++;

    for(i=0; i<n; i++)

    {

        printf(“%d “,antor[i]);

    }

    printf(“n”);

    return 0;

}

 

 

Here, we have used variable n to indicate the number of elements currently present in the array. When a new element is inserted into the array we just increase the value of n by 1. I hope you understand the code.

So, if you want to insert any element at the k-th position of an array having already n number of elements you need to do a total (n-k+2) number of moves. n-k+1 number of moves to shift all the elements from k-th position to n-th position by 1 position right. 1 move to put the new value at the k-th position.

 

So, the complexity for insertion in an array at any position is O(n-k+2) which is O(n).

 

 

 

 

 

 

Deletion In Array:

If you want to delete an element from the last position you just have to reduce the size of the array by 1 i.e. decrease the number of elements present in the array by 1. So, we just have to print the elements from 0 to n-2 in our array. In our array let’s consider having 7 elements again as before the insertion of new elements. Now, we want to delete the last element from the array. Just we have to print the first 6 elements and we will reduce the value of n by 1.

 

Our code to delete the last element from an array will be:

#include<stdio.h>

int main()

{

    int i,n=0;

    int antor[100];

    antor[0]=15;

    n++;

    for(i=1; i<=6; i++)

    {

        antor[i]=i*10;

        n++;

    }

    printf(“Elements of the array before deletionn”);

    for(i=0; i<n; i++)

    {

        printf(“%d “,antor[i]);

    }

    printf(“n”);

    –n;

    printf(“Elements of the array after deletion at the last positionn”);

    for(i=0; i<n; i++)

    {

        printf(“%d “,antor[i]);

    }

    printf(“n”);

    return 0;

}

 

 

The output of this code is:

Elements of the array before deletion

15 10 20 30 40 50 60

Elements of the array after deletion at the last position

15 10 20 30 40 50

 

 

 

 

 

 

 

Deletion Of Array Element At Any Position:

Now, if you want to delete an element from an array at any position other than the last element you have to do just 1 thing. Just left shift all the elements by 1 position from the last position to the k-th position [Consider the k-th element of the array is to be deleted].

 

Algorithm:

 

Step 1: Set i=k to n-2. [k-th position to the (last position-1)]

Step 2: Insert antor[i]=antor[i+1]

Step-3: Increase i by 1 i.e. i++.

Step-4: Reduce the length of the array i.e. the number of elements in the array by 1 i.e. n–.

 

C program for deletion of array element at any position:

#include<stdio.h>

int main()

{

    int i,n=0;

    int antor[100];

    antor[0]=15;

    n++;

    for(i=1; i<=6; i++)

    {

        antor[i]=i*10;

        n++;

    }

    printf(“Elements of the array before deletion:n”);

    for(i=0; i<n; i++)

    {

        printf(“%d “,antor[i]);

    }

    printf(“n”);

    printf(“Which indexed element should be deletedn”);

    int k;

    scanf(“%d”,&k);

    for(i=k; i<n-1; i++)

    {

        antor[i]=antor[i+1];

    }

    –n;

    printf(“Elements of the array after deletion at the position %d:n”,k);

    for(i=0; i<n; i++)

    {

        printf(“%d “,antor[i]);

    }

    printf(“n”);

    return 0;

}

 

 

Input:

4

Output:

Elements of the array before deletion:

15 10 20 30 40 50 60

Elements of the array after deletion at the position 4:

15 10 20 30 50 60

 

In this way, you can perform the operation deletion in an array at any position. In this operation, we have to do a total n-k move to left-shift the elements by 1 from k+1 to the n-1 position.

So, the Complexity for the operation of deletion at any position of an array is O(n-k) you can say O(n).

I hope you understood all the codes shown above. So, hopefully, you can easily perform insertion in an array as well as the deletion in an array from today.

 

Now, I am giving you 1 task that is to write a single program that will do the following things:

 

1) Ask the user the length of the array n where n<100. Then take the n number of elements as input from the user and put them in the array at the 0 to the n-1 position.

2) Ask the user which operation he wants to do and tell him to press 1 for insertion in the array, press 2 for deletion in the array, and press 0 for termination of the program and take this input in a variable p.

3)If he selects 1 ask him the position where to insert the element and take this input in a pos variable also ask him the value to be inserted and take this value in the k variable and perform the insertion operation.

4) If he selects 2 ask him the position which element should be deleted and take his input in the pos variable and perform the deletion operation.

5)After each operation print the resultant array.

6) You have to keep the operation going on until the user selects the value of p=0. When he selects p=0 terminate the program.

For simplification of the program, the array won’t be empty and n won’t be greater than 100 also 0<=p<=2.

Write the code on your own and comment below whatever you have written by yourself.

 

Remember don’t watch my code before trying your best by yourself. Comment the whatever you did by yourself after trying a few hours then watch my code.

 

My code for this problem is below:

 

#include<stdio.h>

int main()

{

    int i,n,pos,k,p;

    int ar[105];

    printf(“Enter the number of elements in the arrayn”);

    scanf(“%d”,&n);

    printf(“Enter the elements of the arrayn”);

    for(i=0; i<n; i++)

    {

        scanf(“%d”,&ar[i]);

    }

    printf(“Elements of the array at the beginning n”);

    for(i=0; i<n; i++)

    {

        printf(“%d “,ar[i]);

    }

    printf(“n”);

    while(1)

    {

        printf(“Press 1 for insertion, 2 for deletion and 0 for terminationn”);

        scanf(“%d”,&p);

        if(p==1)

        {

            printf(“The position where we have to insert the elementn”);

            scanf(“%d”,&pos);

            for(i=n-1; i>=pos; i–)

            {

                ar[i+1]=ar[i];

            }

            printf(“Enter the element wants to insert at %d positionn”,pos);

            scanf(“%d”,&k);

            ar[pos]=k;

            n++;

            printf(“Elements of the array after insertion %d at the position %d:n”,k,pos);

            for(i=0; i<n; i++)

            {

                printf(“%d “,ar[i]);

            }

            printf(“n”);

        }

        else if(p==2)

        {

            printf(“The position which element should be deletedn”);

            scanf(“%d”,&pos);

            for(i=pos; i<n-1; i++)

            {

                ar[i]=ar[i+1];

            }

            n–;

            printf(“Elements of the array after deletion at the position %d:n”,pos);

            for(i=0; i<n; i++)

            {

                printf(“%d “,ar[i]);

            }

            printf(“n”);

        }

        else

        {

            break;

        }

    }

    return 0;

}

I hope you understood the code. So, insertion in array and deletion in array at any position has a complexity of O(n). So, we can’t directly put or delete an element in any position of the array. At first, we have to shift the elements from the required position to the end position then we have to insert or delete the element. I hope now your concept about insertion and deletion in an array is completely clear.

 

 

 

 

 

Solve this problem about operations in an array.

In the next tutorial about C programming, I will introduce you to multidimensional arrays, especially 2d arrays.

To get my all posts about C programming click here.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *