-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.h
More file actions
326 lines (267 loc) · 8.33 KB
/
node.h
File metadata and controls
326 lines (267 loc) · 8.33 KB
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
#ifndef NODE_H
#define NODE_H
#include <iostream>
using namespace std;
template <typename T> class Node {
public:
T data;
Node<T>* next;
Node<T>* prev;
// Constructor
Node(T data) {
this->data = data;
this->next = nullptr;
this->prev = nullptr;
}
void push(T data) {
Node<T>* new_node = new Node<T>(data);
new_node->next = this->next;
new_node->prev = this;
if (this->next != nullptr)
this->next->prev = new_node;
this->next = new_node;
}
void display() {
Node<T>* current = this;
while(current != nullptr) {
cout << current->data;
if (current->next != nullptr)
cout << "<->";
current = current->next;
}
cout << endl;
}
};
// Function templates
/* Series of nodes connected by two-way pointers.
Operations:
- insertion, deletion at beginning/end/specific position
- traversal/reverse */
/* Insertions functions */
template <typename T> void insertAtBeginning(Node<T>*& head, T data) {
// Create a new node with the given data.
Node<T>* newNode = new Node<T>(data);
// Check if the doubly linked list is empty.
if (head == nullptr) {
head = newNode;
return;
}
newNode->next = head;
head->prev = newNode;
head = newNode;
}
template <typename T> void insertAtEnd(Node<T>*& head, T data) {
Node<T>* newNode = new Node<T>(data);
if (head == nullptr) {
head = newNode;
return;
}
Node<T>* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
}
template <typename T> void insertAtPosition(Node<T>*& head, T data, int position) {
if (position < 1) {
cout << "Invalid position (< 1)." << endl;
return;
}
// Position 1 is the head
if (position == 1) {
insertAtBeginning(head, data);
return;
}
// Create a new node
Node<T>* newNode = new Node<T>(data);
Node<T>* temp = head;
for (int i = 1; i < position - 1 && temp != nullptr; i++) {
temp = temp->next;
}
if (temp == nullptr) {
cout << "Position greater than number of nodes." << endl;
return;
}
// Update the next and previous pointers to insert the new node at the specified position.
newNode->next = temp->next;
newNode->prev = temp;
if (temp->next != nullptr) {
temp->next->prev = newNode;
}
temp->next = newNode;
}
/* Deletion functions */
template <typename T> void deleteFirst(Node<T>*& head) {
// Check if the doubly linked list is empty
if (head == nullptr) {
cout << "The list is already empty." << endl;
return;
}
// Update the head pointer and delete the first node
Node<T>* temp = head;
head = head->next;
if (head != nullptr) {
head->prev = nullptr;
}
delete temp;
}
template <typename T> void deleteLast(Node<T>*& head) {
// Check if the doubly linked list is empty
if (head == nullptr) {
cout << "The list is already empty." << endl;
return;
}
Node<T>* temp = head;
// If there is only one node in the list
if (temp->next == nullptr) {
head = nullptr;
delete temp;
return;
}
// Update the head pointer and delete the first node
while (temp->next != nullptr) {
temp = temp->next;
}
/* Update the previous pointer of the second
last node and delete the last node*/
temp->prev->next = nullptr;
delete temp;
}
template <typename T> void deleteList(Node<T>*& head) {
Node<T>* temp;
while (head != nullptr) {
temp = head;
head = head->next;
if (temp != nullptr)
delete temp;
}
}
/* Print functions */
template <typename T> void printListForward(Node<T>*& head) {
Node<T>* temp = head;
cout << "Forward List: ";
while (temp != nullptr) {
cout << temp->data;
if (temp->next != nullptr) {
cout << " <-> ";
}
temp = temp->next;
}
cout << endl;
}
template <typename T> void printListBackward(Node<T>*& head) {
Node<T>* temp = head;
cout << "Backward List: ";
while (temp->next != nullptr) {
temp = temp->next;
}
while (temp != nullptr) {
cout << temp->data;
if (temp->prev != nullptr) {
cout << " <-> ";
}
temp = temp->prev;
}
cout << endl;
}
/* Arithmetic functions */
template <typename T> int size(Node<T>*& head) {
Node<T>* temp = head;
int count = 0;
while (temp->next != nullptr) {
temp = temp->next;
count++;
}
return count;
}
/* Data transfer functions */
/* These functions will first traverse the list to the beginning
position and copy x nodes where x is the legnth */
template <typename T> Node<T>* copyPaste(Node<T>* head, int beginning = 1) {
if (head == nullptr) {
cout << "This list is empty." << endl;
return nullptr;
}
if (beginning < 1) {
cout << "Position outside of list" << endl;
return nullptr;
}
// Move to beginning position
Node<T>* temp = head;
for (int i = 1; i < beginning && temp != nullptr; i++) {
temp = temp->next;
}
if (temp == nullptr) {
cout << "Position is outside of list" << endl;
}
Node<T>* copy = new Node(temp->data);
Node<T>* nextNode = temp->next;
Node<T>* newTemp = copy;
while (nextNode != nullptr) {
Node<T>* newNode = new Node(nextNode->data);
newTemp->next = newNode;
newNode->prev = newTemp;
newTemp = newNode;
nextNode = nextNode->next;
}
return copy;
}
template <typename T> Node<T>* copyPaste(Node<T>* head, int beginning, int length) {
if (head == nullptr) {
cout << "This list is empty." << endl;
return nullptr;
}
if (beginning < 1) {
cout << "Position outside of list" << endl;
return nullptr;
}
if (length < 1) {
cout << "Invalid length" << endl;
return nullptr;
}
// Move to beginning position
Node<T>* temp = head;
for (int i = 1; i < beginning && temp != nullptr; i++) {
temp = temp->next;
}
if (temp == nullptr) {
cout << "Position is outside of list" << endl;
return nullptr;
}
Node<T>* copy = new Node<T>(temp->data);
Node<T>* nextNode = temp->next;
Node<T>* newTemp = copy;
int i = 1;
while (nextNode != nullptr && i < length) {
Node<T>* newNode = new Node(nextNode->data);
newTemp->next = newNode;
newNode->prev = newTemp;
newTemp = newNode;
nextNode = nextNode->next;
i++;
}
return copy;
}
template <typename T> Node<T>* cutPaste(Node<T>* head, int beginning = 1) {
Node<T>* current_node = head;
// Move to beginning position
for (int i = 1; i < beginning && current_node != nullptr; i++) {
current_node = current_node->next;
}
if (!current_node) {
cout << "Attempted to access beyond list." << endl;
return nullptr;
}
/* If there is a previous node, set the previous node's next node to nullptr
and the current node's prev node to nullpointer*/
if (current_node->prev) {
current_node->prev->next = nullptr;
current_node->prev = nullptr;
} else {
// If there is no previous node, current node is at the head
head = nullptr;
}
return current_node;
}
#endif