-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedList.cpp
468 lines (415 loc) · 11.5 KB
/
linkedList.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
#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int data;
Node* next;
Node(int data)
{
this->data = data;
this->next = nullptr;
}
};
class LinkedList
{
private:
Node* head;
public:
LinkedList()
{
head = nullptr;
}
// Function to insert a new node at the end of the list
void insert(int data)
{
Node* newNode = new Node(data);
if(head == nullptr){
head=newNode; }
else {
Node* temp=head;
while(temp->next != nullptr)
{
temp=temp->next;
}
temp->next=newNode;
newNode->next=nullptr;
}
}
// Function to display the linked list
void display()
{
Node* current=head;
while(current!=nullptr)
{
cout<<current->data<<" -> ";
current=current->next;
}
cout<<"null"<<endl;
}
// Count the size
int listLength()
{
Node* current=head;
int size=0;
if(current==nullptr)
{
cout<<size<<endl;
}
while(current!=nullptr)
{
size++;
current=current->next;
}
return size;
}
//Function to REVERSE a list
Node* reverseList()
{
Node* prev=nullptr;
Node* current=head;
while(current!=nullptr)
{
Node* fwd=current->next;
current->next=prev;
prev=current;
current=fwd;
}
head=prev;
return head;
}
// function to insert a node at the given position
Node* insertAtPosition(int i,int data)
{
Node* temp=head;
int j=1;
while(j<i-1)
{
temp=temp->next;
j++;
}
Node* newNode=new Node(data);
newNode->next=temp->next;
temp->next=newNode;
return head;
}
// function to DELETE the LAST NODE
Node* deleteLastNode()
{
Node* prev=head;
Node* current=prev->next;
while(current->next!=nullptr)
{
prev=current;
current=current->next;
}
prev->next=nullptr;
return head;
}
// function to DELETE the specified element
Node* deleteNodeElement(int x)
{
int pos=positionToBeDeleted(x);
int q=1;
Node* temp=head;
while(q<pos-1)
{
temp=temp->next;
}
temp->next=temp->next->next;
return head;
}
int positionToBeDeleted(int x)
{
int i=0;
Node* temp=head;
while(temp->next!=nullptr)
{
if(temp->data==x)
temp=temp->next;
i++;
}
return i;
}
// function to reverse in pairs
Node* reverseInPairs()
{
Node* dummy = new Node(0);
dummy->next=head;
Node* point=dummy;
while(point->next!=nullptr && point->next->next!=nullptr)
{
Node* a=point->next;
Node* b=point->next->next;
// perform swaps
a->next=b->next;
b->next=a;
// for further pairs
point->next=b;
point=a;
}
head=dummy->next;
return head;
}
// Reverse LL in Blocks of k nodes
/*
Node* ReverseBlockOfKNodes(int k)
{
Q
}*/
Node* getKPlusOneThNode(int k)
{
Node* KthNode;
int i=0;
if(!head)
{
return head;
}
for(i=0,KthNode=head;KthNode!=nullptr && (i<k);i++,KthNode=KthNode->next);
if(i==k && KthNode!=nullptr)
{
return KthNode;
}
return head->next;
}
int hasKNodes(int k)
{
int i=0;
Node* current=head;
for(i=0; current && (i<k);i++,current=current->next);
if(i==k)
return 1;
return 0;
}
// Find Nth node from the end
Node* NthElementFromEnd(int n)
{
Node* behind=head;
Node* ahead=head;
for(int i=0;i<n;i++)
{
ahead=ahead->next;
}
while(ahead!=nullptr)
{
behind=behind->next;
ahead=ahead->next;
}
return behind;
}
// Delete the Nth node from the end
Node* DeleteNthFromTheEnd(int n)
{
// the Nth node from the end is (length-N+1)th element from the start
// so, generalizing it to remove Kth element from the begininning, assume k = length-N+1
Node* temp=head;
int length=0;
while(temp!=nullptr)
{
temp=temp->next;
length++;
}
int k=length-n+1;
int i=1;
temp=head;
while(i<k-1)
{
temp=temp->next;
i++;
}
temp->next=temp->next->next;
return head;
}
// PALINDROME check : TIME: O(n) | SPACE: O(n)
bool checkForPalindromeApproach1()
{
Node* temp=head;
vector<int> v;
while(temp!=nullptr)
{
v.push_back(temp->data);
temp=temp->next;
}
int i=0;
int j=v.size()-1;
while(i<j && v.at(i)==v.at(j))
{
i++;
j--;
}
return i>=j;
}
// PALINDROME check : TIME: O(n) | SPACE: O(1) ||| also includes reversing the 2nd part of a LL
bool checkForPalindromeApproach2()
{
return 1;
}
Node* rotateLinkedList(int k)
{
int i=1;
int n=1;
Node* temp=head;
while(temp->next!=nullptr)
{
temp=temp->next;
n++;
}
temp->next=head;
Node* ptr=head;
//temp=head;
while(i<n-k)
{
ptr=ptr->next;
i++;
}
head=ptr->next;
ptr->next=nullptr;
return head;
}
// All even numbers appear before all the Odd numbers in the modified LL
Node* exchangeEvenOddList()
{
Node* oddList=nullptr;
Node* evenList=nullptr;
Node* oddListEnd=nullptr;
Node* evenListEnd=nullptr;
Node* itr=head;
while(itr!=nullptr)
{
if(itr->data%2==0)
{
if(evenList==nullptr)
{
evenList=evenListEnd=itr;
}
else
{
evenListEnd->next=itr;
evenListEnd=itr;
}
}
else
{
if(oddList==nullptr)
{
oddList=oddListEnd=itr;
}
else
{
oddListEnd->next=itr;
oddListEnd=itr;
}
}
itr=itr->next;
}
evenListEnd->next=oddList;
return head;
}
};
int main()
{
LinkedList list;
int choice;
while(1){
cout<<"MENU:-"<<endl<<"1. Insert an Element\n"<<"2. Display current list\n"<<"3. Current List length\n"<<"4. Reverse list\n"<<"5. Insert at position k\n"<<"6. Delete the last node\n"<<"7. Delete a node with given DATA\n"<<"8. Reverse in pairs\n"<<"9. Nth element from the end\n"<<"10. Remove Nth from the end\n"<<"11. Check if a linked list is Palindrome\n"<<"12. Rotate linked list by k elements\n"<<"13. Separate all even numbers to be before odd numbers\n"<<"14. ";
cout<<"Enter choice no.: ";
cin>>choice;
switch(choice)
{
case 1: //insert end
{int x;
cout<<"\nEnter element: ";
cin>>x;
list.insert(x);
break;}
case 2: //display
{cout<<"\nLinked List: ";
list.display();
break;}
case 3: //length
{int length=list.listLength();
cout<<"\nCurrent length: "<<length<<endl;
break;}
case 4: //reverse
{list.reverseList();
cout<<"\nReversed list: ";
list.display();
break;}
case 5: //insert at position k
{int x,k;
cout<<"Enter the position: ";
cin>>k;
cout<<"\nEnter element: ";
cin>>x;
list.insertAtPosition(k,x);
cout<<"\nNew list: ";
list.display();
break;}
case 6: // delete the last node
{list.deleteLastNode();
cout<<"\nNew list: ";
list.display();
break;}
case 7: // delete the node with specified element
{
int d;
cout<<"Enter element to be deleted: ";
cin>>d;
list.deleteNodeElement(d);
cout<<"\nNew list: ";
list.display();
break;
}
case 8: // reverse in pairs
{
list.reverseInPairs();
list.display();
break;
}
case 9: // Nth element from the end
{
int n;
cout<<"Enter N: ";
cin>>n;
Node* NthFromEnd = list.NthElementFromEnd(n);
cout<<NthFromEnd->data<<endl;
break;
}
case 10: // Remove the Nth element from the end
{
int n;
cout<<"Enter N: ";
cin>>n;
list.DeleteNthFromTheEnd(n);
list.display();
break;
}
case 11:
{
bool palindrome=list.checkForPalindromeApproach1();
if(palindrome)
cout<<"The given list is a PALINDROME !";
break;
}
case 12:
{
int k;
cout<<"Enter k: ";
cin>>k;
list.rotateLinkedList(k);
list.display();
break;
}
case 13:
{
list.exchangeEvenOddList();
list.display();
break;
}
default:
{cout<<"Invalid choice !"<<endl;
break;}
}
} //while ends here
return 0;
}