-
Notifications
You must be signed in to change notification settings - Fork 0
/
circularLinkedList.cpp
377 lines (356 loc) · 8.5 KB
/
circularLinkedList.cpp
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
//circular linked list
#include <bits/stdc++.h>
using namespace std;
typedef struct node
{
int data;
node *next = NULL;
} node;
// Function prototypes
void mainMenu();
node *lastNode(node *head);
node *traverse(node *head);
node *insertAtHead(node *head, int val);
node *insertBefore(node *head, int key, int val);
node *insertAfter(node *head, int key, int val);
node *insertAtTail(node *head, int val);
node *deleteAtHead(node *head);
node *deleteBefore(node *head, int key);
node *deleteAfter(node *head, int key);
void deleteTail(node *head);
node *reverseLL(node *head);
void mainMenu()
{
cout << "\n==================MAIN MENU==================" << endl;
cout << "1. Insert At head" << endl;
cout << "2. Insert before value" << endl;
cout << "3. Insert after value" << endl;
cout << "4. Insert at tail" << endl;
cout << "5. Delete At head" << endl;
cout << "6. Delete before value" << endl;
cout << "7. Delete after value" << endl;
cout << "8. Delete at tail" << endl;
cout << "9. Traverse the circular link list" << endl;
cout << "10. Reverse the circular link List" << endl;
cout << "11. Exit" << endl;
}
node *lastNode(node *head)
{
node *temp = head;
if (head == NULL)
return head;
do
temp = temp->next;
while (temp->next != head);
return temp;
}
node *traverse(node *head)
{
node *temp = head;
if (head == NULL)
{
cout << "Empty linked list";
return head;
}
do
{
cout << temp->data << " -> ";
temp = temp->next;
} while (temp != head);
cout << "(head)";
return temp;
}
node *insertAtHead(node *head, int val)
{
node *ptr = new node();
ptr->data = val;
if (head == NULL)
{
ptr->next = ptr;
head = ptr;
return head;
}
node *lastPtr = lastNode(head);
ptr->next = head;
lastPtr->next = ptr;
head = ptr;
return head;
}
node *insertBefore(node *head, int key, int val)
{
if (head == NULL)
{
cout << "Linked list is empty so key doesn't exist";
return head;
}
node *temp = head;
node *nodePtr = new node();
nodePtr->data = val;
if (head->data == key)
{
head = insertAtHead(head, val);
return head;
}
while (temp->next != head && temp->next->data != key)
temp = temp->next;
if (temp->next == head && temp->data != key)
{
cout << "Key doesn't exist";
return head;
}
nodePtr->next = temp->next;
temp->next = nodePtr;
return head;
}
node *insertAfter(node *head, int key, int val)
{
if (head == NULL)
{
cout << "Linked list is empty so key doesn't exist";
return head;
}
node *temp = head;
node *nodePtr = new node();
nodePtr->data = val;
while (temp->next != head && temp->data != key)
temp = temp->next;
if (temp->next == head)
{
if (temp->data != key)
{
cout << "Key doesn't exist";
return head;
}
insertAtTail(head, val);
return head;
}
nodePtr->next = temp->next;
temp->next = nodePtr;
return head;
}
node *insertAtTail(node *head, int val)
{
node *ptr = new node();
if (head == NULL)
{
return insertAtHead(head, val);
}
else
{
node *lastPtr = lastNode(head);
ptr->data = val;
lastPtr->next = ptr;
ptr->next = head;
}
return head;
}
node *deleteAtHead(node *head)
{
if (head->next == head)
{
delete (head);
head = NULL;
return head;
}
node *temp = head->next;
node *lastPtr = lastNode(head);
delete (head);
head = temp;
lastPtr->next = head;
return head;
}
node *deleteBefore(node *head, int key)
{
if (head == NULL)
{
cout << "Can't delete as linked list is empty";
return head;
}
if (head->data == key)
{
deleteTail(head);
return head;
}
if (head->next->data == key)
{
head = deleteAtHead(head);
return head;
}
node *currentNodePtr = head;
node *prevNodePtr = NULL;
while (currentNodePtr->next != head && currentNodePtr->next->data != key)
{
prevNodePtr = currentNodePtr;
currentNodePtr = currentNodePtr->next;
}
if (currentNodePtr->next == head)
{
if (currentNodePtr->data != key)
cout << "Key doesn't exist";
else
deleteTail(head);
return head;
}
prevNodePtr->next = currentNodePtr->next;
delete (currentNodePtr);
return head;
}
node *deleteAfter(node *head, int key)
{
if (head == NULL)
{
cout << "Can't delete as linked list is empty";
return head;
}
node *temp = head;
while (temp->next != head && temp->data != key)
temp = temp->next;
if (temp->next == head)
{
if (temp->data != key)
cout << "Key doesn't exist";
else
head = deleteAtHead(head);
return head;
}
node *temp2 = temp;
temp->next = temp->next->next;
delete (temp2->next);
return head;
}
void deleteTail(node *head)
{
if (head == NULL)
{
cout << "Linked List doesn't exists.";
return;
}
if (head->next == head)
head = deleteAtHead(head);
node *temp = head;
while (temp->next->next != head)
temp = temp->next;
delete (temp->next);
temp->next = head;
}
node *reverseLL(node *head)
{
if (head == NULL)
{
cout << "Can't do the reverse as linked list is empty";
return head;
}
if (head->next == head)
return head;
node *prevNode = NULL;
node *currentNode = head;
node *nextNode = NULL;
do
{
nextNode = currentNode->next;
currentNode->next = prevNode;
prevNode = currentNode;
currentNode = nextNode;
} while (currentNode != head);
head->next = prevNode;
head = prevNode;
return head;
}
int main()
{
node *head = NULL;
int startLinkSize = 0;
cout << "Enter the starting linked List size :";
cin >> startLinkSize;
cout << "Enter the element with space or enter in between them :";
int temp;
while (startLinkSize != 0)
{
cin >> temp;
head = insertAtTail(head, temp);
startLinkSize--;
}
int switchCase = 0;
int in;
while (switchCase != 11)
{
mainMenu();
cin >> switchCase;
switch (switchCase)
{
case 1:
cout << "Enter the data value" << endl;
cin >> in;
cout << endl;
head = insertAtHead(head, in);
traverse(head);
break;
case 2:
{
int key;
cout << "Enter the data value before which you want to add new node" << endl;
cin >> key;
cout << "Enter the data value" << endl;
cin >> in;
cout << endl;
head = insertBefore(head, key, in);
traverse(head);
break;
}
case 3:
{
int key;
cout << "Enter the data value after which you want to add new node" << endl;
cin >> key;
cout << "Enter the data value" << endl;
cin >> in;
cout << endl;
head = insertAfter(head, key, in);
traverse(head);
break;
}
case 4:
cout << "Enter the data value" << endl;
cin >> in;
cout << endl;
head = insertAtTail(head, in);
traverse(head);
break;
case 5:
head = deleteAtHead(head);
traverse(head);
break;
case 6:
cout << "Enter the data value before which node will be deleted" << endl;
cin >> in;
cout << endl;
head = deleteBefore(head, in);
traverse(head);
break;
case 7:
cout << "Enter the data value after which node will be deleted" << endl;
cin >> in;
cout << endl;
head = deleteAfter(head, in);
traverse(head);
break;
case 8:
deleteTail(head);
traverse(head);
break;
case 9:
traverse(head);
break;
case 10:
head = reverseLL(head);
traverse(head);
break;
case 11:
switchCase = 11;
break;
default:
break;
}
}
return 0;
}