-
Notifications
You must be signed in to change notification settings - Fork 34
/
search_linked_list.c
73 lines (65 loc) · 1.52 KB
/
search_linked_list.c
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
// below is the C program to search an element in a linked list
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
// Link list node
struct Node
{
int data;
struct Node* next;
};
/* Given a reference (pointer to pointer) to the head
of a list and an int, push a new node on the front
of the list. */
void push(struct Node** head, int new_data)
{
/* allocate node */
struct Node* new_node =
(struct Node*) malloc(sizeof(struct Node));
/* put in the data */
new_node->data = new_data;
/* link the old list off the new node */
new_node->next = (*head);
/* move the head to point to the new node */
(*head) = new_node;
}
/* Checks whether the value x is present in linked list */
bool search(struct Node* head, int x)
{
struct Node* current = head; // Initialize current
while (current != NULL)
{
if (current->data == x)
return true;
current = current->next;
}
return false;
}
/* Driver program to test count function*/
int main()
{
/* Start with the empty list */
struct Node* head = NULL;
int x ;
int d,i;
do
{
printf("\nEnter your option\n1. insert\n2. exit\n");
scanf("%d", &d);
switch (d)
{
case 1:
printf("enter data");
scanf("%d",&i);
push(&head,i);
break;
default:
break;
}
} while (d != 2);
/* Use push() to construct below list */
printf("enter data to search\n");
scanf("%d",&x);
search(head, x)? printf("Yes") : printf("No");
return 0;
}