-
Notifications
You must be signed in to change notification settings - Fork 0
/
singly_linked_list.c
271 lines (245 loc) · 7.04 KB
/
singly_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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
//singly linked lists all functions
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int val;
struct node *next;
}node;
typedef struct list {
node *head;
int num_elements;
} list;
list* create_list() {
list *lst = (list*)malloc(sizeof(list));
lst->head = NULL;
lst->num_elements = 0;
return lst;
}
void add_front(list *lst, int ele) {
node *new_node = (node*)malloc(sizeof(node));
new_node->val = ele;
new_node->next = lst->head;
lst->head = new_node;
lst->num_elements++;
}
void add_back(list *lst, int ele) {
node *new_node = (node*)malloc(sizeof(node));
new_node->val = ele;
new_node->next = NULL;
node *current = lst->head;
if (lst->head == NULL) {
lst->head = new_node;
}
else {
while (current->next != NULL) {
current = current->next;
}
current->next = new_node;
}
lst->num_elements++;
}
void insert_after(list *lst, int ele, int key) {
node *prev = lst->head;
if(prev==NULL) {
printf("List is empty.");
return;
}
while(prev!=NULL) {
if(prev->val == key) {
node *new_node = (node*)malloc(sizeof(node));
new_node->val = ele;
new_node->next = prev->next;
prev->next = new_node;
lst->num_elements++;
return;
}
prev = prev->next;
}
printf("%d not in the list\n", key);
}
void insert_before(list *lst, int ele, int key) {
node *prev = lst->head;
if(prev==NULL) {
printf("List is empty.");
return;
}
if(key==lst->head->val) { //same as prev->val. checking if the key is first node
node *new_node = (node*)malloc(sizeof(node));
new_node->val = ele;
new_node->next = lst->head;
lst->head = new_node;
lst->num_elements++;
return;
}
while(prev->next != NULL) {
if(prev->next->val == key) {
node *new_node = (node*)malloc(sizeof(node));
new_node->val = ele;
new_node->next = prev->next;
prev->next = new_node;
lst->num_elements++;
return;
}
prev = prev->next;
}
printf("%d not in the list\n", key);
}
void insert_at(list *lst, int ele, int pos) {
node *p = lst->head;
if(p==NULL) {
printf("List is empty.");
return;
}
if(pos==1) {
node *new_node = (node*)malloc(sizeof(node));
new_node->val = ele;
new_node->next = lst->head;
lst->head = new_node;
lst->num_elements++;
return;
}
for(int i=1; i<pos-1 && p!=NULL; i++) {
p = p->next;
}
if (p == NULL) {
printf("Less than %d elements present in the list\n", pos);
return;
}
else {
node *new_node = (node*)malloc(sizeof(node));
new_node->val = ele;
new_node->next = p->next;
p->next = new_node;
return;
}
}
int delete_front(list *lst) {
node *del = lst->head;
if (lst->head == NULL) {
printf("There are no elements to delete.");
return -1;
}
int ele = del->val;
lst->head = del->next;
free(del);
lst->num_elements--;
return ele;
}
int delete_back(list *lst) {
node *current = lst->head;
if (lst->head == NULL) {
printf("There are no elements to delete.");
return -1;
}
while(current->next->next != NULL) {
current = current->next;
}
node *del = current->next;
int ele = del->val;
free(del);
current->next = NULL;
lst->num_elements--;
return ele;
}
void delete_between(list *lst, int key) {
node *current = lst->head;
if (lst->head == NULL) {
printf("There are no elements to delete.");
return;
}
while(current->next != NULL) {
if(current->next->val == key) {
node *del = current->next;
current->next = del->next; //or current->next->next
free(del);
lst->num_elements--;
return;
}
current = current->next;
}
printf("%d not in the list\n", key);
}
void traversing(list *lst) {
node *curr = lst->head;
if(lst->head == NULL) {
printf("No items in list to display.");
}
while(curr != NULL){
printf("%d ",curr->val);
curr = curr->next;
}
}
int main() {
list *lst = create_list();
int ch, ele, del, key;
for(;;) {
printf("\n\nMENU\n");
printf("1. Add to the front of the list\n");
printf("2. Add to the back of the list\n");
printf("3. Insert after a given element\n");
printf("4. Insert before a given element\n");
printf("5. Insert at a given point\n");
printf("6. Delete at the front of the list\n");
printf("7. Delete at the back of the list\n");
printf("8. Delete a given element\n");
printf("9. Display the list\n");
printf("10. View the no.of elements in list\n");
printf("11. Exit\n");
printf("Enter your choice : ");
scanf("%d",&ch);
switch(ch) {
case 1:
printf("enter the element you want to add to the list : ");
scanf("%d",&ele);
add_front(lst, ele);
break;
case 2:
printf("enter the element you want to add to the list : ");
scanf("%d",&ele);
add_back(lst, ele);
break;
case 3:
printf("enter the element you want to insert to the list : ");
scanf("%d",&ele);
printf("enter the key after which you want to insert element : ");
scanf("%d",&key);
insert_after(lst, ele, key);
break;
case 4:
printf("enter the element you want to insert to the list : ");
scanf("%d",&ele);
printf("enter the key before which you want to insert element : ");
scanf("%d",&key);
insert_before(lst, ele, key);
break;
case 5:
printf("enter the element you want to insert to the list : ");
scanf("%d",&ele);
printf("enter the position at which you want to insert element : ");
scanf("%d",&key);
insert_at(lst, ele, key);
break;
case 6:
del = delete_front(lst);
break;
case 7:
del = delete_back(lst);
break;
case 8:
printf("enter the element you want to delete : ");
scanf("%d",&key);
delete_between(lst, key);
break;
case 9:
traversing(lst);
break;
case 10:
printf("The no.of elements is : %d",lst->num_elements);
break;
case 11:
exit(0);
default:
printf("Invalid choice.");
}
}
}