-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedlist.cpp
More file actions
545 lines (523 loc) · 9.76 KB
/
linkedlist.cpp
File metadata and controls
545 lines (523 loc) · 9.76 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
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
/* This file was first created on 18th August,2018 by Keshav Tangri
This is for educational purposes only.
This is for B.Tech Students who are or will be studying Data Structures in there curriculum
For queries mail me @ : tangri57@gmail.com
*/
#include<iostream>
#include<cstdlib>
#include<math.h>
using namespace std;
class Node{
public:
Node* next;
int data;
};
class llist{
public:
Node* head = new Node;
llist(){
head=NULL;
}
Node* createnode(int value); //line 114
void addatbeg(int val);//line 150
void display();//line 136
void addele(int val);//line 121
void addatpos(int val);//line 246
void deleteele(int pos);//line 157
void deletelist();//line 424
int searchele(int val);//line 191
void updateval(int val);//line 212
void reverselist();//line 278
void sortlist();//line 299
void display_nth_element(int pos);//line 322
void display_nth_element_from_last(int pos);//line 350
void palindrome_list();//line 365
void remove_duplicate_usorted_list();//line 398
void swap_nodes_without_swapping_values(int val1,int val2);
};
//MAIN FUNCTION BEGINS HERE !
int main(){
int value;
int posi=0;
llist obj;
while(1){
int i;
cout<<"1.Add element at first position\n2.Add element at last position\n3.Add element at a position\n4."
<<"Display List\n5.Delete Element\n6.Delete Whole List\n7.Search an Element\n8.Update Value\n9.Reverse the list\n10.Sort the List\n11.Display N-th Element\n"
<<"12.Display N-th Element From Last\n13.Palindrome Linked List\n14.Remove Duplicates\n15.Swap nodes without swapping values\n16.Exit\nChoose your option : ";
cin>>i;
switch(i){
case 1:
cout<<"Enter value you want to add : ";
cin>>value;
cout<<"\n";
obj.addatbeg(value);
break;
case 2:
cout<<"Enter value you want to add : ";
cin>>value;
cout<<"\n";
obj.addele(value);
break;
case 3:
cout<<"Enter value you want to add : ";
cin>>value;
obj.addatpos(value);
break;
case 4:
obj.display();
break;
case 5:
cout<<"Enter position where you want to delete : ";
cin>>value;
obj.deleteele(value);
break;
case 6:
obj.deletelist();
break;
case 7:
cout<<"Enter the value of element you want to search : ";
cin>>value;
posi=obj.searchele(value);
if(posi==0){
cout<<"\n\nELEMENT NOT FOUND !\n\n";
}else{
cout<<"\n\nElement found at : "<<posi<<" position.\n\n";
}
break;
case 8:
cout<<"Enter the value of the Element you want to update : ";
cin>>value;
obj.updateval(value);
break;
case 9:
obj.reverselist();
break;
case 10:
obj.sortlist();
break;
case 11:
cout<<"Enter Position of the Element : ";
cin>>value;
obj.display_nth_element(value);
break;
case 12:
cout<<"Enter Position of the Element from Last: ";
cin>>value;
obj.display_nth_element_from_last(value);
break;
case 13:
obj.palindrome_list();
break;
case 14:
obj.remove_duplicate_usorted_list();
break;
case 15:
cout<<"\n\nUNDER MAINTAINENCE\n\n";
int val2;
cout<<"Enter the value of 2 nodes you want to swap : ";
cin>>value;
cin>>val2;
obj.swap_nodes_without_swapping_values(value,val2);
break;
case 16:
exit(0);
break;
}
}
}
//CLASS FUNCTIONS BEGINS HERE !
//CREATING THE NODE !
Node* llist::createnode(int value){
Node* temp =new Node;
temp->data = value;
return temp;
}
//ADDING ELEMENT AT LAST !
void llist::addele(int val){
if(head == NULL){
addatbeg(val);
}else{
Node* temp = createnode(val);
Node* ptr =head;
while(ptr->next!=NULL){
ptr=ptr->next;
}
temp->next=NULL;
ptr->next=temp;
}
}
//PRINTING THE LINKED LIST !
void llist::display(){
Node* ptr = head;
if(head==NULL){
cout<<"\nList does not exist !\n";
return;
}
while(ptr!=NULL){
cout<<ptr->data<<" ";
ptr = ptr->next;
}
cout<<"\n\n";
}
//ADD ELEMENT AT BEGINNING !
void llist::addatbeg(int val){
Node* temp = createnode(val);
temp->next = head;
head = temp;
}
//DELETE AT POSITION !
void llist::deleteele(int pos){
if(head == NULL){
cout<<"ERROR !\nLIST IS EMPTY !\n";
return;
}
if(pos==1){
Node* p = head;
head = p->next;
delete p;
cout<<"\nElement Deleted !\n";
}else{
int counter = 0;
Node* p = head;
while(p!=NULL){
p=p->next;
counter++;
}
if(pos>1&&pos<=counter){
p = head;
Node* prev;
for(int i=1;i<pos;i++){
prev =p;
p=p->next;
}
prev->next=p->next;
delete p;
cout<<"\nELEMENT DELETED SUCCESSFULLY !\n";
}else{
cout<<"\nOUT OF RANGE EXCEPTION !\n\n";
}
}
}
//SEARCHING AN ELEMENT FUNCTION !
int llist::searchele(int val){
int counter=0;
Node* p = head;
if(head==NULL){
cout<<"\nEMPTY LIST !\n";
return counter;
}
while(p!=NULL){
counter++;
if(p->data==val){
break;
}
p=p->next;
}
if(p==NULL){
counter = 0;
}
return counter;
}
//UPDATING A VALUE FUNCTION
void llist::updateval(int val){
int counter=0;
Node* p = head;
if(head==NULL){
cout<<"\nEMPTY LIST !\n";
return;
}
while(p!=NULL){
counter++;
if(p->data==val){
break;
}
p=p->next;
}
if(p==NULL){
cout<<"\nVALUE NOT PRESENT !\n";
return;
}
if(p!=NULL){
cout<<"Enter New Value to be updated : ";
int temp;
cin>>temp;
p->data = temp;
cout<<"\nVALUE UPDATED !\n\n";
}
}
//ADDING ELEMENT AT A POSITION !
void llist::addatpos(int val){
cout<<"Enter position where you want to add the element : ";
int pos;
cin>>pos;
Node* ptr = head;
int counter =0;
if(pos==1){
addatbeg(val);
cout<<"\n\nELEMENT ADDED SUCCESSFULLY !\n\n";
}else{
while(ptr!=NULL){
counter++;
ptr=ptr->next;
}
if(pos>1&&pos<=counter){
Node* prev;
Node* temp = createnode(val);
ptr = head;
for(int i =1;i<pos;i++){
prev = ptr;
ptr=ptr->next;
}
prev->next = temp;
temp->next = ptr;
cout<<"\n\nELEMENT ADDED SUCCESSFULLY !\n\n";
}else{
cout<<"\n\nEND OF RANGE EXCEPTION !\n\n";
return;
}
}
}
//REVERSING THE LIST FUNCTION
void llist::reverselist(){
if(head==NULL){
cout<<"\n\nEMPTY LIST !\n\n";
return;
}
Node* curr;
Node* prev;
curr = head;
head = head->next;
curr->next=NULL;
while(head->next!=NULL){
prev = curr;
curr = head;
head = head->next;
curr->next = prev;
}
head->next=curr;
cout<<"\n\nLIST REVERSED SUCCESSFULLY !\n\n";
}
//SORTING THE LIST FUNCTION !
void llist::sortlist(){
if(head==NULL){
cout<<"\n\nEMPTY LIST !\n\n";
return;
}else{
Node* ptr = head;
Node* s=ptr->next;
while(ptr!=NULL){
while(s!=NULL){
if(ptr->data>s->data){
int temp = ptr->data;
ptr->data=s->data;
s->data=temp;
}
s=s->next;
}
ptr=ptr->next;
s=ptr;
}
cout<<"\n\nLIST SORTED SUCCESSFULLY !\n\n";
}
}
//DISPLAY NTH ELEMENT IN A LINKED LIST FUNCTION
void llist::display_nth_element(int pos){
if(head ==NULL){
cout<<"\n\nEMPTY LIST\n\n";
return;
}
if(pos==1){
cout<<"\n\nValue at "<<pos<<" is : "<<head->data<<"\n\n";
return;
}else{
Node* ptr = head;
int counter=0;
while(ptr!=NULL){
counter++;
ptr=ptr->next;
}
if(pos>=counter){
cout<<"\n\nOUT OF RANGE EXCEPTION !\n\n";
return;
}else{
ptr = head;
for(int i=1;i<pos;i++){
ptr=ptr->next;
}
cout<<"\n\nValue at required position is : "<<ptr->data<<"\n\n";
}
}
}
//DISPLAY NTH ELEMENT FROM LAST IN A LINKED LIST function
void llist::display_nth_element_from_last(int pos){
if(head ==NULL){
cout<<"\n\nEMPTY LIST\n\n";
return;
}
Node* ptr = head;
int counter=0;
while(ptr!=NULL){
counter++;
ptr=ptr->next;
}
pos = counter-pos+1;
display_nth_element(pos);
}
//PALINDROME LINKED LIST FUNCTION !
void llist::palindrome_list(){
if(head==NULL){
cout<<"\n\nEMPTY LIST !\n\n";
return;
}
Node* ptr = head;
int counter=0;
while(ptr!=NULL){
counter++;
ptr=ptr->next;
}
ptr = head;
llist ob2;
for(int i=0;i<counter;i++){
ob2.addatbeg(ptr->data);
ptr = ptr->next;
}
ptr = head;
Node* ptrnew = ob2.head;
bool flag = true;
for(int i=0;i<counter;i++){
if(ptr->data!=ptrnew->data){
flag = false;
break;
}
ptr=ptr->next;
ptrnew=ptrnew->next;
}
if(flag){
cout<<"\n\nLinked List is a Palindrome !\n\n";
}else{
cout<<"\n\nLinked List is not a Palindrome !\n\n";
}
}
//REMOVING DUPLICATES FROM LIST
void llist::remove_duplicate_usorted_list(){
if(head==NULL){
cout<<"\n\nEMPTY LIST !\n\n";
return;
}
Node* ptr =head;
Node* prev;
Node* nextptr;
while(ptr!=NULL){
prev = ptr;
nextptr = ptr->next;
while(nextptr!=NULL){
if(nextptr->data==ptr->data){
prev->next=nextptr->next;
delete nextptr;
nextptr = prev->next;
}else{
prev = nextptr;
nextptr = nextptr->next;
}
}
ptr = ptr->next;
}
cout<<"\n\nDUPLICATES REMOVED SUCCESSFULLY !\n\n";
}
//DELETE WHOLE LIST FUNCTION
void llist::deletelist(){
if(head==NULL){
cout<<"\n\nEMPTY LIST !\n\n";
return;
}
Node*ptr = head;
Node*prev;
while(ptr!=NULL){
prev = ptr;
ptr=ptr->next;
delete prev;
}
head = NULL;
cout<<"\n\nLIST DELETED SUCCESSFULLY !\n\n";
}
//swap_nodes_without_swapping_values function
void llist::swap_nodes_without_swapping_values(int val1,int val2){
Node* ptr = head;
Node* prev,*curr1,*curr1prev,*curr2,*curr2prev;
int counter =0;
while(ptr!=NULL){
counter++;
ptr=ptr->next;
}
if(head==NULL){
cout<<"\n\nEMPTY LIST !\n\n";
return;
}
int val1c = searchele(val1);
int val2c = searchele(val2);
if(val1c==0||val2c==0){
cout<<"\n\nONE OF THE TWO ELEMENTS NOT IN THE LIST, LIST REMAINS UNCHANGED !\n\n";
return;
}
ptr=head;
prev = NULL;
while(ptr!=NULL){
if(ptr->data==val1){
curr1 = ptr;
curr1prev = prev;
}else if(ptr->data==val2){
curr2 = ptr;
curr2prev =prev;
}
prev = ptr;
ptr = ptr->next;
}
ptr=head;
if( (ptr->data==val1||ptr->data==val2)&&abs(val1c-val2c)==1 ){
head=curr2;
curr1->next = curr2->next;
curr2->next = curr1;
cout<<"\n\nMODIFICATIONS SUCCESSFUL !\n\n";
}else if((ptr->data==val1||ptr->data==val2)){
head = curr2;
curr2prev->next=curr2->next;
curr2->next= curr1->next;
curr1->next=curr2prev->next;
curr2prev->next=curr1;
cout<<"\n\nMODIFICATIONS SUCCESSFUL !\n\n";
}else if(abs(val1c-val2c)==1){
//for adjacent nodes !
ptr =head;
prev = head;
for(int i=1;i<=counter;i++){
if(i==val1c||i==val2c){
curr1prev->next=curr2;
curr2prev->next=curr2->next;
curr2->next = curr1;
cout<<"\n\nMODIFICATIONS SUCCESSFUL !\n\n";
break;
}
prev=ptr;
ptr=ptr->next;
}
}else{
//for non adjacent nodes !
ptr =head;
prev = head;
for(int i=1;i<=counter;i++){
if(i==val1c||i==val2c){
curr1prev->next=curr2;
curr2prev->next=curr2->next;
curr2->next=curr1->next;
curr1->next=curr2prev->next;
curr2prev->next=curr1;
cout<<"\n\nMODIFICATIONS SUCCESSFUL !\n\n";
break;
}
prev=ptr;
ptr=ptr->next;
}
}
}
//HOPE YOU LIKED IT :)