forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
singly_linked_list.c
412 lines (359 loc) · 8.28 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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#include <stdio.h>
#include <malloc.h>
typedef struct node
{
int value;
struct node *next;
}node;
node* create_node(int);
void insert_node_first();
void insert_node_last();
void insert_node_pos();
void count();
void delete_pos();
void search();
void empty();
void display();
node *newnode, *ptr, *prev, *temp;
node *first = NULL, *last = NULL;
int main()
{
int ch;
char ans = 'Y';
while (ans == 'Y'||ans == 'y')
{
printf("\n---------------------------------\n");
printf("\nOperations on singly linked list\n");
printf("\n---------------------------------\n");
printf("\n1-Insert node at first");
printf("\n2-Insert node at last");
printf("\n3-Insert node at position");
printf("\n4-Display the total number of nodes");
printf("\n5-Delete Node from any Position");
printf("\n6-Check if list is empty or not");
printf("\n7-Search Element in the linked list");
printf("\n8-Display List from Beginning to end");
printf("\n9-Exit\n");
printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("\nEnter your choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("\nInserting node at first :\n");
insert_node_first();
break;
case 2:
printf("\nInserting node at last :\n");
insert_node_last();
break;
case 3:
printf("\nInserting node at position :\n");
insert_node_pos();
break;
case 4:
count();
break;
case 5:
delete_pos();
break;
case 6:
printf("\nchecking the list :\n");
empty();
break;
case 7:
printf("\nSearching Element in the List :\n");
search();
break;
case 8:
printf("\nDisplaying List From Beginning to End :\n");
display();
break;
case 9:
printf("\nExiting :\n");
return 0;
break;
default:
printf("\nInvalid Choice :\n");
break;
}
printf("\nYOU WANT TO CONTINUE (Y/N)");
scanf(" %c", &ans);
}
return 0;
}
node* create_node(int val)
{
newnode = (node *)malloc(sizeof(node));
if (newnode == NULL)
{
printf("\nMemory was not allocated");
return 0;
}
else
{
newnode->value = val;
newnode->next = NULL;
return newnode;
}
}
void insert_node_first()
{
int val;
printf("\nEnter the value for the node:");
scanf("%d", &val);
newnode = create_node(val);
if (first == last && first == NULL)
{
first = last = newnode;
first->next = NULL;
last->next = NULL;
}
else
{
temp = first;
first = newnode;
first->next = temp;
}
printf("\n----INSERTED----");
}
void insert_node_last()
{
int val;
printf("\nEnter the value for the Node:");
scanf("%d", &val);
newnode = create_node(val);
if (first == last && last == NULL)
{
first = last = newnode;
first->next = NULL;
last->next = NULL;
}
else
{
last->next = newnode;
last = newnode;
last->next = NULL;
}
printf("\n----INSERTED----");
}
void insert_node_pos()
{
int pos, val, cnt = 0, i;
printf("\nEnter the value for the Node:");
scanf("%d", &val);
newnode = create_node(val);
printf("\nEnter the position ");
scanf("%d", &pos);
ptr = first;
while (ptr != NULL)
{
ptr = ptr->next;
cnt++;
}
if (pos == 1)
{
if (first == last && first == NULL)
{
first = last = newnode;
first->next = NULL;
last->next = NULL;
}
else
{
temp = first;
first = newnode;
first->next = temp;
}
printf("\nInserted");
}
else if (pos>1 && pos<=cnt)
{
ptr = first;
for (i = 1;i < pos;i++)
{
prev = ptr;
ptr = ptr->next;
}
prev->next = newnode;
newnode->next = ptr;
printf("\n----INSERTED----");
}
else
{
printf("Position is out of range");
}
}
void count()
{
node *nxt;
int t = 0;
if (first == NULL)
{
printf("\nEMPTY LIST:");;
}
else
{
for (ptr = first;ptr != NULL;ptr = ptr->next)
{
t++;
}
printf("\nTotal Number of nodes : %d",t);
}
}
void delete_pos()
{
int pos, cnt = 0, i;
if (first == NULL)
{
printf("\nEMPTY LIST:");;
printf(":No node to delete\n");
}
else
{
printf("\nEnter the position of value to be deleted:");
scanf(" %d", &pos);
ptr = first;
if (pos == 1)
{
first = ptr->next;
printf("\nElement deleted");
}
else
{
while (ptr != NULL)
{
ptr = ptr->next;
cnt = cnt + 1;
}
if (pos > 0 && pos <= cnt)
{
ptr = first;
for (i = 1;i < pos;i++)
{
prev = ptr;
ptr = ptr->next;
}
prev->next = ptr->next;
}
else
{
printf("Position is out of range");
}
free(ptr);
printf("\nElement deleted");
}
}
}
void empty()
{
int oldval, newval, flag = 0;
if (first == NULL)
{
printf("The list is empty\n");
}
}
void search()
{
int flag = 0, key, pos = 0;
if (first == NULL)
{
printf("\nEMPTY LIST:");;
printf(":No nodes in the list\n");
}
else
{
printf("\nEnter the value to search");
scanf("%d", &key);
for (ptr = first;ptr != NULL;ptr = ptr->next)
{
pos = pos + 1;
if (ptr->value == key)
{
flag = 1;
break;
}
}
if (flag == 1)
{
printf("\nElement %d found at %d position\n", key, pos);
}
else
{
printf("\nElement %d not found in list\n", key);
}
}
}
void display()
{
if (first == NULL)
{
printf("\nEMPTY LIST:");;
printf(":No nodes in the list to display\n");
}
else
{
for (ptr = first;ptr != NULL;ptr = ptr->next)
{
printf("%d\t", ptr->value);
}
}
}
/*
Following are the sample of Input/Output processed by this program :
---------------------------------
Operations on singly linked list
---------------------------------
1-Insert node at first
2-Insert node at last
3-Insert node at position
4-Display the total number of nodes
5-Delete Node from any Position
6-Check if list is empty or not
7-Search Element in the linked list
8-Display List from Beginning to end
9-Exit
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter your choice : 1
Inserting node at first :
Enter the value for the node:12
----INSERTED----
YOU WANT TO CONTINUE (Y/N)y
---------------------------------
Operations on singly linked list
---------------------------------
1-Insert node at first
2-Insert node at last
3-Insert node at position
4-Display the total number of nodes
5-Delete Node from any Position
6-Check if list is empty or not
7-Search Element in the linked list
8-Display List from Beginning to end
9-Exit
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter your choice : 2
Inserting node at last :
Enter the value for the Node:13
----INSERTED----
YOU WANT TO CONTINUE (Y/N)y
---------------------------------
Operations on singly linked list
---------------------------------
1-Insert node at first
2-Insert node at last
3-Insert node at position
4-Display the total number of nodes
5-Delete Node from any Position
6-Check if list is empty or not
7-Search Element in the linked list
8-Display List from Beginning to end
9-Exit
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter your choice : 8
Displaying List From Beginning to End :
12 13
YOU WANT TO CONTINUE (Y/N)n
Process returned 0 (0x0) execution time : 31.014 s
Press any key to continue.
*/