-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsearch in simple linklist.cpp
69 lines (69 loc) · 1.4 KB
/
search in simple linklist.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
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<stdbool.h>
struct node
{
int info;
struct node *link;
};
struct node *start,*last,*temp,*new_node;
void create()
{
int value,choice;
do
{
struct node *new_node=(struct node*)malloc(sizeof(struct node));
printf("enter value : ");
scanf("%d",&value);
if(start==NULL)
{
new_node->info=value;
start=new_node;
last=new_node;
}
else
{
new_node->info=value;
last->link=new_node;
last=new_node;
last->link=NULL;
}
printf("want to add more 1/0");
scanf("%d",&choice);
}
while(choice==1);
}
void display()
{
printf("your linklist : ");
temp=start;
while(temp->link!=NULL)
{
printf("%d->",temp->info);
temp=temp->link;
}
printf("%d->",temp->info);
printf("NULL");
}
bool search(struct node* start, int x)
{
struct node* current = start; // Initialize current
while (current != NULL)
{
if (current->info == x)
return true;
current = current->link;
}
return false;
}
int main()
{
int item;
create();
display();
printf("\nEnter item for search\n");
scanf("%d",&item);
search(start, item)? printf("Yes there is element") : printf("No element found");
return 0;
}