-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch in LL.cpp
87 lines (82 loc) · 1.51 KB
/
search in LL.cpp
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node {
int data;
struct node * link;
};
void append ( struct node **, int ) ;
void display ( struct node * ) ;
int search (struct node *, int);
int main() {
struct node *p ;
p=NULL;
int n,ele;
char ch[10];
do {
printf("Enter the value\n");
scanf("%d",&n);
append(&p,n);
printf("Do you want to add another node? Type Yes/No\n");
scanf("%s",ch);
}while(!strcmp(ch,"Yes"));
printf("The elements in the linked list are");
display(p);
printf("\n");
printf("Enter the element to be searched\n");
scanf("%d",&ele);
if(search(p,ele))
printf("%d is present in the linked list\n",ele);
else
printf("%d is not present in the linked list\n",ele);
return 0;
}
void append ( struct node **q, int num ) {
struct node *temp;
struct node *nn=(struct node *)malloc(sizeof(struct node));
temp=*q;
nn->data=num;
nn->link=NULL;
if(*q==NULL)
{
*q=nn;
}
else
{
while(temp->link!=NULL)
{
temp=temp->link;
}
temp->link=nn;
}
}
void display ( struct node *q ) {
struct node *temp=q;
while(temp!=NULL)
{
printf(" %d",temp->data);
temp=temp->link;
}
}
int search ( struct node *q, int e ) {
struct node *temp;
temp=q;
int f=0;
while(temp!=NULL)
{
if(temp->data==e)
{
f++;
break;
}
temp=temp->link;
}
if(f==0)
{
return 0;
}
else
{
return 1;
}
}