Repository navigation
Expand file tree
/
Copy pathcircularLInkedList.cpp
More file actions
498 lines (433 loc) · 12.8 KB
/
Copy pathcircularLInkedList.cpp
File metadata and controls
498 lines (433 loc) · 12.8 KB
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int data;
Node *next;
Node(int el)
{
data = el;
next = NULL;
}
};
class circularLinkedList {
public:
Node *head;
circularLinkedList() {
this->head = NULL;
}
void insertAtHead(int el)
{
Node *n = new Node(el);
if (this->head == NULL)
{
this->head = n;
this->head->next = this->head;
}
Node *last_element = this->head;
while (last_element->next != this->head)
{
last_element = last_element->next;
}
n->next = this->head;
last_element->next = n;
this->head = n;
}
void display()
{
if (this->head == NULL)
{
cout << "Empty linked list" << endl;
return;
}
Node *temp = this->head;
if (temp->next == this->head)
{
cout << temp->data;
}
else
{
while (temp->next != this->head)
{
cout << temp->data;
cout << "->";
temp = temp->next;
}
cout << temp->data;
}
cout << endl;
}
int search_in_list(int el)
{
int position = 1;
if (this->head == NULL)
{
return -1;
}
Node *temp = this->head;
do
{
if (temp->data == el)
{
return position;
}
temp = temp->next;
position++;
} while (temp != head);
return -1;
}
void insertAfterPosition( int elementToEnter, int pos)
{
if (pos == -1)
{
return;
}
else
{
if (this->head == NULL)
{
cout << "List is empty";
return;
}
Node *n = new Node(elementToEnter);
Node *temp = head;
for (int i = 1; i < pos; i++)
{
if (temp->next == this->head)
{
cout << "List is shorter then the input index " << endl;
return;
}
temp = temp->next;
}
Node *newTemp = temp->next;
temp->next = n;
n->next = newTemp;
}
}
void insertAtEnd( int elementToEnter)
{
Node *n = new Node(elementToEnter);
if (this->head == NULL)
{
insertAtHead(elementToEnter);
return;
}
Node *temp = this->head;
do
{
temp = temp->next;
} while (temp->next != head);
temp->next = n;
n->next = this->head;
}
void deleteFromStart()
{
if (this->head == NULL)
{
cout << "Operation not possible" << endl;
return;
}
Node *temp = this->head;
while (temp->next != this->head)
{
temp = temp->next;
}
if (temp == this->head) {
delete head;
return ;
} else{
Node* old_head = this->head;
this->head = old_head->next;
temp->next = this->head;
delete old_head;
}
}
void deleteFromEnd()
{
if (this->head == NULL)
{
cout << "Operation not possible" << endl;
return;
}
else if (this->head->next == this->head)
{
delete this->head;
}
else
{
Node *temp = this->head;
while (temp->next->next != head)
{
temp = temp->next;
}
delete temp->next;
temp->next = this->head;
}
}
void removeNodeWithGivenElement(int pos)
{
if (pos == -1)
{
cout << "Operation not possible" << endl;
}
else if (pos == 1)
{
deleteFromStart();
}
else
{
Node *temp = this->head;
for (int i = 1; i < pos - 1; i++)
{
if (temp->next == head)
{
cout << "List is shorter then the input index " << endl;
}
temp = temp->next;
}
temp->next = temp->next->next;
}
}
void printPointerOfPos( int pos)
{
Node *temp = head;
for (int i = 1; i < pos; i++)
{
if (temp->next == this->head)
{
cout << "List is shorter then the input index " << endl;
return;
}
temp = temp->next;
}
cout << "Element found at position " << pos << " and its pointer is ";
cout << temp->next << endl;
}
Node* createNewLinkedList() {
this->insertAtHead(30);
this->insertAtHead(20);
this->insertAtHead(10);
return this->head;
}
void concatenateTwoLinkedLists()
{
if (this->head == NULL)
{
cout << "Empty linked list cannot be concatenated " << endl;
return;
}
else
{
Node *temp1 = this->head;
circularLinkedList cll2;
Node *temp2 = cll2.createNewLinkedList();
Node *head2 = temp2;
while (temp1->next != head) {
temp1 = temp1->next;
}
temp1->next = temp2;
while (temp2->next != head2)
{
temp2 = temp2->next;
}
temp2->next = this->head;
cout << "Both linked list concatenated successfully" << endl;
}
}
Node* findMiddle() {
if (this->head == NULL) {
return NULL;
}
Node* slow_p = this->head;
Node* fast_p = this->head;
while(fast_p->next != this->head && fast_p->next->next != this->head) {
slow_p = slow_p->next;
fast_p = fast_p->next->next;
}
return slow_p;
}
void insertNodeInSortedOrder(int data) {
if (this->head == NULL) {
this->insertAtHead(data);
return;
}
Node* prev = NULL;
Node* temp = this->head;
while(temp->data < data && temp->next != this->head) {
prev = temp;
temp = temp->next;
}
if (prev == NULL) {
if (temp->data > data) {
this->insertAtHead(data);
} else{
this->insertAtEnd(data);
}
return;
}
Node* n = new Node(data);
prev->next = n;
n->next = temp;
}
void exchangeFirstWithLast(){
if (this->head->next == this->head ) {
return;
}
Node* temp = this->head;
while(temp->next != this->head) {
temp = temp->next;
}
auto temp_d = this->head->data;
this->head->data = temp->data;
temp->data = temp_d;
}
};
circularLinkedList splitList(circularLinkedList original) {
Node* middle = original.findMiddle();
Node* temp = middle->next;
Node* new_head = temp;
middle->next = original.head;
circularLinkedList newCll;
while(temp->next != original.head) {
temp = temp->next;
}
temp->next = new_head;
newCll.head = new_head;
return newCll;
}
int JosephusCircle(int n, int k) {
circularLinkedList josephusCircleList;
for (int i = 0; i < n; i++) {
josephusCircleList.insertAtEnd(i);
}
josephusCircleList.display();
Node* temp = josephusCircleList.head;
while(temp->next != temp){
for (int i = 1; i < k-1; ++i) {
temp = temp->next;
}
// if (temp->next = josephusCircleList.head) {
// josephusCircleList.head = josephusCircleList.head->next;
// }
Node* delNode = temp->next;
temp->next = temp->next->next;
if (delNode == josephusCircleList.head) {
josephusCircleList.head = josephusCircleList.head->next;
}
delNode->next = NULL;
cout << "deleting :" << delNode->data << endl;
delete(delNode);
}
return josephusCircleList.head->data;
}
int main(int argc, char const *argv[])
{
// cout << JosephusCircle(4,2) << endl;
circularLinkedList cll;
cll.insertAtEnd(10);
cll.insertAtEnd(20);
cll.insertAtEnd(30);
cll.insertAtEnd(40);
cll.insertAtEnd(50);
cll.insertAtEnd(60);
cll.insertAtEnd(70);
cll.exchangeFirstWithLast();
cll.display();
// circularLinkedList cll2 = splitList(cll);
// cll.display();
// cll2.display();
// cout << "-------CPP program implementing a linked list------- " << endl
// << endl;
// circularLinkedList cll;
// char more;
// int element, position, search;
// do
// {
// cout << "(i) Insert an element at the beginning of the circular linked list" << endl
// << "(ii) Insert an element x after an element y in the circular linked list" << endl
// << "(iii)Insert an element at the end of the circular linked list" << endl
// << "(iv) Remove an element from the back of the circular linked list." << endl
// << "(v) Remove an element from the front of the circular linked list." << endl
// << "(vi) Remove the element from the circularly linked list " << endl
// << "(vii)Search for an element from the circularly linked list and return its pointer" << endl
// << "(viii) Concatenate two circularly linked lists" << endl
// << endl;
// int choice;
// cout << "Enter which operation you want to peform : ";
// cin >> choice;
// switch (choice)
// {
// case 1:
// cout << "Enter the element you want to enter : ";
// cin >> element;
// cll.insertAtHead( element);
// cout << "Element successfully inserted at head" << endl;
// cll.display();
// break;
// case 2:
// cout << "Enter the element you want to enter : ";
// cin >> element;
// cout << "Enter the element whose next the given element must reside : ";
// cin >> search;
// if (cll.search_in_list(search) == -1)
// {
// cout << "No element as " << search << " in the list" << endl;
// break;
// }
// cll.insertAfterPosition( element, cll.search_in_list(search));
// cll.display();
// break;
// case 3:
// cout << "Enter the element you want to enter : ";
// cin >> element;
// cll.insertAtEnd(element);
// cll.display();
// break;
// case 4:
// cll.deleteFromStart();
// cll.display();
// break;
// case 5:
// cll.deleteFromEnd();
// cll.display();
// break;
// case 6:
// cout << "Enter Element you want to remove :";
// cin >> search;
// cll.removeNodeWithGivenElement(cll.search_in_list(search));
// cll.display();
// break;
// case 7:
// cout << "Enter which element's pointer you want to print :";
// cin >> search;
// position = cll.search_in_list( search);
// if (position == -1)
// {
// cout << "Element Not found in the list " << endl;
// break;
// }
// else
// {
// cll.printPointerOfPos( position);
// }
// break;
// case 8:
// {
// cll.concatenateTwoLinkedLists();
// cll.display();
// break;
// }
// default:
// cin.clear();
// cout << "Invalid input" << endl;
// break;
// }
// cout << "Do you want to continue operations on linked list, press n/N to exit : ";
// cin >> more;
// cout << endl
// << endl;
// } while (more != 'n' && more != 'N');
return 0;
}