forked from ialbluwi/psut-data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dll_comments.h
420 lines (335 loc) · 10.2 KB
/
dll_comments.h
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
#pragma once
#include <iostream>
using std::string;
using std::ostream;
template <class T>
class DLList;
/*
A Node in a doubly-linked list.
Every node has a value and a pointer to the next and previous nodes.
+--+-------+--+
| | | |
<--|-*| Value |*-|-->
| | | |
+--+----+--+--+
*/
template <class T>
class DLLNode {
public:
DLLNode(const T& val, DLLNode* next, DLLNode* prev);
T get_val() const { return val; }
DLLNode* get_next() const { return next; }
DLLNode* get_prev() const { return prev; }
private:
T val;
DLLNode* next;
DLLNode* prev;
friend class DLList<T>;
};
template <class T>
DLLNode<T>::DLLNode(const T& val, DLLNode* next, DLLNode* prev)
{
this->val = val;
this->next = next;
this->prev = prev;
}
/*
A Doubly-Linked List is a group of nodes connected to each other.
- Each node is connected to the next and previous nodes in the list.
- The previous pointer of the head node is a null pointer.
- The next pointer of the tail node is a null pointer.
- The rest of the properties are similar to the properties of the
Singly-Linked List.
+--+-------+--+ +--+-------+--+ +--+-------+--+
| | |*-|-->| | |*-|-->| | |*-|--> null
| | Value | | | | Value | | | | Value | |
null <--|-*| | |<--|-*| | |<--|-*| | |
+--+-------+--+ +--+-------+--+ +--+-------+--+
^ ^
| |
head tail
*/
template <class T>
class DLList {
public:
DLList();
DLList(const DLList& other);
~DLList();
bool is_empty() const;
DLLNode<T>* head_node() const;
DLLNode<T>* tail_node() const;
T head_val() const;
T tail_val() const;
void add_to_head(const T& val);
void add_to_tail(const T& val);
void append(const DLList& otherList);
void remove_head();
void remove_tail();
bool remove(const T& val);
void clear();
bool contains(const T& val) const;
DLList& operator=(const DLList& other);
private:
DLLNode<T>* head;
DLLNode<T>* tail;
};
template <class T>
DLList<T>::DLList()
{
head = nullptr;
tail = nullptr;
}
// Copy constructor. Performs a deep copy of the other list.
template <class T>
DLList<T>::DLList(const DLList<T>& other) {
head = nullptr;
tail = nullptr;
// function append adds each value of
// "other" to the tail of this list
append(other);
}
// Destructor: Deletes all the nodes in the list.
// Delegates the work to function clear().
// --- Asymptotic complexity: O(n).
template <class T>
DLList<T>::~DLList()
{
clear();
}
// Deletes all the nodes in the list.
// --- Asymptotic complexity: O(n).
template <class T>
void DLList<T>::clear()
{
DLLNode<T>* tmp = head;
DLLNode<T>* del_node;
while (tmp != nullptr) {
del_node = tmp;
tmp = tmp->next;
delete del_node;
}
head = nullptr;
tail = nullptr;
}
// Checks if the list is empty.
template <class T>
bool DLList<T>::is_empty() const
{
return head == nullptr;
}
template <class T>
DLLNode<T>* DLList<T>::head_node() const
{
return head;
}
template <class T>
DLLNode<T>* DLList<T>::tail_node() const
{
return tail;
}
template <class T>
T DLList<T>::head_val() const
{
if (is_empty())
throw string("ERROR: Attempting to retrieve a value of the head from an empty list.");
return head->val;
}
template <class T>
T DLList<T>::tail_val() const
{
if (is_empty())
throw string("ERROR: Attempting to retrieve a value of the tail from an empty list.");
return tail->val;
}
// Adds a node to the beginning of the list.
// --- Asymptotic complexity: O(1)
template <class T>
void DLList<T>::add_to_head(const T& val)
{
// The next of the new node is the old head, and the previous is nullptr
DLLNode<T>* new_node = new DLLNode<T>(val, head, nullptr);
// If there is a head in the list, then its previous must
// point at the newly crated node
if (head != nullptr)
head->prev = new_node;
// The head must become the new node.
// The tail changes only if it is the first node added to the list
head = new_node;
if (tail == nullptr)
tail = new_node;
}
// Adds a node to the end of the list.
// --- Asymptotic complexity: O(1)
template <class T>
void DLList<T>::add_to_tail(const T& val)
{
// The next of the new tail is nullptr, and the previous is the old tail
DLLNode<T>* new_node = new DLLNode<T>(val, nullptr, tail);
// If there is a tail already in the list, then its next
// must point at the newly created node
if (tail != nullptr)
tail->next = new_node;
// The tail pointer must always move to point at the
// newly created node. The head changes only if it is
// the first node added to the list.
tail = new_node;
if (head == nullptr)
head = new_node;
}
// Deletes the head node.
// --- Asymptotic complexity: O(1)
template <class T>
void DLList<T>::remove_head()
{
if (is_empty())
return;
// save a pointer to the current head
// and move the head forward.
DLLNode<T>* del_node = head;
head = del_node->next;
// if the head is now nullptr, it means that
// there is only one node in the list. Hence,
// the tail must also become nullptr.
if (head == nullptr)
tail = nullptr;
else
head->prev = nullptr;
// if the head is not nullptr, its prev
// must be set to nullptr
delete del_node;
}
// Deletes the tail node.
// --- Asymptotic complexity: O(1)
// --- Note that this operation was O(n) in the singly-linked list.
// --- The prev pointer allows moving the tail easily one step backward, which
// --- was not possible in the SLL without traversing the list up to the node
// --- preceding the tail.
template <class T>
void DLList<T>::remove_tail()
{
if (is_empty())
return;
// save a pointer to the current tail
// and move the tail pointer backwards
DLLNode<T>* del_node = tail;
tail = tail->prev;
// if the tail is now nullptr, this means that
// there is only one node in the list. Hence,
// the head must also point at nullptr
if (tail == nullptr)
head = nullptr;
else
tail->next = nullptr;
// if the tail is not nullptr, its next
// must be set to nullptr
delete del_node;
}
// Removes one occurrence of "val" from the list and returns true.
// Returns false if val is not in the list.
// * If the list is empty, there is nothing to do.
// * If "val" is at the head or the tail, then use remove_head() or
// remove_tail().
// * If "val" is not at the head or the tail, then search for val.
// If val is found, we need to delete the node and link the node before it
// to the node after it. Due to the presence of the prev pointer, there is
// no need to move a pred pointer as was the case in the SLL.
//
//
// +--------------------------+
// | |
// | x V
// +--+-------+--+ +--+-------+--+ +--+-------+--+
// | | |*-|-X->| | |*-|--->| | |*-|-->
// .... | | val | | | | val | | | | val | | ....
// <--|-*| | |<---|-*| | |<-X-|-*| | |
// +--+-------+--+ +--+-------+--+ +--+-------+--+
// ^ ^ ^ | ^
// | | | | |
// pred | node | succ
// +---------------------------+
//
// --- Asymptotic complexity:
// * Best Case: O(1) If the value is at the head or the tail.
// * Worst Case: O(n) If the value is at the node before the tail or if
// the value is not in the list.
template <class T>
bool DLList<T>::remove(const T& val)
{
if (is_empty())
return false;
if (val == head->val) {
remove_head();
return true;
}
if (val == tail->val) {
remove_tail();
return true;
}
// traverse list to find the node to be deleted
for (DLLNode<T>* curr = head->next; curr != nullptr; curr = curr->next) {
if (curr->val == val) {
DLLNode<T>* pred = curr->prev;
DLLNode<T>* succ = curr->next;
pred->next = succ;
succ->prev = pred;
delete curr;
return true;
}
}
// not found
return false;
}
// Searches for a value in the list.
// --- Asymptotic complexity: O(n).
template <class T>
bool DLList<T>::contains(const T& val) const
{
DLLNode<T>* curr;
for (curr = head; curr != nullptr; curr = curr->next) {
if (curr->val == val)
return true;
}
return false;
}
// Adds all of the values in the nodes of "other" to
// the tail of the current list. The other list should stay intact and the two
// lists should remain separate lists.
// --- Asymptotic complexity: O(n), where n is the number
// --- of elements in the other list.
template <class T>
void DLList<T>::append(const DLList& other)
{
if (this == &other)
throw string("ERROR: Can't append the list to itself");
// THINK: Why not?
// How can we handle this case?
DLLNode<T>* curr;
for (curr = other.head; curr != nullptr; curr = curr->next)
add_to_tail(curr->val);
}
// copy assignment
// This function is important to avoid shallow copying when using the
// assignment operator
template <class T>
DLList<T>& DLList<T>::operator=(const DLList<T>& other)
{
// Guard against self assignment
if (this == &other)
return *this;
clear();
append(other);
return *this;
}
template<class T>
ostream& operator<<(ostream& out, const DLList<T>& list) {
out << "[";
DLLNode<T>* curr = list.head_node();
while (curr != nullptr) {
out << curr->get_val();
if (curr->get_next() != nullptr)
out << ", ";
curr = curr->get_next();
}
out << "]";
return out;
}