DS

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



Subscribe us on Youtube

Share This Page on

Questions


SHANI KUMAR SAROJ | 31-Aug-2017 04:58:41 pm

sir data structure ki book pdf me download kyo nahi ho rahi hai please send book my email sksaroj464@gmail.com


Sagar Dhampalwar | 06-Oct-2017 10:44:51 pm

I want to delete an element from beginning and at end of array Plz post the program for that


Josna | 22-Jan-2018 06:29:20 pm

Is this C or C++ ?


Piyush Varma | 01-Feb-2018 12:19:58 am

Can you explain the operations on linked lists?


Shaguj | 24-Oct-2018 11:51:00 am

What is the benefit of while loop?


Ask Question