blob: d8b73db8989ad1745b4465156f32138435859a55 (
plain) (
blame)
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
421
422
|
#pragma once
#include <cstddef>
#include <utility>
template <typename T>
class singly_linked_list
{
private:
struct node
{
T data;
node* next;
node(const T& data_, node* next_ = nullptr) :
data(data_),
next(next_) {}
};
node* _head = nullptr;
node* _tail = nullptr;
size_t _size = 0;
public:
singly_linked_list() = default;
singly_linked_list(const singly_linked_list<T>& other);
singly_linked_list(singly_linked_list<T>&& other);
singly_linked_list<T>& operator=(const singly_linked_list<T>& other);
singly_linked_list<T>& operator=(singly_linked_list<T>&& other);
~singly_linked_list();
void push_front(const T& el);
void push_back(const T& el);
void pop_front();
const T& front() const;
const T& back() const;
T& front();
T& back();
size_t size() const;
bool empty() const;
template <typename U>
friend singly_linked_list<U> concat(singly_linked_list<U>& lhs, singly_linked_list<U>& rhs);
class sll_iterator
{
node* currentElementPtr;
friend class singly_linked_list;
public:
sll_iterator(node* currentElementPtr = nullptr) : currentElementPtr(currentElementPtr) {}
T& operator*()
{
return currentElementPtr->data;
}
T* operator->()
{
return ¤tElementPtr->data;
}
sll_iterator& operator++()
{
if (currentElementPtr)
currentElementPtr = currentElementPtr->next;
return *this;
}
sll_iterator operator++(int)
{
sll_iterator copy(*this);
++(*this);
return copy;
}
sll_iterator& operator+=(size_t s)
{
while (s--)
++(*this);
return *this;
}
sll_iterator operator+(int i) const
{
sll_iterator res(*this);
return res += i;
}
bool operator==(const sll_iterator& rhs) const { return currentElementPtr == rhs.currentElementPtr; }
bool operator!=(const sll_iterator& rhs) const { return !(rhs == *this); }
};
class const_sll_iterator
{
node* currentElementPtr;
friend class singly_linked_list;
public:
const_sll_iterator(const sll_iterator& nonConstIter) : currentElementPtr(nonConstIter.currentElementPtr) {}
const_sll_iterator(node* currentElementPtr = nullptr) : currentElementPtr(currentElementPtr) {}
const T& operator*() const
{
return currentElementPtr->data;
}
const T* operator->() const
{
return ¤tElementPtr->data;
}
const_sll_iterator& operator++()
{
if (currentElementPtr)
currentElementPtr = currentElementPtr->next;
return *this;
}
const_sll_iterator operator++(int)
{
const_sll_iterator copy(*this);
++(*this);
return copy;
}
const_sll_iterator& operator+=(size_t s)
{
while (s--)
++(*this);
return *this;
}
const_sll_iterator operator+(int i) const
{
const_sll_iterator res(*this);
return res += i;
}
bool operator==(const const_sll_iterator& rhs) const { return currentElementPtr == rhs.currentElementPtr; }
bool operator!=(const const_sll_iterator& rhs) const { return !(rhs == *this); }
};
sll_iterator begin()
{
return sll_iterator(_head);
}
sll_iterator end()
{
return sll_iterator(nullptr);
}
const_sll_iterator cbegin() const
{
return const_sll_iterator(_head);
}
const_sll_iterator cend() const
{
return const_sll_iterator(nullptr);
}
sll_iterator insert_after(const T& element, const const_sll_iterator& it);
sll_iterator remove_after(const const_sll_iterator& it);
private:
void copy(const singly_linked_list<T>& other);
void move(singly_linked_list<T>&& other);
void free();
};
template <typename T>
void singly_linked_list<T>::push_front(const T& el)
{
node* new_node = new node(el);
if (empty())
{
_head = _tail = new_node;
}
else
{
new_node->next = _head;
_head = new_node;
}
_size++;
}
template <typename T>
void singly_linked_list<T>::push_back(const T& el)
{
node* new_node = new node(el);
if (empty())
{
_head = _tail = new_node;
}
else
{
_tail->next = new_node;
_tail = new_node;
}
_size++;
}
template <typename T>
void singly_linked_list<T>::pop_front()
{
if (empty())
return;
if (_head == _tail)
{
delete _head;
_head = _tail = nullptr;
}
else
{
node* to_delete = _head;
_head = _head->next;
delete to_delete;
}
_size--;
}
template <typename T>
const T& singly_linked_list<T>::front() const
{
return _head->data;
}
template <typename T>
const T& singly_linked_list<T>::back() const
{
return _tail->data;
}
template <typename T>
T& singly_linked_list<T>::front()
{
return _head->data;
}
template <typename T>
T& singly_linked_list<T>::back()
{
return _tail->data;
}
template <typename T>
singly_linked_list<T> concat(singly_linked_list<T>& lhs, singly_linked_list<T>& rhs)
{
singly_linked_list<T> result;
if (!lhs._head)
{
result._head = rhs._head;
result._tail = rhs._tail;
}
else if (!rhs._head)
{
result._head = lhs._head;
result._tail = lhs._tail;
}
else
{
lhs._tail->next = rhs._head;
result._head = lhs._head;
result._tail = rhs._tail;
}
result._size = rhs._size + lhs._size;
rhs._size = lhs._size = 0;
rhs._head = rhs._tail = lhs._head = lhs._tail = nullptr;
return result;
}
template <typename T>
typename singly_linked_list<T>::sll_iterator singly_linked_list<T>::insert_after(const T& element, const typename singly_linked_list<T>::const_sll_iterator& it)
{
if(it == end())
return end();
node* new_node = new node(element);
node* itNode = it.currentElementPtr;
new_node->next = itNode->next;
itNode->next = new_node;
_size++;
if(itNode == _tail){
_tail = new_node;
}
return singly_linked_list<T>::sll_iterator(new_node);
}
template <typename T>
typename singly_linked_list<T>::sll_iterator singly_linked_list<T>::remove_after(const typename singly_linked_list<T>::const_sll_iterator& it)
{
if(it == end() || !it.currentElementPtr->next)
return end();
node* to_delete = (it + 1).currentElementPtr;
node* new_next = to_delete->next;
it.currentElementPtr->next = new_next;
_size--;
if(to_delete == _tail)
{
_tail = it.currentElementPtr;
}
delete to_delete;
return singly_linked_list<T>::sll_iterator(new_next);
}
template <typename T>
bool singly_linked_list<T>::empty() const
{
return size() == 0;
}
template <typename T>
size_t singly_linked_list<T>::size() const
{
return _size;
}
template <typename T>
singly_linked_list<T>::singly_linked_list(const singly_linked_list<T>& other)
{
copy(other);
}
template <typename T>
singly_linked_list<T>::singly_linked_list(singly_linked_list<T>&& other)
{
move(std::move(other));
}
template <typename T>
singly_linked_list<T>& singly_linked_list<T>::operator=(const singly_linked_list<T>& other)
{
if (this != &other)
{
free();
copy(other);
}
return *this;
}
template <typename T>
singly_linked_list<T>& singly_linked_list<T>::operator=(singly_linked_list<T>&& other)
{
if (this != &other)
{
free();
move(std::move(other));
}
return *this;
}
template <typename T>
singly_linked_list<T>::~singly_linked_list()
{
free();
}
template <typename T>
void singly_linked_list<T>::copy(const singly_linked_list<T>& other)
{
node* iter = other._head;
while (iter)
{
push_back(iter->data);
iter = iter->next;
}
}
template <typename T>
void singly_linked_list<T>::move(singly_linked_list<T>&& other)
{
_head = other._head;
_tail = other._tail;
_size = other.size();
other._head = other._tail = nullptr;
other._size = 0;
}
template <typename T>
void singly_linked_list<T>::free()
{
node* iter = _head;
while (iter)
{
node* to_delete = iter;
iter = iter->next;
delete to_delete;
}
_head = _tail = nullptr;
_size = 0;
}
|