-
Notifications
You must be signed in to change notification settings - Fork 14
/
searching in singly
63 lines (57 loc) · 1.26 KB
/
searching in singly
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <stdio.h>
#include <stdlib.h>
struct node
{
int num;
struct node *nextptr;
}
stnode, *ennode;
int FindElement(int);
void main()
{
int n,i,FindElem,FindPlc;
stnode.nextptr=NULL;
ennode=&stnode;
printf("\n\n Linked List : Search an element in a Singly Linked List :\n");
printf("---------------------------------------------------------------\n");
printf(" Input the number of nodes : ");
scanf("%d", &n);
printf("\n");
for(i=0;i< n;i++)
{
ennode->nextptr=(struct node *)malloc(sizeof(struct node));
printf(" Input data for node %d : ",i+1);
scanf("%d",&ennode->num);
ennode=ennode->nextptr;
}
ennode->nextptr=NULL;
printf("\n Data entered in the list are :\n");
ennode=&stnode;
while(ennode->nextptr!=NULL)
{
printf(" Data = %d\n",ennode->num);
ennode=ennode->nextptr;
}
printf("\n");
printf(" Input the element to be searched : ");
scanf("%d",&FindElem);
FindPlc=FindElement(FindElem);
if(FindPlc<=n)
printf(" Element found at node %d \n\n",FindPlc);
else
printf(" This element does not exists in linked list.\n\n");
}
int FindElement(int FindElem)
{
int ctr=1;
ennode=&stnode;
while(ennode->nextptr!=NULL)
{
if(ennode->num==FindElem)
break;
else
ctr++;
ennode=ennode->nextptr;
}
return ctr;
}