-
Notifications
You must be signed in to change notification settings - Fork 369
/
Copy pathSinglyLinkedList.c
337 lines (319 loc) · 6.97 KB
/
SinglyLinkedList.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void insertAtEnd(int);
struct node
{
int data;
struct node *next;
} *start = NULL;
void createList(int n)
{
for (int i = 0; i < n; i++)
{
printf("Enter data of node %d:- ", i + 1);
insertAtEnd(1);
}
printf("\nSUCCESSFULLY CREATED\n");
};
void traverse()
{
struct node *ptr;
if (start == NULL)
{
printf("\nEmpty Linked List");
return;
}
ptr = start;
while (ptr != NULL)
{
printf("%d ", ptr->data);
ptr = ptr->next;
}
};
struct node *elementSearch(int s, int skey)
{
int key = 0;
int index = 0;
struct node *ptr, *preptr; // preptr for before and after insert;
if (start == NULL)
{
printf("\nEmpty Linked List");
return 0;
}
ptr = start;
preptr = start;
while (ptr != NULL)
{
if (ptr->data == s)
{
key = 1;
break;
}
index++;
preptr = ptr;
ptr = ptr->next;
}
if (key == 1)
{
printf("\nElement found! at index %d\n", index);
}
else
{
printf("\nElement not found!\n");
return 0;
}
if (skey == 1)
{
return preptr;
}
else
return ptr;
}
void insertAtBeginning()
{
struct node *nptr;
int data;
nptr = (struct node *)malloc(sizeof(struct node));
printf("\nEnter element to assign at beginning:- ");
scanf("%d", &nptr->data);
if (start == NULL)
{
nptr->next = NULL;
start = nptr;
return;
}
nptr->next = start;
start = nptr;
printf("\nSUCCESSFULLY ADDED \n");
};
void insertAtEnd(int key)
{
struct node *nptr, *ptr;
nptr = (struct node *)malloc(sizeof(struct node));
nptr->next = NULL;
if (key == 0)
{
printf("\nEnter element to assign at the end:- ");
}
scanf("%d", &nptr->data);
if (start == NULL)
{
if (key == 0)
{
printf("\nLinked list is empty so inserted at first\n");
}
start = nptr;
return;
}
ptr = start;
while (ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = nptr;
if (key == 0)
printf("\nSUCCESSFULLY ADDED \n");
};
void insertBefGivNode()
{
int el;
struct node *preptr, *nptr;
nptr = (struct node *)malloc(sizeof(struct node));
printf("\nEnter element before which you want to insert a node:- ");
scanf("%d", &el);
preptr = elementSearch(el, 1);
if (preptr == 0)
return;
printf("\nEnter element to enter:- ");
scanf("%d", &nptr->data);
if (preptr == start)
{
nptr->next = start;
start = nptr; // or start=nptr
}
else
{
nptr->next = preptr->next;
preptr->next = nptr;
}
printf("\nSUCCESSFULLY ADDED \n");
};
void insertAftGivNode()
{
int el;
struct node *aftptr, *nptr;
nptr = (struct node *)malloc(sizeof(struct node));
printf("\nEnter element after which you want to insert a node:- ");
scanf("%d", &el);
aftptr = elementSearch(el, 0);
if (aftptr == 0)
return;
printf("\nEnter element to enter:- ");
scanf("%d", &nptr->data);
if (aftptr == start)
{
start->next = nptr;
nptr->next = NULL;
}
else
{
nptr->next = aftptr->next;
aftptr->next = nptr;
}
printf("\nSUCCESSFULLY ADDED \n");
};
void delAtStart()
{
struct node *ptr;
if (start == NULL)
{
printf("\nEmpty linked list(underflow)");
return;
}
ptr = start;
start = start->next;
printf("\nData deleted = %d\n", ptr->data);
/* Clears the memory occupied by first node*/
free(ptr);
printf("SUCCESSFULLY DELETED FIRST NODE FROM LIST\n");
};
void delAtEnd()
{
struct node *ptr, *preptr;
if (start == NULL)
{
printf("\nEmpty linked list(underflow)");
return;
}
ptr = start;
while (ptr->next != NULL)
{
preptr = ptr;
ptr = ptr->next;
}
preptr->next = NULL;
printf("\nData deleted = %d\n", ptr->data);
/* Clears the memory occupied by first node*/
free(ptr);
printf("SUCCESSFULLY DELETED LAST NODE FROM LIST\n");
}
void delGivNode()
{
struct node *ptr, *preptr;
int toDelete, key = 0;
if (start == NULL)
{
printf("\nEmpty linked list(underflow)");
return;
}
printf("\nEnter element to delete:- ");
scanf("%d", &toDelete);
ptr = start;
while (ptr->next != NULL)
{
if (ptr->data == toDelete)
break;
preptr = ptr;
key = 1;
ptr = ptr->next;
}
if (ptr == start)
{
printf("\nData deleted = %d\n", ptr->data);
/* Clears the memory occupied by first node*/
start = NULL;
printf("SUCCESSFULLY DELETED LAST NODE FROM LIST\n");
return;
}
if (key == 1)
{
preptr->next = ptr->next;
printf("\nData deleted = %d\n", ptr->data);
/* Clears the memory occupied by first node*/
free(ptr);
printf("SUCCESSFULLY DELETED LAST NODE FROM LIST\n");
}
else
{
printf("\nElement not found in LL");
}
};
int main()
{
int ch;
char Opr[][31] = {"Create a List", "Traversal", "Searching in a list", "Insertion at the beginning", "Insertion at the end", "Insert before the given Node", "Insert after the given Node ", "Delete the first node", "Delete the last node", "Delete the given node", "Exit"};
do
{
printf("\n*****Main Menu*****\n");
for (int i = 0; i < sizeof(Opr) / 31; i++)
printf("%d. %s\n", i, Opr[i]);
printf(" Enter your choice:- ");
scanf("%d", &ch);
switch (ch)
{
case 0:
{
int n;
printf("\nEnter no. of node :- ");
scanf("%d", &n);
createList(n);
break;
}
case 1:
{
traverse();
break;
}
case 2:
{
int s, skey = -1;
printf("\nEnter element to search :- ");
scanf("%d", &s);
elementSearch(s, skey); // skey=search key for before and after insert
break;
}
case 3:
{
insertAtBeginning();
break;
}
case 4:
{
insertAtEnd(0);
break;
}
case 5:
{
insertBefGivNode();
break;
}
case 6:
{
insertAftGivNode();
break;
}
case 7:
{
delAtStart();
break;
}
case 8:
{
delAtEnd();
break;
}
case 9:
{
delGivNode();
break;
}
default:
{
if (ch > 10 || ch < 0)
printf("\nWrong selection!\n");
}
}
} while (ch != 10);
return 0;
}