forked from starrohan999/Hacktoberfest-Accepted
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoubly_Linked_List.cpp
212 lines (150 loc) · 4.57 KB
/
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
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node *prev;
Node *next;
};
void addNodeAtBegin(int data, Node **Head){
Node *new_Node = new Node;
new_Node->data = data;
new_Node->prev = NULL;
new_Node->next = (*Head);
if((*Head) != NULL) (*Head)->prev = new_Node;
(*Head) = new_Node;
// Node *prev = NULL;
// Node *temp = Node *Head->next;
// node *next = *temp;
}
void addNodeAtEnd(int data, Node **Head){
Node *new_Node = new Node;
Node *last = (*Head);
new_Node->data = data;
new_Node->next = NULL;
if((*Head) == NULL){
new_Node->prev = NULL;
(*Head) = new_Node;
return;
}
while(last->next != NULL){
last = last->next;
}
last->next = new_Node;
new_Node->prev = last;
}
void addNodeAfter(int data, Node *prev){
if (prev == NULL)
{
cout<<"the given previous node cannot be NULL";
return;
}
Node *new_Node = new Node;
new_Node->data = data;
new_Node->prev = prev;
new_Node->next = (prev)->next;
(prev)->next = new_Node;
if(new_Node->next != NULL){
new_Node->next->prev = new_Node;
}
}
void printList(Node* node)
{
Node* last;
cout<<"\nTraversal in forward direction \n";
while (node != NULL)
{
cout<<" "<<node->data<<" ";
last = node;
node = node->next;
}
// cout<<"\nTraversal in reverse direction \n";
// while (last != NULL)
// {
// cout<<" "<<last->data<<" ";
// last = last->prev;
// }
}
void deleteNode(Node **Head, Node *del){
if(del == NULL || *Head == NULL) return;
if (*Head == del) *Head = del->next;
if(del->next != NULL) del->next->prev = del->prev;
if(del->prev != NULL) del->prev->next = del->next;
free(del);
}
void ReverseList(Node **Head){
// while((*Head)->next != NULL){
// *Head->next = *Head;
// }
Node *temp = NULL;
Node *current = *Head;
while(current != NULL){
temp = current->prev;
current->prev = current->next;
current->next = temp;
current = current->prev;
}
if(temp != NULL) *Head = temp->prev;
}
int findSize(Node *node)
{
int count = 0;
while (node != NULL)
{
count++;
node = node->next;
}
return count;
// cout << '\n' << "Size of Linked List: " << count << endl;
}
void swapKthNode(Node **Head, int k){
int n = findSize(*Head);
cout << '\n' << "Size of Linked List: " << n << endl;
if(k < 0 || k > n) return;
if(k == (n - k + 1)) {
printList(*Head);
return;
}
// find kth element from begining
Node *x1 = *Head;
Node *x1_prev = NULL;
for(int i = 1; i < k; i++){
x1_prev = x1;
x1 = x1->next;
}
// find kth element from last
Node *x2 = *Head;
Node *x2_prev = NULL;
for(int i = 1; i < n - k + 1; i++){
x2_prev = x2;
x2 = x2->next;
}
if(x1_prev) x1_prev->next = x2;
if(x2_prev) x2_prev->next = x1;
// swap next pointers
Node *temp = x1->next;
x1->next = x2->next;
x2->next = temp;
if(k == 1) *Head = x2;
if(k == n) *Head = x1;
cout << "Linked List after swapping nodes: " << endl;
printList(*Head);
}
int main() {
Node *head = NULL;
addNodeAtEnd(9, &head);
addNodeAtBegin(3, &head);
addNodeAtBegin(5, &head);
addNodeAtEnd(2, &head);
addNodeAfter(8, head->next);
printList(head);
// deleteNode(&head, head);
// deleteNode(&head, head->next);
// deleteNode(&head, head->next);
// printList(head);
// ReverseList(&head);
// printList(head);
// findSize(head);
swapKthNode(&head, 3);
return 0;
}