Sequential search is the most common search used on linked list structures.
Algorithm
1.
2.
3.
4.
Step-2: Compare the KEY value with the Current node value;
5.
6.
7.
8.
else go to step-3.
9.
10.
Step-3: Move the Current pointer to point to the next node in the
list and go to step-2, till the list is not over or else quit.
11.
Example
1.
2.
3.
4.
#include<stdio.h>
5.
6.
#include<conio.h>
7.
8.
#include<malloc.h>
9.
10.
struct node
11.
12.
13.
14.
int data;
15.
16.
17.
18.
19.
20.
first, *nw;
21.
22.
int search(int);
23.
24.
void main()
25.
26.
27.
28.
int no,i,item,pos;
29.
30.
clrscr();
31.
32.
first.next=NULL;
33.
34.
nw=&first;
35.
36.
37.
38.
scanf("%d",&no);
39.
40.
printf("\n");
41.
42.
for(i=0;i< no;i++)
43.
44.
45.
46.
47.
48.
49.
50.
51.
scanf("%d",&nw->data);
52.
nw=nw->next;
53.
54.
55.
56.
nw->next=NULL;
57.
58.
59.
60.
nw=&first;
61.
62.
while(nw->next!=NULL)
63.
64.
65.
66.
printf("%d\t",nw->data);
67.
68.
nw=nw->next;
69.
70.
71.
72.
printf("\n");
73.
74.
75.
76.
scanf("%d",&item);
77.
78.
pos=search(item);
79.
80.
if(pos<=no)
81.
82.
83.
84.
else
85.
86.
87.
88.
getch();
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
int count=1;
99.
100.
101.
nw=&first;
102.
while(nw->next!=NULL)
103.
104.
105.
106.
if(nw->data==item)
107.
108.
break;
109.
110.
else
111.
112.
count++;
113.
114.
nw=nw->next;
115.
116.
117.
118.
return count;
119.
120.
121.
Output
1.
2.
#include <stdio.h>
3.
#include <conio.h>
4.
#include <malloc.h>
5.
struct node
6.
7.
int data;
8.
9.
};
10.
11.
void main()
12.
13.
14.
int i;
15.
int num ;
16.
struct
17.
clrscr();
18.
19.
scanf("%d", &num );
20.
first->next = NULL;
21.
nw = first;
22.
23.
24.
25.
nw = nw->next;
26.
27.
scanf("%d", &nw->data);
28.
nw->next = NULL;
29.
30.
new1 = first;
31.
32.
33.
34.
35.
36.
37.
38.
new1->data = count->data;
39.
count->data = temp;
40.
41.
42.
43.
nw = first->next;
44.
45.
while (nw)
46.
47.
printf("%d\t", nw->data);
48.
nw = nw->next;
49.
50.
getch();
51.
52.
Output
/* pull off the front node of the source and put it in dest */
void MoveNode(struct node** destRef, struct node** sourceRef);
{
/* a dummy first node to hang the result on */
struct node dummy;
tail = tail->next;
}
return(dummy.next);
}
/* UTILITY FUNCTIONS */
= new_node;
push(&b, 20);
push(&b, 3);
push(&b, 2);
return 0;
}
Output :
Merged Linked List is:
2 3 5 10 15 20