Computer Science: Array Computer Solutions: Insertion and Deletion

Glide to success with Doorsteptutor material for CBSE : fully solved questions with step-by-step explanation- practice your way to success.

Download PDF of This Page (Size: 137K)

Insertion

It means addition of a data item in the middle or at the end of the array.

If data is to be added after a given data item, then the location of the data item is first determined by applying search procedure and then the insertion procedure is implemented.

# include < iostream.h >

void main ()

{

int x [20].

int l, loc, n, data.

cout << “Enter the no. of elements”.

cin >> n.

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

{

cout << “Enter the array element”.

cin >> x [l].

}

cout << “Enter the location after which data is to be inserted”.

cin >> loc.

for (l = n - 1; l > = loc; l - -)

x [l + 1] = x [1].

cout << “enter the new data to be added”.

cin >> x [loc].

n ++.

cout << “Array elements after insertion”.

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

cout << x [l].

}

Let data 12 to be inserted at location 2

Image of Insertion

Image of Insertion

Deletion

It means removal of a data. First the location of the item to be deleted is determined by applying an appropriate search procedure and then the value present at particular location is deleted.

# include < iostream.h >

void main ()

{

int x [20].

int l, j, n, loc, data.

cout << “Enter the no. of elements”.

cin >> n.

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

{

cout << “Enter value”.

cin >> x [l].

}

cout << “Enter the location to be deleted”.

cin >> loc.

if (loc! = 0)

{

data = x [loc].

for {j = loc; j < n - 1; j ++)

x [j] = x [ j + 1].

}

n = n - 1.

cout << “Elements after deletion”.

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

cout << x [l].

}

Image of Deletion

Image of Deletion

Developed by: