-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlinked_list.h
312 lines (268 loc) · 6.37 KB
/
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
#ifndef STRUCTURES_LINKED_LIST_H
#define STRUCTURES_LINKED_LIST_H
#include <cstdint>
#include <stdexcept>
#include <traits.h>
namespace structures {
/**
* @brief Implements a singly linked list
* @details A linked list, is a list where each node has a pointer to the next
* node, so, you only need a pointer to the first node (which here is head).
* The last node points to 'nullptr'.
*
* @tparam T Data type of the elements
*/
template <typename T>
class LinkedList {
public:
LinkedList() = default;
LinkedList(const LinkedList<T>& other)
: head{copy_list(other.head)}, size_{other.size_} {}
LinkedList(LinkedList<T>&& other) : head{other.head}, size_{other.size_} {
other.head = nullptr;
other.size_ = 0;
}
LinkedList<T>& operator=(const LinkedList<T>& other) {
LinkedList<T> copy{other};
std::swap(head, copy.head);
std::swap(size_, copy.size_);
return *this;
}
LinkedList<T>& operator=(LinkedList<T>&& other) {
LinkedList<T> copy{std::move(other)};
std::swap(head, copy.head);
std::swap(size_, copy.size_);
return *this;
}
virtual ~LinkedList() { clear(); }
/**
* @brief Clears the list
*/
void clear() {
while (!empty())
pop_front();
}
/**
* @brief Inserts at the end of the list
*
* @param data The element that'll be inserted
*/
void push_back(const T& data) { insert(data, size_); }
/**
* @brief Inserts at the beginning of the list
*
* @param data The element that'll be inserted
*/
void push_front(const T& data) {
head = new Node(data, head);
++size_;
}
/**
* @brief Inserts at a given position of the list
*
* @param data The element that'll be inserted
* @param index The position where 'data' will be inserted
*/
void insert(const T& data, std::size_t index) {
if (index == 0) {
return push_front(data);
} else if (index > size_) {
throw std::out_of_range("Invalid index");
} else {
Node* it = head;
for (std::size_t i = 0; i < index - 1; ++i) {
it = it->next;
}
it->next = new Node(data, it->next);
++size_;
}
}
/**
* @brief Inserts the element sorted into the list
*
* @param data The element that'll be inserted
*/
void insert_sorted(const T& data) {
if (empty() || data <= head->data) {
return push_front(data);
} else {
Node* it = head;
while (it->next != nullptr && data > it->next->data) {
it = it->next;
}
it->next = new Node(data, it->next);
++size_;
}
}
/**
* @brief Checks if the index is valid, then returns a reference to the
* element at the given index of the list
* @param index The index on the list of the element that'll be returned
*
* @return A reference to the element at the given index
*/
T& at(std::size_t index) {
return const_cast<T&>(static_cast<const LinkedList*>(this)->at(index));
}
const T& at(std::size_t index) const {
if (index >= size_) {
throw std::out_of_range("Index out of bounds");
} else {
Node* it = head;
for (std::size_t i = 0; i < index; i++) {
it = it->next;
}
return it->data;
}
}
/**
* @brief Removes the element at the given index
*
* @param index The index of the element that'll be removed
*
* @return The element that was removed
*/
T erase(std::size_t index) {
if (index >= size_) {
throw std::out_of_range("Index out of bounds");
} else if (index == 0) {
return pop_front();
} else {
Node* it = head;
for (std::size_t i = 0; i < index - 1; ++i) {
it = it->next;
}
T removed = it->next->data;
Node* p_removed = it->next;
it->next = it->next->next;
--size_;
delete p_removed;
return removed;
}
}
/**
* @brief Removes the element at the end of the list
*
* @return The removed element
*/
T pop_back() { return erase(size_ - 1); }
/**
* @brief Removes the element at the beginning of the list
*
* @return The removed element
*/
T pop_front() {
if (empty()) {
throw std::out_of_range("List is empty");
} else {
T removed = head->data;
Node* old_head = head;
head = head->next;
delete old_head;
--size_;
return removed;
}
}
/**
* @brief Removes 'data' from the list
*
* @param data The element that'll be removed
*/
void remove(const T& data) {
if (head->data == data) {
pop_front();
return;
} else {
Node* it;
for (it = head; it->next->data != data; it = it->next) {
if (it->next == nullptr)
return;
}
Node* p_removed = it->next;
it->next = it->next->next;
delete p_removed;
--size_;
}
}
/**
* @brief Checks if the list is empty
*
* @return True if the list is empty
*/
bool empty() const { return size_ == 0; }
/**
* @brief Checks if the list contains an element(data)
*
* @param data The element that'll be checked if it is contained by the
* list
*
* @return True if the list contains 'data'
*/
bool contains(const T& data) const { return find(data) != size_; }
/**
* @brief Returns the position of 'data' on the list
*
* @param data The element that'll be searched
*
* @return The index of 'data' on the list
*/
std::size_t find(const T& data) const {
std::size_t index = 0;
for (Node* it = head; it != nullptr; it = it->next) {
if (it->data == data)
break;
++index;
}
return index;
}
/**
* @brief Size of the list
*
* @return Size of the list
*/
std::size_t size() const { return size_; }
T& front() { return head->data; }
const T& front() const { return head->data; }
T& back() {
Node* it = head;
for (std::size_t i = 1; i < size_; ++i) {
it = it->next;
}
return it->data;
}
const T& back() const {
Node* it = head;
for (std::size_t i = 1; i < size_; ++i) {
it = it->next;
}
return it->data;
}
private:
struct Node {
explicit Node(const T& data) : data{data} {}
Node(const T& data, Node* next) : data{data}, next{next} {}
T data;
Node* next{nullptr};
};
static Node* copy_list(const Node* other_head) {
auto new_tail = new Node(other_head->data);
auto new_head = new_tail;
auto it = other_head->next;
while (it != nullptr) {
new_tail->next = new Node(it->data);
new_tail = new_tail->next;
it = it->next;
}
return new_head;
}
Node* head{nullptr};
std::size_t size_{0u};
};
} // namespace structures
/* list trait */
template <>
const bool traits::is_list<structures::LinkedList>::value = true;
/* name trait */
template <>
const std::string traits::type<structures::LinkedList>::name = "LinkedList";
#endif