-
Notifications
You must be signed in to change notification settings - Fork 0
/
24.doubly linked list.cpp
417 lines (350 loc) · 9.82 KB
/
24.doubly linked list.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
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
//! doubly linked list
// in sinly ll we can only go one direction that means ones we start we can only
// forward to next Node
// in doubly link list we can go both backword and forward from a Node
//! implemenation of class for doubly linked list similar singly one just
// added one pointer back pointer which points previous Node from the current one
class Node
{
public:
// integer data
int data;
// pointer that pointing out interger data
Node *next;
// pointer for pointing the privious element
Node *back;
Node(int data1, Node *next1, Node *back1)
{
data = data1;
next = next1;
back = back1;
}
Node(int data1)
{
data = data1;
next = nullptr;
back = nullptr;
}
};
//! traversel function for linked list
void print(Node *head)
{
Node *temp = head;
while (temp != nullptr)
{
cout << temp->data << " ";
temp = temp->next;
}
}
Node *convertArr2DLL(vector<int> &arr)
{
// first element int the array is head of link list
Node *head = new Node(arr[0]);
// previous pointing to the head
Node *prev = head;
for (int i = 1; i < arr.size(); i++)
{
// creating new node with its next pointing null and back point pre
Node *temp = new Node(arr[i], nullptr, prev);
// linking the node
prev->next = temp;
// moving to next node
prev = temp;
}
return head;
}
//! deletion in doubly linked list delete head
Node *delete_head(Node *head)
{
if (head == NULL || head->next == NULL)
{
return NULL;
}
Node *prev = head;
head = head->next;
head->back = nullptr;
prev->next = nullptr;
delete prev;
return head;
}
//! delete the last element of DLL
Node *delete_Tail(Node *head)
{
if (head == NULL || head->next == NULL)
{
return NULL;
}
Node *tail = head;
while (tail->next != NULL)
{
tail = tail->next;
}
Node *newTail = tail->back;
newTail->next = nullptr;
tail->back = nullptr;
delete tail;
return head;
}
//! delete Kth element from the DLl
Node *delete_kth_node(Node *head, int k)
{
if (head == NULL)
{
return NULL;
}
// going to the kth node
int cnt = 0; // keep the track of number of node
Node *temp = head; // declaring the pointer to the head
while (temp != NULL)
{
cnt++;
if (cnt == k)
break; // if cnt and k is equal then we are in the kth position node
// moving the kthpointer to next node
temp = temp->next;
}
// pointer which point temp back node
Node *prev = temp->back;
// points temp next node
Node *front = temp->next;
// its check there is only single node in linklist which temp pointing
// null<--prev<--node-->next-->null both the pointer are pointing to null
if (prev == NULL && front == NULL)
{
return NULL;
}
// if only pre is pointing to the null that means the temp is pointing to the head node
else if (prev == NULL)
{
return delete_head(head);
}
// if only front is poiting to null that means the temp is currently in tail or last node of linklist
else if (front == NULL)
{
return delete_Tail(head);
}
// changing the pointer from temp to is next node
prev->next = front;
// changing the back pointer from the temp to its back node
front->back = prev;
// free the temp next and prev pointers
temp->next = NULL;
temp->back = NULL;
// delete the temp node
delete temp;
return head;
}
//! insertion in doubly link list
// insertion before head
Node *insertion_before_head(Node *head, int element)
{
// created a new node which next pointer is pointing to head and back will be pointing to null
Node *newHead = new Node(element, head, nullptr);
// now shift the head pointer to our new created head
head->back = newHead;
// returning the newhead
return newHead;
}
//! insertion before the Tail
Node *insert_before_tail(Node *head, int element)
{
// if the ll has only one element then its a head of ll
// so we simply call insert head of liniked list
if (head->next == NULL)
{
return insertion_before_head(head, element);
}
Node *tail = head;
while (tail->next != NULL)
{
tail = tail->next;
}
// a pointer which points tails back
Node *prev = tail->back;
// creating the new node
Node *newnode = new Node(element, tail, prev);
// prev next pointing to new node
prev->next = newnode;
// tail is last element and its back is pointing two the new node
tail->back = newnode;
return head;
}
//! insertion before the Kth element
Node *insert_before_kth(Node *head, int k, int element)
{
if (k == 1)
{
return insertion_before_head(head, element);
}
Node *temp = head;
int cnt = 0;
while (temp->next != NULL)
{
cnt++;
if (cnt == k)
break;
temp = temp->next;
}
Node *prev = temp->back;
Node *newnode = new Node(element, temp, prev);
prev->next = newnode;
temp->back = newnode;
return head;
}
void insertBeforeNode(Node *node, int val)
{
Node *prev = node->back;
Node *newnode = new Node(val, node, prev);
prev->next = newnode;
node->back = newnode;
}
//! reverse the DLL using data swap brute force
void reverse_DLL(Node *head)
{
Node *temp = head;
stack<int> st;
// storing the element fromt ll to stack
while (temp != NULL)
{
// push the ll data into stack
st.push(temp->data);
// move the temp in ll for getting the next data
temp = temp->next;
}
// rewrite the element inside the ll from the stack
// restarting the temp to head
temp = head;
while (temp != NULL)
{
// temp 1st element
temp->data = st.top();
st.pop();
temp = temp->next;
}
//! O(n) + O(n) = O(2n) TC
//! O(n) SC
}
//! reverse the DLL using link swaping
Node *reverse_DLL_linkSwap(Node *head)
{
if (head == NULL || head->next == NULL)
return head;
// Initialize a pointer to
// the previous node
Node *prev = NULL;
// Initialize a pointer to
// the current node
Node *current = head;
// Traverse the linked list
while (current != NULL)
{
// Store a reference to
// the previous node
prev = current->back;
// Swap the previous and
// next pointers
current->back = current->next;
// This step reverses the links
current->next = prev;
// Move to the next node
// in the original list
current = current->back;
}
return prev->back;
}
// find the the mid of LL
void findMiddle(Node *head)
{
// Write your code here
// Node *temp = head;
// int cnt = 0;
// while (temp != NULL)
// {
// cnt++;
// temp = temp->next;
// }
// int mid = (cnt / 2) + 1;
// reset the temp
// temp = head;
// while (temp != NULL)
// {
// mid--;
// if (mid == 0)
// break;
// temp = temp->next;
// }
// return temp;
// using the Tortoise and Hare algorithm
// How it works:
// Slow pointer (slow): Moves one step at a time.
// Fast pointer (fast): Moves two steps at a time.
// When the fast pointer reaches the end of the list, the slow pointer will be at the middle node. This method is efficient and widely used to find the middle of a linked list.
Node *slow = head;
Node *fast = head;
while (fast != NULL && fast->next != NULL)
{
slow = slow->next;
fast = fast->next->next;
}
cout << slow->data;
}
int main()
{
//! vector<int> arr = {2, 3, 4, 5};
// we are not passing the null ptr since we are already declare them if there only one value so dydefualt the pointer next back will be nullptr so we dont need to pass them again again
// Node *head = convertArr2DLL(arr);
// print(head);
// //! deleting the head of linked list
// vector<int> arr = {2, 3, 4, 5};
// Node* head = convertArr2DLL(arr);
// head = delete_head(head);
// print(head);
//! deleting the Tail of linked list
// vector<int> arr = {2, 3, 4, 5};
// Node* head = convertArr2DLL(arr);
// head = delete_Tail(head);
// print(head);
//! delete the kth node from the DLL
// vector<int> arr = {2, 3, 4, 5};
// Node* head = convertArr2DLL(arr);
// head = delete_kth_node(head,1);
// print(head);
//! insert before the head
// vector<int> arr = {2, 3, 4, 5};
// Node* head = convertArr2DLL(arr);
// head = insertion_before_head(head,1);
// print(head);
// //! insertion before the tail
// vector<int> arr = {2, 3, 4, 5};
// Node* head = convertArr2DLL(arr);
// head = insert_before_tail(head,1);
// print(head);
//! insertion before kth node
// vector<int> arr = {2, 3, 4, 5};
// Node* head = convertArr2DLL(arr);
// head = insert_before_kth(head,1,70);
// print(head);
//! insert Before Node in DLL
// vector<int> arr = {2, 3, 4, 5};
// Node* head = convertArr2DLL(arr);
// insertBeforeNode(head->next->next, 40);
// print(head);
//! reverse the linked list using the data swap
// vector<int> arr = {2, 3, 4, 5};
// Node* head = convertArr2DLL(arr);
// reverse_DLL(head);
// print(head);
//! reverse the ll using link switching
// vector<int> arr = {2, 3, 4, 5};
// Node *head = convertArr2DLL(arr);
// head = reverse_DLL_linkSwap(head);
// print(head);
//! find the mid of liked List
vector<int> arr = {2, 3, 4, 5,7,8,20};
Node *head = convertArr2DLL(arr);
findMiddle(head);
return 0;
}