-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinked_list.c
194 lines (146 loc) · 2.81 KB
/
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
* List - Set of items organised sequentially
* Linked list - A way to represent a list where by each item in the list
* part of a structure also contains a "link" to the structure
* containing the next item
*
* Basic operations on a linked list:
* 1.Insert
* 2.Traverse
* 3.Search
* 4.Display
* 5.Delete
*/
/* PROGRAM TO PERFORM OPERATIONS ON A LINKED LIST */
#include<stdio.h>
#include<stdlib.h>
// Node structure
struct node {
int d;
struct node *next;
};
// Alias the node structure
typedef struct node* NODE;
// Initialize head pointer of the linked list to NULL
NODE head = NULL;
main()
{
int ch;
void insert();
void display();
void search();
void delete();
while (1) {
printf("MENU\n1. Insert\n 2. Display\n 3. Search\n 4. Delete\n 5. Exit\nEnter choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
insert();
break;
case 2:
display();
break;
case 3:
search();
break;
case 4:
delete();
break;
case 5:
exit(0);
default:
printf("Invalid Option :-(\n");
break;
}
}
}
void insert()
{
int v;
NODE new, temp;
printf("Enter value: ");
scanf("%d", &v);
// Create new node
new = (NODE)malloc(sizeof(struct node));
// Store the value for the node
new->d = v;
new->next = NULL;
if (head == NULL) {
// List is empty
head = new;
} else {
// Otherwise
temp = head;
while (temp->next != NULL)
temp = temp->next;
temp->next = new;
}
}
void display()
{
NODE temp;
if (head == NULL) {
printf("List is empty\n");
return;
}
printf("Values in list: ");
temp = head;
// Iterate through the list and print the value at each node
while (temp != NULL) {
printf("%d ", temp->d);
temp = temp->next;
}
printf("\n");
}
void search()
{
int e;
NODE temp;
printf("Enter element: ");
scanf("%d", &e);
if (head == NULL) {
printf("List is empty\n");
return;
}
temp = head;
// Iterate through the list to search for the element
while (temp != NULL) {
if (temp->d == e) {
printf("Element found\n");
break;
}
temp = temp->next;
}
if (temp == NULL)
printf("Element not found\n");
}
void delete()
{
int e;
NODE temp, t;
printf("Enter element: ");
scanf("%d", &e);
temp = head;
// The item to be deleted is the first in the list
if (temp->d == e) {
head = temp->next;
free(temp);
printf("Deleted\n");
return;
}
while (temp != NULL) {
if (temp->d == e) {
break;
}
t = temp;
temp = temp->next;
}
// Item not present in the list
if (temp == NULL)
printf("Element not found\n");
else {
t->next = temp->next;
free(temp);
printf("Deleted\n");
}
}