forked from ravikartar/hacktoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Doubly_Linked_List.cpp
158 lines (145 loc) · 2.79 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
#include <iostream>
using namespace std;
class Node {
public:
int val;
Node *next, *prev;
Node(int val) {
this->val = val;
next = prev = NULL;
}
};
class Solution {
private:
Node *head, *tail;
public:
Solution(): head(NULL), tail(NULL) {}
private:
Node *create(int val, Node *head) {
Node *newNode = new Node(val);
if (head == NULL) {
head = newNode;
this->head = head;
}
else {
head->next = newNode;
newNode->prev = head;
head = head->next;
}
return head;
}
void insertAtHead(int val, Node *head) {
Node *newNode = new Node(val);
if (head == NULL) {
this->head = newNode;
return;
}
newNode->next = head;
head->prev = newNode;
this->head = newNode;
}
void insertAtTail(int val, Node *head) {
if (head == NULL) {
insertAtHead(val, head);
return;
}
Node *newNode = new Node(val);
while (head->next != NULL) {
head = head->next;
}
head->next = newNode;
newNode->prev = head;
}
void insertAtKPosition(int val, int k, Node *head) {
if (k == 1) {
insertAtHead(val, head);
return;
}
Node *newNode = new Node(val);
Node *pre = NULL;
while (head) {
if (k == 1) {
pre->next = newNode;
newNode->prev = pre;
newNode->next = head;
head->prev = newNode;
return;
}
k--;
pre = head;
head = head->next;
}
pre->next = newNode;
newNode->prev = pre;
}
void printNext(Node *head) {
Node *temp = head;
while (temp != NULL) {
cout << temp->val << ' ';
temp = temp->next;
}
cout << '\n';
}
void printPrev(Node *tail) {
while (tail->next) tail = tail->next;
Node *temp = tail;
while (temp) {
cout << temp->val << ' ';
temp = temp->prev;
}
cout << '\n';
}
Node *reverseIterative(Node *head) {
Node *pre = NULL, *curr = head, *nxt = head;
while (curr != NULL) {
nxt = curr->next;
curr->next = pre;
if (pre != nullptr)
pre->prev = curr;
pre = curr;
curr = nxt;
}
return pre;
}
Node *reverseRecursive(Node *head) {
if (head == NULL || head->next == NULL) return head;
Node *restNode = reverseRecursive(head->next);
auto nxt = head->next;
nxt->next = head;
head->prev = nxt;
head->next = nullptr;
restNode->prev = nullptr;
return restNode;
}
public:
void create(int val) {
tail = create(val, tail);
// insertAtHead(1, head);
// insertAtHead(2, head);
// insertAtHead(3, head);
// insertAtTail(4, head);
// insertAtKPosition(8, 2, head);
// insertAtKPosition(11, 2, head);
// insertAtKPosition(111, 2, head);
}
void reverse() {
// head = reverseIterative(head);
head = reverseRecursive(head);
}
void print() {
// printNext(head);
printPrev(head);
}
};
int main() {
int n; cin >> n;
Solution ob;
for (int i = 0; i < n; ++i) {
int val; cin >> val;
ob.create(val);
}
ob.print();
ob.reverse();
ob.print();
return 0;
}