-
Notifications
You must be signed in to change notification settings - Fork 46
/
circ_singly_ll.cpp
759 lines (560 loc) · 14.8 KB
/
circ_singly_ll.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
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
// Copyright 2019 Souvik Biswas
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include <iostream>
using namespace std;
// Function prototypes
struct node *insert_beg(node *, int );
struct node *insert_end(node *, int );
struct node *insert_after(node *, int );
struct node *insert_before(node *, int );
struct node *create(node *);
struct node *del_beg(node *);
struct node *del_end(node *);
struct node *del_after(node *);
struct node *del_before(node *);
struct node *del_element(node *);
void count(node *);
void display(node *);
/*
Declaring a structure called node
having two members:
1) data block
2) next block (which stores the address of the next node)
*/
struct node {
int data;
node *next;
};
/*
ALGORITHM FOR INSERTING AT THE BEGINNING:
1) IF AVAIL = NULL
Write Overflow
EXIT
2) NEW_NODE = AVAIL
3) AVAIL = AVAIL -> NEXT
4) NEW_NODE -> DATA = VALUE
5) NEW_NODE -> NEXT = START
6) PTR = START
7) WHILE PTR -> NEXT != START
PTR = PTR -> NEXT
8) PTR -> NEXT = NEW_NODE
9) START = NEW_NODE
10) EXIT
*/
node *insert_beg(node *start, int value) {
// Creating a new node
node *new_node = new node;
// Storing the data and assigning
// which address to keep in the next block
new_node->data = value;
new_node->next = start;
// Storing start in ptr
node *ptr = start;
// Incrementing till the last element
while(ptr->next != start) {
ptr = ptr->next;
}
// Updating the next block of the last node to store the address
// of new node
ptr->next = new_node;
// making the start point to be first element of the linked list
start = new_node;
return start;
}
/*
ALGORITHM FOR INSERTING AT THE END:
1) IF AVAIL = NULL
Write Overflow
EXIT
2) NEW_NODE = AVAIL
3) AVAIL = AVAIL -> NEXT
4) NEW_NODE -> DATA = VALUE
5) NEW_NODE -> NEXT = START
6) PTR = START
7) WHILE PTR -> NEXT != START
PTR = PTR -> next
8) PTR -> NEXT = NEW_NODE
7) EXIT
*/
node *insert_end(node *start, int value) {
// Storing the value of start to ptr
node *ptr = start;
// Creating a new node
node *new_node = new node;
// Storing the data and assigning
// which address to keep in the next block
new_node->data = value;
new_node->next = start;
// Searching for the last element after which we should insert
while(ptr->next != start) {
// Incrementing
ptr = ptr->next;
}
// Storing the address of new node to the next pointer of ptr
ptr->next = new_node;
return start;
}
/*
ALGORITHM FOR INSERTING AFTER A GIVEN NODE:
1) IF AVAIL = NULL
Write Overflow
EXIT
2) NEW_NODE = AVAIL
3) AVAIL = AVAIL -> NEXT
4) NEW_NODE -> DATA = VALUE
5) PTR = START
6) PREPTR = PTR
7) WHILE PREPTR -> NEXT != NUM
PREPTR = PTR
PTR = PTR -> NEXT
8) PREPTR -> NEXT = NEW_NODE
9) NEW_NODE -> NEXT = PTR
10) EXIT
*/
node *insert_after(node *start, int value) {
int num;
// Storing the value of start to ptr
node *ptr = start;
// Number after which to insert
cout<<"Enter the number after which to insert: ";
cin>>num;
// Creating a new node
node *new_node = new node;
// Storing the data
new_node->data = value;
// Taking another node to store the location of
// the element before ptr
node *preptr = ptr;
// Incrementing till the number is found
while(preptr->data != num) {
preptr = ptr;
ptr = ptr->next;
}
// Adding the new node
preptr->next = new_node;
new_node->next = ptr;
return start;
}
/*
ALGORITHM FOR INSERTING BEFORE A GIVEN NODE:
1) IF AVAIL = NULL
Write Overflow
EXIT
2) NEW_NODE = AVAIL
3) AVAIL = AVAIL -> NEXT
4) NEW_NODE -> DATA = VALUE
5) PTR = START
6) PREPTR = PTR
7) WHILE PTR -> NEXT != NUM
PREPTR = PTR
PTR = PTR -> NEXT
8) PREPTR -> NEXT = NEW_NODE
9) NEW_NODE -> NEXT = PTR
10) EXIT
*/
node *insert_before(node *start, int value) {
int num;
// Storing the value of start to ptr
node *ptr = start;
// Number after which to insert
cout<<"Enter the number before which to insert: ";
cin>>num;
// Creating a new node
node *new_node = new node;
// Storing the data
new_node->data = value;
// Taking another node to store the location of
// the element before ptr
node *preptr = ptr;
// Incrementing till the number is found
while(ptr->data != num) {
preptr = ptr;
ptr = ptr->next;
}
// Adding the new node
preptr->next = new_node;
new_node->next = ptr;
return start;
}
// For creating the entire linked list
node *create(node *start) {
int value;
// Storing the value to be inserted
cout<<"Enter -1 to stop\n";
cout<<"Enter the value to be stored: ";
cin>>value;
while(value != -1){
if(start == NULL) {
node *new_node = new node;
new_node->data = value;
start = new_node;
new_node->next = start;
}
else {
// Elements are inserted at the end
insert_end(start, value);
}
cout<<"Enter the value to be stored: ";
cin>>value;
}
return start;
}
/*
ALGORITHM FOR DELETING FROM THE BEGINNING:
1) IF START = NULL
Write Underflow
EXIT
2) PTR = START
3) WHILE PTR -> NEXT != START
PTR = PTR -> NEXT
4) PTR -> NEXT = START -> NEXT
5) DELETE START
6) START = PTR-> NEXT
7) EXIT
*/
node *del_beg(node *start) {
// Checking if the linked list is empty
if(start == NULL) {
cout<<"The linked list is empty !\n";
}
else {
// Storing start to ptr
node *ptr = start;
// Incrementing till the last position
while(ptr->next != start) {
ptr = ptr->next;
}
// Updating the next block of ptr
ptr->next = start->next;
// Printing the value to be deleted
cout<<"The deleted value is: "<<start->data<<endl;
// Deleting the first element
delete start;
// Assigning the new start
start = ptr->next;
}
return start;
}
/*
ALGORITHM FOR DELETING FROM THE END:
1) IF START = NULL
Write Underflow
EXIT
2) PTR = START
3) PREPTR = PTR
4) WHILE PTR -> NEXT != START
PREPTR = PTR
PTR = PTR -> NEXT
5) PREPTR -> NEXT = START
6) DELETE PTR
7) EXIT
*/
node *del_end(node *start) {
// Checking if the linked list is empty
if(start == NULL) {
cout<<"The linked list is empty !\n";
}
else {
// Storing start to ptr
node *ptr = start;
// Defining another pointer for storing
// the previous element of ptr
node *preptr = ptr;
// Incrementing till the last position
while(ptr->next != start) {
preptr = ptr;
ptr = ptr->next;
}
// Storing the address of start in the NEXT block as the
// the last element is to be deleted
preptr->next = start;
// Printing the value to be deleted
cout<<"The deleted value is: "<<ptr->data<<endl;
// Deleting the last element
delete ptr;
}
return start;
}
/*
ALGORITHM FOR DELETING AN ELEMENT AFTER A GIVEN NODE:
1) IF START = NULL
Write Underflow
EXIT
2) PTR = START
3) PREPTR = PTR
4) WHILE PREPTR -> DATA != NUM
PREPTR = PTR
PTR = PTR -> NEXT
5) PREPTR -> NEXT = PTR -> NEXT
6) DELETE PTR
7) EXIT
*/
node *del_after(node *start) {
// Checking if the linked list is empty
if(start == NULL) {
cout<<"The linked list is empty !\n";
}
else {
int num;
// Storing the element, after which to be deleted
cout<<"Enter the element after which to be deleted: ";
cin>>num;
// Storing start to ptr
node *ptr = start;
// Defining another pointer for storing
// the previous element of ptr
node *preptr = ptr;
// Incrementing till num is found
while(preptr->data != num) {
preptr = ptr;
ptr = ptr->next;
}
// Storing the address of element leaving one block,
// which is to be deleted
preptr->next = ptr->next;
// Printing the value to be deleted
cout<<"The deleted value is: "<<ptr->data<<endl;
// Deleting the element
delete ptr;
}
return start;
}
/*
ALGORITHM FOR DELETING AN ELEMENT BEFORE A GIVEN NODE:
1) IF START = NULL
Write Underflow
EXIT
2) PTR = START
3) PREPTR = PTR
4) WHILE PTR -> NEXT -> DATA != NUM
PREPTR = PTR
PTR = PTR -> NEXT
5) PREPTR -> NEXT = PTR -> NEXT
6) DELETE PTR
7) EXIT
*/
node *del_before(node *start) {
// Checking if the linked list is empty
if(start == NULL) {
cout<<"The linked list is empty !\n";
}
else {
int num;
// Storing the element, before which to be deleted
cout<<"Enter the element before which to be deleted: ";
cin>>num;
// Storing start to ptr
node *ptr = start;
// Defining another pointer for storing
// the previous element of ptr
node *preptr = ptr;
// Incrementing till num is found
while(ptr->next->data != num) {
preptr = ptr;
ptr = ptr->next;
}
// Storing the address of element leaving one block,
// which is to be deleted
preptr->next = ptr->next;
// Printing the value to be deleted
cout<<"The deleted value is: "<<ptr->data<<endl;
// Deleting the element
delete ptr;
}
return start;
}
/*
ALGORITHM FOR DELETING CURRENT ELEMENT:
1) IF START = NULL
Write Underflow
EXIT
2) PTR = START
3) PREPTR = PTR
4) WHILE PTR -> DATA != NUM
PREPTR = PTR
PTR = PTR -> NEXT
5) PREPTR -> NEXT = PTR -> NEXT
6) DELETE PTR
7) EXIT
*/
node *del_element(node *start) {
// Checking if the linked list is empty
if(start == NULL) {
cout<<"The linked list is empty !\n";
}
else {
int num;
// Storing the element, which to be deleted
cout<<"Enter the element which to be deleted: ";
cin>>num;
// Storing start to ptr
node *ptr = start;
// Defining another pointer for storing
// the previous element of ptr
node *preptr = ptr;
// Incrementing till num is found
while(ptr->data != num) {
preptr = ptr;
ptr = ptr->next;
}
// Storing the address of element leaving one block,
// which is to be deleted
preptr->next = ptr->next;
// Printing the value to be deleted
cout<<"The deleted value is: "<<ptr->data<<endl;
// Deleting the element
delete ptr;
}
return start;
}
/*
ALGORITHM FOR COUNTING THE NUMBER OF ELEMENTS:
1) IF START = NULL
Write Underflow
EXIT
2) PTR = START
3) SET COUNT = 0
4) WHILE PTR != NULL
COUNT = COUNT + 1
PTR = PTR -> NEXT
5) WRITE COUNT
6) EXIT
*/
void count(node *start) {
int count = 0;
// Checking if the linked list is empty
if(start == NULL) {
cout<<"The linked list is empty !\n";
}
else {
count = 1;
// Storing start in ptr
node *ptr = start;
// Incrementing till the end
while(ptr->next != start) {
count++;
ptr = ptr->next;
}
}
// Printing the number of elements
cout<<"The number of elements present in the linked list are: "
<<count
<<endl;
}
/*
ALGORITHM FOR DISPLAYING THE ELEMENTS:
1) IF START = NULL
Write Underflow
EXIT
2) PTR = START
3) WHILE PTR != START
Write PTR -> DATA
PTR = PTR -> NEXT
4) EXIT
*/
void display(node *start) {
// Checking if the linked list is empty
if(start == NULL) {
cout<<"The linked list is empty !\n";
}
else {
// Storing start in ptr
node *ptr = start;
// Incrementing till the end
while(ptr->next != start) {
// Printing the elements
cout<<ptr->data<<" ";
ptr = ptr->next;
}
cout<<ptr->data<<endl;
}
}
// Main Function
int main() {
// Initialize start to be null
node *start = NULL;
int option, value;
// Continuing to show the menu till the program is exited
do {
cout<<"\n********** MENU **********\n"
<<"1. Create the linked list\n"
<<"2. Insert at the beginning\n"
<<"3. Insert at the end\n"
<<"4. Insert after an element\n"
<<"5. Insert before an element\n"
<<"6. Delete from the beginning\n"
<<"7. Delete from the end\n"
<<"8. Delete after an element\n"
<<"9. Delete before an element\n"
<<"10. Delete the element\n"
<<"11. Count\n"
<<"12. Display\n"
<<"13. Exit\n";
cout<<"Enter your option: ";
cin>>option;
switch(option) {
case 1: start = create(start);
break;
case 2: // Storing the value to be inserted
cout<<"Enter the value to be stored: ";
cin>>value;
start = insert_beg(start, value);
break;
case 3: // Storing the value to be inserted
cout<<"Enter the value to be stored: ";
cin>>value;
start = insert_end(start, value);
break;
case 4: // Storing the value to be inserted
cout<<"Enter the value to be stored: ";
cin>>value;
start = insert_after(start, value);
break;
case 5: // Storing the value to be inserted
cout<<"Enter the value to be stored: ";
cin>>value;
start = insert_before(start, value);
break;
case 6: start = del_beg(start);
break;
case 7: start = del_end(start);
break;
case 8: start = del_after(start);
break;
case 9: start = del_before(start);
break;
case 10: start = del_element(start);
break;
case 11: count(start);
break;
case 12: display(start);
break;
case 13: break;
default: cout<<"Wrong option is selecred !!\n";
break;
}
} while(option != 13);
// Freeing the space occupied by start
if(option == 13) {
cout<< "\nTHANK YOU for using the program !\n"
<<"Have a good day.\n\n";
delete start;
}
}