-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathList.hpp
330 lines (257 loc) · 6.19 KB
/
List.hpp
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
#ifndef BUW_LIST_HPP
#define BUW_LIST_HPP
#include <cstddef>
#include <iterator>
template <typename T>
struct List;
template <typename T>
struct ListNode
{
ListNode() : m_value(), m_prev(nullptr), m_next(nullptr) {}
ListNode(T const& v, ListNode* prev, ListNode* next)
: m_value(v), m_prev(prev), m_next(next)
{}
T m_value;
ListNode* m_prev; //
ListNode* m_next;
};
template <typename T>
struct ListIterator
{
public:
typedef ListIterator<T> Self;
typedef ListNode<T> Node;
typedef T value_type;
typedef T* pointer;
typedef T& reference;
typedef ptrdiff_t difference_type;
typedef std::forward_iterator_tag iterator_category;
friend class List<T>;
ListIterator() : m_node(nullptr) {}
ListIterator(ListNode<T>* n) : m_node(n) {}
reference operator*() const { return m_node->m_value; }
pointer operator->() const { return &(m_node->m_value); }
Self& operator++() {
if (nullptr != m_node) {
m_node = m_node->m_next;
}
return *this;
}
Self operator++(int) {
ListIterator temp{*this};
++(*this);
return temp;
}
bool operator==(const Self& rhs) const { return (m_node == rhs.m_node); }
bool operator!=(const Self& rhs) const { return (m_node != rhs.m_node); }
Self next() const {
if (m_node)
return ListIterator(m_node->m_next);
else
return ListIterator(nullptr);
}
Node* node() const {
return m_node;
}
private:
// The Node the iterator is pointing to
ListNode<T>* m_node;
};
template <typename T>
struct ListConstIterator
{
typedef ListConstIterator<T> Self;
typedef ListNode<T> Node;
typedef T value_type;
typedef T* pointer;
typedef T const& reference;
typedef ptrdiff_t difference_type;
typedef std::forward_iterator_tag iterator_category;
friend class List<T>;
ListConstIterator() : m_node(nullptr) {}
ListConstIterator(ListNode<T>* n) : m_node(n) {}
reference operator*() const { return m_node->m_value; }
pointer operator->() const { return &(m_node->m_value); }
Self& operator++() {
if (m_node != nullptr)
m_node = m_node->m_next;
return *this;
}
Self operator++(int) {
if (m_node != nullptr)
m_node = m_node->m_next;
return this;
}
bool operator==(const Self& x) const { return (m_node == x.m_node); }
bool operator!=(const Self& x) const { return (m_node != x.m_node); }
Self next() const
{
if (m_node)
return ListConstIterator(m_node->m_next);
else
return ListConstIterator(nullptr);
}
private:
ListNode<T>* m_node;
};
template <typename T>
class List
{
public:
List() : m_size{0}, m_first{nullptr}, m_last{nullptr} {}
//copy ctor
List(List const& l) : m_size{0}, m_first{nullptr}, m_last{nullptr} {
for (auto i : l) {
push_back(i);
}
}
//move ctor
List(List&& l) : m_size{l.m_size}, m_first{l.m_first}, m_last{l.m_last} {
l.m_size = 0;
l.m_first = nullptr;
l.m_last = nullptr;
}
~List() { clear(); }
bool empty() const { return m_size == 0; }
std::size_t size() const { return m_size; }
void push_front(T const& v) {
ListNode<T>* ln = new ListNode<T>{v, nullptr, m_first};
if (empty()) {
m_last = ln;
} else {
m_first->m_prev = ln;
}
m_first = ln;
++m_size;
};
void push_back(T const& v) {
ListNode<T>* ln = new ListNode<T>{v, m_last, nullptr};
if (size() > 0)
m_last->m_next = ln;
if (empty())
m_first = ln;
m_last = ln;
++m_size;
}
void pop_front() {
if (!empty()) {
if (size() > 1) {
ListNode<T>* newFirst = m_first->m_next;
newFirst->m_prev = nullptr;
delete m_first;
m_first = newFirst;
} else { //size==1
delete m_first;
m_first = nullptr;
m_last = nullptr;
}
--m_size;
}
}
void pop_back() {
if (!empty()) {
if (size() > 1) {
ListNode<T>* newLast = m_last->m_prev;
newLast->m_next = nullptr;
delete m_last;
m_last = newLast;
} else {
delete m_last;
m_last = nullptr;
m_first = nullptr;
}
--m_size;
}
}
void clear() {
while (!empty()) {
pop_front();
}
}
ListIterator<T> insert(ListIterator<T> pos, T const& val) {
ListNode<T>* ln = new ListNode<T>{val, nullptr, nullptr};
if (pos != begin()) {
ln->m_prev = pos.node()->m_prev;
(pos.node()->m_prev)->m_next = ln;
} else {
m_first = ln;
}
if (pos != end()) {
ln->m_next = pos.node();
pos.node()->m_prev = ln;
} else {
m_last = ln;
}
++m_size;
++pos;
return pos;
}
void reverse() {
auto it = ListIterator<T>{m_last};
while (it != end()) {
//swap pointers of node:
auto tmp = it.node()->m_next;
it.node()->m_next = it.node()->m_prev;
it.node()->m_prev = tmp;
++it;
}
//swap m_first and m_last:
auto temp = m_first;
m_first = m_last;
m_last = temp;
}
T& front() const {
if (!empty())
return m_first->m_value;
}
T& back() const {
if (!empty())
return m_last->m_value;
}
ListIterator<T> begin() const {
return ListIterator<T>{m_first};
}
ListIterator<T> end() const {
return ListIterator<T>{};
}
ListConstIterator<T> cbegin() const {
return ListConstIterator<T>{m_first};
}
ListConstIterator<T> cend() const {
return ListConstIterator<T>{};
}
List<T>& operator=(List<T> const& rhs) {
clear();
for (auto i : rhs) {
push_back(i);
}
}
private:
std::size_t m_size;
ListNode<T>* m_first;
ListNode<T>* m_last;
};
template<typename T>
bool operator==(List<T> const& xs, List<T> const& ys) {
if (xs.size() != ys.size())
return false;
ListConstIterator<T> it1 = xs.cbegin();
ListConstIterator<T> it2 = ys.cbegin();
for (ListConstIterator<T> i = xs.cbegin(); i != xs.cend(); ++i) {
if (*i != *it2)
return false;
++it2;
}
return true;
}
template<typename T>
bool operator!=(List<T> const& xs, List<T> const& ys) {
return !(xs == ys);
}
template<typename T>
List<T> reverse(List<T> const& l) {
List<T> list{l};
list.reverse();
return list;
}
#endif // #define BUW_LIST_HPP