-
Notifications
You must be signed in to change notification settings - Fork 32
/
searching.c
53 lines (44 loc) · 905 Bytes
/
searching.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
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *next;
} node;
node *head = NULL;
void insert(int data) {
node *new_node = malloc(sizeof(node));
new_node->data = data;
new_node->next = NULL;
if (head == NULL) {
head = new_node;
} else {
node *current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = new_node;
}
}
node* search(int data) {
node *current = head;
while (current != NULL) {
if (current->data == data) {
return current;
}
current = current->next;
}
return NULL;
}
int main() {
insert(10);
insert(20);
insert(30);
insert(50);
node *found_node = search(50);
if (found_node != NULL) {
printf("The node containing the data 50 was found.\n");
} else {
printf("The node containing the data 50 was not found.\n");
}
return 0;
}