forked from aryansharma23124/Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinked_list_basic.cpp
623 lines (540 loc) · 10.8 KB
/
linked_list_basic.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
#include<bits/stdc++.h>
using namespace std;
struct Node{
int data;
char flag;
struct Node *next;
};
void printlist(Node *n){
while(n!=NULL){
cout<<n->data;
n=n->next;
}
}
void insert_head(Node **head,int data_){
Node *insert_head=new Node;
insert_head->data=data_;
insert_head->next=*head;
*head=insert_head;
}
void insert_after_given_node(Node *given,int data){
Node *new_middle=new Node;
new_middle->data=data;
new_middle->next=given->next;
given->next=new_middle;
}
void insert_end(Node **head,int data){
Node *last_new=new Node;
last_new->data=data;
last_new->next=NULL;
//*end=last_new;
Node *temp=*head;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=last_new;
}
void delete_a_node(Node **head,int data_){
Node *temp=*head,*temp2;
if(temp->data=data_){
*head=temp->next;
}
else{
while(temp->data!=data_){
temp2=temp;
temp=temp->next;
}
temp2->next=temp->next;
}
}
void delte_particular_pos(Node **head,int pos){
Node *temp=*head,*temp2;
int counter=0;
if(pos==0){
*head=temp->next;
}
else{
while(counter!=pos){
temp2=temp;
temp=temp->next;
counter++;
}
temp2->next=temp->next;
free(temp);
}
}
void delete_a_linked_list(Node **head){
Node *temp=*head,*temp2;
while(temp->next!=NULL){
temp2=temp->next;
free(temp);
temp=temp2;
}
}
void search_element_iterative(Node **head,int data_){
Node *temp=*head,*temp2;
while(temp->data!=data_){
temp2=temp;
temp=temp->next;
}
cout<<temp2->next<<endl;
cout<<temp->data<<endl;
}
void search_element_recursive(Node **head,int data_){
Node *temp=*head,*temp2;
int data2=data_;
if(temp->data==data_){
cout<<temp->data<<endl;
}
else{
temp=temp->next;
search_element_recursive(&temp,data2);
}
}
void count_iterative(Node **head){
int counter=0;
Node *temp=*head;
while(temp!=NULL){
temp=temp->next;
counter++;
}
cout<<counter<<endl;
}
int count_recursive(Node *head,int co){
int c2=co+1;
if(head==NULL){
return 0;
}
else if(head->next!=NULL){
head=head->next;
count_recursive(head,c2);
}
else{
cout<<c2;
}
}
int count_recursive_gfg(Node *head){
if(head==NULL){
return 0;
}
else{
return 1+count_recursive_gfg(head->next);
}
}
int search_element_recursive_gfg(Node *head,int data_){
if(head==NULL){
return 0;
}
if(head->data==data_){
return 1;
}
return search_element_recursive_gfg(head->next,data_);
}
//get nth node iteratively
int nth_Node(Node *head,int counter){
int count=0;
while(count<counter){
count++;
head=head->next;
}
cout<<head->data;
}
//get nth node recursively
int get_nth_node_recursive(Node *head,int index){
int count=1;
if(count==index){
return head->data;
}
return (get_nth_node_recursive(head->next,index-1));
}
int get_nth_node_behind(Node *head,int index){
//count the linked list;
int count=0;
Node *temp=head;
while(temp!=NULL){
count++;
temp=temp->next;
}
Node *temp2=head;
int find=count-index;
while(find>0){
temp2=temp2->next;
find--;
}
cout<<temp2->data;
}
int get_nth_node_behind_gfg(Node *head,int index){
Node *temp=head,*temp2=head;
while(index>1){
temp=temp->next;
index--;
}
while(temp->next!=NULL){
temp2=temp2->next;
temp=temp->next;
}
cout<<temp2->data;
}
int get_middle_node(Node *head){
Node *temp=head,*temp2=head;
while(temp->next!=NULL){
temp=temp->next;
if(temp->next!=NULL){
temp=temp->next;
temp2=temp2->next;
}
else{
break;
}
}cout<<temp2->data;
}
int detect_loop_linked_list(Node *head){
Node *temp=head,*temp2=head;
while(temp->next!=NULL){
temp=temp->next;temp=temp->next;
temp2=temp2->next;
if(temp==temp2){
return 1;
}
else{
continue;
}
}
return 0;
}
int find_length_loop_linked_list(Node *head){
Node *temp=head,*temp2=head;
while(temp->next!=NULL){
temp=temp->next;
temp=temp->next;
temp2=temp2->next;
if(temp==temp2){
int count=1;
temp2=temp2->next;
while(temp!=temp2){
count++;
temp2=temp2->next;
}
cout<<count;
break;
}
else{
continue;
}
}
}
int reverse_linked_list(Node **head){
Node *temp=*head,*temp2=*head;
*head=NULL;
while(temp!=NULL){
insert_head(head,temp->data);
temp=temp->next;
}
temp2->next=NULL;
}
int reverse_linked_list_between_two_nodes(Node *head,int index1,int index2){
Node *temp1=head,*temp2=head,*prev=head;
int counter1=0,counter2=0;
while(counter1<index1){
temp1=temp1->next;
counter1++;
if(counter1<index1){
prev=prev->next;
}
}
while(counter2<index2){
temp2=temp2->next;
counter2++;
}
int total=index2-index1;
int c=0;
while(c<total){
insert_after_given_node(temp2,temp1->data);
temp1=temp1->next;
c++;
}
prev->next=temp2;
}
int check_palindrome(Node *head){
Node *temp1=head,*middle=head;int index1=0,index2=0;
while(temp1->next!=NULL){
temp1=temp1->next;
if(temp1->next!=NULL){
temp1=temp1->next;
index1+=2;index2+=1;
middle=middle->next;
}
else{
index1+=1;
break;
}
}
//cout<<index1<<index2;
reverse_linked_list_between_two_nodes(head,index2+1,index1);
temp1=head;
//cout<<middle->data;
while(middle->next!=NULL){
middle=middle->next;
//cout<<middle->data<<temp1->data;
if(temp1->data==middle->data){
//continue;
//cout<<"hi";
int a=0;
}
else{
//cout<<"o";
return 0;
}
//middle=middle->next;
temp1=temp1->next;
}
return 1;
}
void removeDuplicates(struct Node* head)
{
/* Pointer to traverse the linked list */
struct Node* current = head;
/* Pointer to store the next pointer of a node to be deleted*/
struct Node* next_next;
/* do nothing if the list is empty */
if (current == NULL)
return;
/* Traverse the list till last node */
while (current->next != NULL)
{
/* Compare current node with next node */
if (current->data == current->next->data)
{
/* The sequence of steps is important*/
next_next = current->next->next;
free(current->next);
current->next = next_next;
}
else /* This is tricky: only advance if no deletion */
{
current = current->next;
}
}
}
void remove_duplicates_unsorted(Node *head){
Node *temp=head,*temp1=head,*temp2=head,*temp3=head;
while(temp->next!=NULL){
temp2=temp;
while(temp2->next!=NULL){
if(temp->data==temp2->next->data){
//cout<<"A"<<temp2->data;
temp3=temp2->next;
//temp2=temp2->next;
temp2->next=temp2->next->next;
delete temp3;
//temp2=temp1;
}
else{
temp2=temp2->next;
}
}
if(temp->next==NULL){
temp->next=NULL;
}
else{
temp=temp->next;
//cout<<temp->data<<endl;
}
}
}
int remove_loop_linked_list(Node *head){
Node *temp=head,*temp2=head,*temp3=head,*temp4=head;
while(1){
temp=temp->next;temp=temp->next;
temp2=temp2->next;
if(temp==temp2){
break;
}
}
while(1){
temp3=temp2;
while(temp3->next!=temp2 and temp3->next!=temp4){
temp3=temp3->next;
}
if(temp3->next==temp4){
break;
}
temp4=temp4->next;
}
temp3->next=NULL;
// while(temp3->next!=temp4){
// temp3=temp3->next;
// }
// temp3->next=NULL;
}
// while(temp->next!=NULL){
// if(temp2!=temp){
// temp=temp->next;temp=temp->next;
// temp2=temp2->next;
// }
// else{
// temp3=temp2;
// while(temp3->next!=temp4){
// temp3=temp3->next;
// }
// temp3->next=NULL;
// }
// }
int delete_both_duplicates(Node **head){
Node *temp1=*head,*temp2=*head,*temp3=*head,*temp4=*head,*temp5=*head,*temp6=*head,*temp7=*head,*temp8=*head;
while(temp1->next!=NULL){
temp2=temp1;
temp3=temp1;
int flag=0;
while(temp3->next!=NULL){
temp4=temp3->next;
if(temp4->data==temp2->data){
temp3->next=temp3->next->next;
flag=1;
free(temp4);
temp3=temp2;
//cout<<temp2->data;
//free(temp2);
}
else
temp3=temp3->next;
//cout<<"A";
}
if(flag==1){
// temp5=temp1;
// temp1=temp1->next;
// free(temp5);
// cout<<"A";
//temp1=temp2;
if(temp1!=*head and temp1->next!=NULL){
while(temp5->next!=temp1 and temp5->next!=NULL and temp1->next!=NULL){
//temp7=temp5;
temp5=temp5->next;
}
temp6=temp1;
temp5->next=temp1->next;
free(temp6);
temp1=temp1->next;
}
else if(temp1->next==NULL and temp1==*head){
*head=NULL;
free(temp1);
//ree(temp1);
break;
}
else if(temp1->next==NULL){
while(temp8->next!=temp1){
temp8=temp8->next;
}
temp8->next=NULL;
temp1=temp8;
}
else{
temp7=temp1;
*head=temp1->next;
//temp1=temp1->next;
//free(temp7);
}
}
else{
temp1=temp1->next;
}
//cout<<"A";
}
}
int merge_two_linked_list(Node **head,Node **head2){
Node *temp1=*head,*temp2=*head2;
while(temp1->next!=NULL){
temp1=temp1->next;
}
temp1->next=temp2;
}
int find_intersection_between_linked_list(Node *head,Node *head2){
Node *temp1=head,*temp2=head2;
while(temp1->next!=NULL){
temp1->flag='v';
temp1=temp1->next;
}
int data;
while(temp2->next!=NULL){
if(temp2->flag=='v'){
data=temp2->data;
break;
}
else{
temp2->flag='v';
temp2=temp2->next;
}
}
cout<<data<<endl;
}
int main(){
Node *head=new Node;
Node *second=new Node;
Node *third=new Node;
Node *fourth=new Node;
Node *five=new Node;
Node *six=new Node;
Node *seven=new Node;
Node *final=new Node;
//Node *n_head=new Node;
head->data=1;
head->flag='a';
head->next=second;
second->data=1;
second->flag='a';
second->next=third;
third->data=2;
third->flag='a';
third->next=fourth;
fourth->data=2;
fourth->flag='a';
fourth->next=five;
five->data=3;
five->flag='a';
five->next=six;
six->data=3;
six->flag='a';
six->next=seven;
seven->data=6;
seven->flag='a';
seven->next=final;
final->data=6;
final->flag='a';
final->next=NULL;
//insert_after_given_node(second,7);
//insert_end(&head,8);
//delete_a_node(&head,1);
//delte_particular_pos(&head,0);
//delete_a_linked_list(&head);
//search_element_iterative(&head,3);
//search_element_recursive(&head,3);
//count_iterative(&head);
//count_recursive(head,0);
// int count_rec_gfg=count_recursive_gfg(head);
// cout<<count_rec_gfg;
// cout<<endl;
// int search_element_recursive_var_gfg=search_element_recursive_gfg(head,3);
// cout<<search_element_recursive_var_gfg<<endl;
//nth_Node(head,1);
//get_nth_node_behind_gfg(head,2);
//get_middle_node(head);
// int detect =detect_loop_linked_list(head);
// cout<<detect;
//cout<<
//find_length_loop_linked_list(head);
//reverse_linked_list(&head);
//reverse_linked_list_between_two_nodes(head,3,5);
//cout<<check_palindrome(head);
//cout<<endl;
//removeDuplicates(head);
//remove_duplicates_unsorted(head);
//remove_loop_linked_list(head);
delete_both_duplicates(&head);
//merge_two_linked_list(&head,&five);
//find_intersection_between_linked_list(head,five);
printlist(head);
//cout<<final->data;
//b = *a;
//printlist(head);
}