474 Programming and Data Structures
Enter eight elements: 12 34 54 76 69 78 85 97
Enter an element: 3
The element 3 is not found in list
Explanation In the above program eight elements are entered. The user has to enter an element
whose predecessor and successor are to be identified. All the elements of the array are checked with
the entered number. When the match is found, the next element of the entered number is displayed
as successor and the previous element is displayed as predecessor. If the element entered is the first
element of the list then only successor is displayed. If the entered element is the last element of the
list then only predecessor is displayed. The above conditions are checked using conditional operator
(?:).
14.7 INSERTION
When a new element is added in the list it called append. However, when an element is added in
between two elements in the list the process is called as insertion. The insertion can be done at the
beginning, inside or anywhere in the list.
For successful insertion of an element, the array implementing the list should have at least one
empty location. If the array is full, insertion cannot be possible. The target location where element is
to be inserted is made empty by shifting elements downwards by one position and the newly Inserted
element is placed at that location. Consider the Fig. 14.5.
5 7
9
10 12
0
1 2 3 4 5 6
5 7
9
10
12
0
1 2 3 4
5 6
(a) (b)
5 7
3 9 10 12
0 1
2 3 4
5 6
(c)
Fig. 14.5 Insertion
As per Fig. 14.5 (a), two empty spaces are available. Suppose we want to insert 3 in between 7 and 9.
All the elements after 7 must be shifted towards end of the array The resulting array would be as
shown in Fig. 14.5 (b). The entered number 3 can be assigned to that memory location and the array
would look like Fig. 14.5 (c). Fig. 14.5 describes the insertion of an element.
Linear Data Structure 475
The following program illustrates the insertion operation.
14.4 Write a program to create a list. Insert some element at the specified location.
# include <stdio.h>
# include <conio.h>
# include <process.h>
main ()
{
int num[8]= {0 }, j,kaO,p,n;
c lr s c r();
printf ( ”\n Enter elements ( 0 to exit ) t );
for (j«0;j<8;j+ +)
{
scanf ("%d"#&num[j]);
i f ( num[j]*»0)
break;
}
i f (j<8)
{
printf ("\n Enter Position number and element : );
scanf ("%d %dn,&p,&n);
--p ;
}
else
while (num[k]1=0)
k++;
k-- ;
for (j =k;j > = p ;j- -)
num[j + 1]=num [j];
num [p] =n;
for (j=0 ;j <8;j+ + )
prin tf ( M %d , num [j]);
}
OUTPUT:
Enter elements ( 0 to exit): 1234560
Enter Position number and element: 5 10
1 2 3 4 10 5 6 0
Explanation In the above program an array num [8] is declared. The user has to enter elements
continuously. The user can enter zero to exit from the continuous input routine. If zero is entered, the
break statement takes control out of the for loop. Thus, the list contains few empty memory locations,
which are enough to carry insertion operation. When the user enters eight elements, the program

Get Programming and Data Structures now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.