-
Notifications
You must be signed in to change notification settings - Fork 0
/
doubly_linked_list.h
355 lines (288 loc) · 10 KB
/
doubly_linked_list.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
/*
* doubly_linked_list.h
*
* Created on: 13/06/2014
* Author: ranieri
*/
#ifndef DOUBLY_LINKED_LIST_H_
#define DOUBLY_LINKED_LIST_H_
#include <algorithm>
#include <initializer_list>
#include <stdexcept>
template<typename T>
class doubly_linked_list {
private:
struct node {
public:
node(node* pred, node* succ, const T& item) :
_pred(pred), _succ(succ), _item(item) {
}
node* _pred;
node* _succ;
T _item;
};
template<typename NodeT>
class iterator_base {
public:
iterator_base(node* ptr) :
_ptr(ptr) {
}
iterator_base operator=(const iterator_base& other) {
return {other._ptr};
}
iterator_base& operator++() {
if (_ptr == nullptr)
throw std::out_of_range("Iterating beyond list end.");
_ptr = _ptr->_succ;
return *this;
}
iterator_base operator++(int) {
node* old = _ptr;
++(*this);
return {old};
}
iterator_base& operator--() {
if (_ptr == nullptr)
throw std::out_of_range("Iterating beyond list begin.");
_ptr = _ptr->_pred;
return *this;
}
iterator_base operator--(int) {
node* old = _ptr;
--(*this);
return {old};
}
bool operator==(const iterator_base& other) const {
return _ptr == other._ptr;
}
bool operator!=(const iterator_base& other) const {
return _ptr != other._ptr;
}
NodeT& operator*() const {
return _ptr->_item;
}
NodeT* operator->() const {
return &(_ptr->_item);
}
private:
node* _ptr;
};
using init_list = std::initializer_list<T>;
using self = doubly_linked_list<T>;
using size_type = std::size_t;
public:
doubly_linked_list() :
_front(nullptr), _back(nullptr), _size(0) {
}
doubly_linked_list(const self& other) {
for (auto e : other)
push_back(e);
}
doubly_linked_list(self&& other) {
swap(*this, other);
}
doubly_linked_list(const init_list& items) {
for (auto e : items) {
push_back(e);
}
}
~doubly_linked_list() {
node* old;
while (_front != nullptr) {
old = _front;
_front = _front->_succ;
delete old;
}
}
T at(size_type position) const {
if (position < 0 || position >= this->_size)
throw std::out_of_range("Out of range access.");
node* p;
if (position < (_size >> 1)) {
p = _front;
for (size_type i = 0; i < position; ++i)
p = p->_succ;
} else {
p = _back;
for (size_type i = _size - 1; i > position; --i)
p = p->_pred;
}
return p->_item;
}
T back() const {
empty_check();
return _back->_item;
}
T front() const {
empty_check();
return _front->_item;
}
size_type size() const {
return this->_size;
}
/**< Removal operations */
T pop(size_type position) {
if (position < 0 || position >= _size)
throw std::out_of_range("Empty list.");
if (position == 0)
return pop_front();
if (position == _size - 1)
return pop_back();
node* p;
if (position < (_size >> 1)) {
p = _front;
for (size_type i = 0; i < position; ++i)
p = p->_succ;
} else {
p = _back;
for (size_type i = _size - 1; i > position; --i)
p = p->_pred;
}
/**< Hold node, advance it and then delete the old one */
T value = std::move(p->_item);
p->_pred->_succ = p->_succ;
p->_succ->_pred = p->_pred;
delete p;
--this->_size;
return value;
}
T pop_back() {
empty_check();
node* aux = _back;
T item = std::move(_back->_item);
_back = _back->_pred;
if (_back == nullptr) {
_front = nullptr;
} else {
_back->_succ = nullptr;
}
delete aux;
--this->_size;
return item;
}
T pop_front() {
empty_check();
/**< Hold head, advance it and then delete the old one */
node* aux = _front;
T item = std::move(aux->_item);
_front = _front->_succ;
if (_front == nullptr) {
_back = nullptr;
} else {
_front->_pred = nullptr;
}
delete aux;
--this->_size;
return item;
}
/**< Insertion operations */
void push(size_type position, const T& item) {
if (position < 0 || position > this->_size)
throw std::out_of_range("Out of range access.");
if (position == 0) {
push_front(item);
return;
}
if (position == this->_size) {
push_back(item);
return;
}
node* p;
if (position < (_size >> 1)) {
p = _front;
for (size_type i = 0; i < position; ++i)
p = p->_succ;
} else {
p = _back;
for (size_type i = _size - 1; i > position; --i)
p = p->_pred;
}
p->_pred = p->_pred->_succ = new node(p->_pred, p, item);
++this->_size;
}
void push_back(const T& item) {
if (!_size) {
_front = _back = new node(nullptr, nullptr, item);
} else {
_back = _back->_succ = new node(_back, nullptr, item);
}
++this->_size;
}
void push_front(const T& item) {
if (!_size) {
_front = _back = new node(nullptr, nullptr, item);
} else {
_front = _front->_pred = new node(nullptr, _front, item);
}
++this->_size;
}
using iterator = iterator_base<T>;
iterator begin() {
return {_front};
}
iterator end() {
return {nullptr};
}
iterator rbegin() {
return {_back};
}
iterator rend() {
return {nullptr};
}
using const_iterator = iterator_base<const T>;
const_iterator begin() const {
return {_front};
}
const_iterator end() const {
return {nullptr};
}
const_iterator rbegin() const {
return {_back};
}
const_iterator rend() const {
return {nullptr};
}
self& operator=(self&& rhs) {
swap(*this, rhs);
return *this;
}
bool operator==(const self& rhs) const {
if (_size == rhs.size()) {
for (const_iterator a = begin(), b = rhs.begin();
a != end() && b != rhs.end(); ++a, ++b)
if (*a != *b)
return false;
return true;
}
return false;
}
bool operator==(const init_list& rhs) const {
if (_size == rhs.size()) {
// not sure about this auto, but tends to select const_iterator.
for (auto a = begin(), b = rhs.begin();
a != end() && b != rhs.end(); ++a, ++b)
if (*a != *b)
return false;
return true;
}
return false;
}
bool operator!=(const self& rhs) const {
return !(*this == rhs);
}
friend void swap(self& a, self& b) {
using std::swap;
swap(a._front, b._front);
swap(a._back, b._back);
swap(a._size, b._size);
}
private:
void empty_check() const {
if (!_size)
throw std::out_of_range("Empty list.");
}
node* _front { nullptr };
node* _back { nullptr };
size_type _size { 0 };
};
#endif /* DOUBLY_LINKED_LIST_H_ */