-
Notifications
You must be signed in to change notification settings - Fork 0
/
4CircularSinglyLinkedList.c
355 lines (317 loc) · 7.02 KB
/
4CircularSinglyLinkedList.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
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node* link;
}node;
int count(node* tail)
{
int c = 0;
if(tail == NULL)
{
return 0;
}
else
{
node* ptr = tail->link;
c++;
ptr = ptr->link;
while(ptr != tail->link)
{
c++;
ptr = ptr->link;
}
}
return c;
}
node* InsertAtBeginning(node* tail, int val)
{
node* temp = (node*)malloc(sizeof(node));
if(tail == NULL)
{
temp->data = val;
temp->link = temp;
tail = temp;
}
else
{
temp->data = val;
temp->link = NULL;
temp->link = tail->link;
tail->link = temp;
}
return tail;
}
void display(node* tail)
{
if(tail == NULL)
{
printf("Linked list is empty!\n");
}
else
{
node* ptr = tail->link;
printf("%d\n", ptr->data);
ptr = ptr->link;
while(ptr != tail->link)
{
printf("%d\n", ptr->data);
ptr = ptr->link;
}
}
}
node* InsertAtEnd(node* tail, int val)
{
if(tail == NULL)
{
printf("Linked list is empty!\n");
}
else
{
node* temp = (node*)malloc(sizeof(node));
temp->data = val;
temp->link = tail->link;
tail->link = temp;
tail = tail->link;
}
return tail;
}
node* InsertInBetween(node* tail, int pos, int val)
{
int c = count(tail);
if(pos == 1)
{
tail = InsertAtBeginning(tail, val);
}
else if(pos == c+1)
{
tail = InsertAtEnd(tail, val);
}
else
{
node* temp = (node*)malloc(sizeof(node));
temp->data = val;
temp->link = NULL;
node* ptr = tail->link;
pos--;
while(pos != 1)
{
ptr = ptr->link;
pos--;
}
temp->link = ptr->link;
ptr->link = temp;
}
return tail;
}
node* DeleteFromBeginning(node* tail)
{
if(tail == NULL)
{
printf("Linked list is already empty!\n");
}
else
{
node* ptr = tail->link;
tail->link = ptr->link;
free(ptr);
ptr = NULL;
}
return tail;
}
node* DeleteFromEnd(node* tail)
{
if(tail == NULL)
{
printf("Linked list is already Empty!\n");
}
else
{
node* ptr = tail;
node* temp = tail->link;
while(temp->link != tail)
{
temp = temp->link;
}
temp->link = ptr->link;
free(ptr);
ptr = NULL;
tail = temp;
}
return tail;
}
node* DeleteInBetween(node* tail, int pos)
{
int c = count(tail);
if(tail == NULL)
{
printf("Linked list is already empty!\n");
}
else if(pos == 1)
{
tail = DeleteFromBeginning(tail);
}
else if(pos == c)
{
tail = DeleteFromEnd(tail);
}
else
{
node* ptr1 = tail->link;
node* ptr2 = NULL;
while(pos != 1)
{
ptr2 = ptr1;
pos--;
ptr1 = ptr1->link;
}
ptr2->link = ptr1->link;
free(ptr1);
ptr1 = NULL;
}
return tail;
}
void search(node* tail, int val)
{
int c = 0;
if(tail == NULL)
{
printf("Linked list is empty!\n");
}
else
{
node* ptr = tail->link;
ptr = ptr->link;
if(ptr->data == val)
{
c++;
}
while(ptr != tail->link)
{
ptr = ptr->link;
if(ptr->data == val)
{
c++;
}
}
}
if(c == 0)
{
printf("The element is not in the linked list\n");
}
else
{
printf("Element found!\n");
}
}
node* CreateList(node* tail)
{
int n, val;
printf("Enter the number of nodes you want to enter: ");
scanf("%d", &n);
if(n == 0)
{
return tail;
}
else
{
printf("Enter the value for node 1: ");
scanf("%d", &val);
tail = InsertAtBeginning(tail, val);
for(int i=1; i<n; i++)
{
printf("Enter the value for node %d: ", i+1);
scanf("%d", &val);
tail = InsertAtEnd(tail, val);
}
}
return tail;
}
int main()
{
node* tail = (node*)malloc(sizeof(node));
tail = NULL;
tail = CreateList(tail);
int op, val, c, pos;
while(1)
{
printf("Enter 1 to Insert a node at beginning\n");
printf("Enter 2 to Insert the node at the end\n");
printf("Enter 3 to Insert a node in between two nodes\n");
printf("Enter 4 to Delete the node from the beginning\n");
printf("Enter 5 to delete the node form the end\n");
printf("Enter 6 to delete the node in between two nodes\n");
printf("Enter 7 to search an element in the list\n");
printf("Enter 8 to count the number of nodes in the linked list\n");
printf("Enter 9 to print the linked list\n");
printf("Enter 10 to exit\n");
printf("Enter your option: ");
scanf("%d", &op);
switch(op)
{
case 1:
printf("Enter the value to want to enter in the beginning: ");
scanf("%d", &val);
tail = InsertAtBeginning(tail, val);
break;
case 2:
printf("Enter the value you want to enter at the end: ");
scanf("%d", &val);
tail = InsertAtEnd(tail, val);
break;
case 3:
c = count(tail);
printf("Enter the position at which you want to insert the node: ");
scanf("%d", &pos);
if(pos < 1 || pos > c+1)
{
printf("Invalid position!\n");
}
else
{
printf("Enter the value you want to enter: ");
scanf("%d", &val);
tail = InsertInBetween(tail, pos, val);
}
break;
case 4:
tail = DeleteFromBeginning(tail);
break;
case 5:
tail = DeleteFromEnd(tail);
break;
case 6:
c = count(tail);
printf("Enter the position from which you want to delete the node: ");
scanf("%d", &pos);
if(pos < 1 || pos > c)
{
printf("Invalid position!\n");
}
else
{
tail = DeleteInBetween(tail, pos);
}
break;
case 7:
printf("Enter the value you want to search in the linked list: ");
scanf("%d", &val);
search(tail, val);
case 8:
c = count(tail);
printf("Total number of nodes in the linked list are: %d\n", c);
break;
case 9:
display(tail);
break;
case 10:
exit(1);
break;
default:
printf("Wrong input\n");
break;
}
}
return 0;
}