472 Programming and Data Structures
{
i f (sim [j] =»o)
{
f *1 ;
p rin tf ( an Found ! Po sitio n o f in teger %d i i %d " , n ,j+ l ) ;
jtr^ajci
}
i f a-lO)
p rin tf ( an Il«o a n t not found !•);
Q1JTEIHQ
Enter 9even Integers: 2 4 23 45 67 56 89
Enter Integer to Search: 67
Found ! Position of integer £7 is 5
Explanation In the above program an array sim [7] is declaed and it stores seven integers, which
are entered by the programmer. Followed by this, an element is entered to search in the list. This is
done by the for loop and the embedded if statement. The i f () statement checks the entered element
with the list elements. If the match is found the flag sets to T , else it remains '0.' When match is
found, the number's position is displayed.
14.6 PREDECESSOR AND SUCCESSOR
The elements of a list are located with their positions. For example, we can place the elements at
different locations and call them element n, (n -1) predecessor and (rc+1) successor. Once the position
of an element is fixed, we can easily find its predecessor and successor. In other words, in the list, the
elements have relative positions. The left element is the predecessor and the right element is the
successor.
The first element of a list does not have a predecessor and the last one does not have a successor
Predecessor
Successor
5
8
_
I
10
15 18 20
Fig. 14.4 Predecessor and successor
Fig. 14.4 shows the predecessor and successor elements of number 10.
The following program displays the predecessor and successor elements of the entered element
from the list.
Linear Data Structure 473
14-3 Write a program to find the predecessor and successor o i the entered number in a list
f include <stdio.h >
# include <conlo.h>
main ()
{
in t n u n [8 ], j,n ,k »0 ,fa O ;
c l r s c r ( ) ;
p rin tf ("n Enter eight elements : ) ;
fo r ( j« 0 ;j< 8 jj+ + )
scanf ( ”%da,& num [j]) ;
p rin t f (*n Enter an element : );
scanf ( "%d", &n);
fo r ( j« 0 ; j< 8 ; j+ + )
{
i f (n««nu m [j] )
{
f-1 ;
(j >0> ? p r in tf ("n The Predecessor of %d is %d ,num[ j ] , num[j- 1 ] ) :
p rin t f (■ No P redecessor");
(j » * 7 ) ? p rin tf ("n No Successor") :
p rin t f ("n The Successor o f %d is %d"/ num[j] , num[ j + 1 ]);
break;
}
k++;
}
if (f= = 0 )
p rin tf (" The element %d is not found in l i e t ,3,n );
>
OUTPUT:
Enter eight elements: 12587446
Enter an element: 5
The Predecessor of 5 is 2
The Successor of 5 is 8
Enter eight elements: 12345678
Enter an element: 1
No Predecessor
The Successor of 1 is 2
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.199.184