DS Insertion Operation

Insertion Operation

Insert operation is to insert one or more data elements into an array. Based on the requirement, new element can be added at the beginning, end or any given index of array.

Here, we see a practical implementation of insertion operation, where we add data at the end of the array.

Example

Let LA is a Linear Array (unordered) with N elements and K is a positive integer such that K<=N.

//Write a program to perform Insertion operation.
#include <stdio.h>
void main()
{
   int LA[] = {2,4,6,8,9};
   int item = 11, k = 3, n = 5;
   int i = 0, j = n;

   printf("The original array elements are:\n");

   for(i = 0; i<n; i++)
   {
      printf("LA[%d] = %d \n", i, LA[i]);
   }

   n = n + 1;

   while( j >= k)
   {
      LA[j+1] = LA[j];
      j = j - 1;
   }

   LA[k] = item;

   printf("The array elements after insertion:\n");

   for(i = 0; i<n; i++)
   {
      printf("LA[%d] = %d \n", i, LA[i]);
   }
}

Output:

Command Prompt
The original array elements are:
LA[0]=2
LA[1]=4
LA[2]=6
LA[3]=8
LA[4]=9
The array elements after insertion:
LA[0]=2
LA[1]=4
LA[2]=6
LA[3]=11
LA[4]=8
LA[5]=9

Share This Page to Download:

Share to Download

Share This Page on:


Ask Question