-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingly_linked_list.h
More file actions
401 lines (375 loc) · 11.4 KB
/
Copy pathsingly_linked_list.h
File metadata and controls
401 lines (375 loc) · 11.4 KB
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
#ifndef DATA_STRUCTURES_SINGLY_LINKED_LIST_H
#define DATA_STRUCTURES_SINGLY_LINKED_LIST_H
#include <iostream>
#include <stdexcept>
namespace DataStructures {
/**
* @class SinglyLinkedList
* @brief Singly linked list class with all related functionality.
* @tparam T Type of the implementation class.
*/
template <typename T>
class SinglyLinkedList {
public:
/**
* @brief Constructor for the SinglyLinkedList class.
*
* Initializes the head node and points the tail to the head. Size is
* initialized to zero.
*/
SinglyLinkedList()
{
head = new Node(T());
tail = head;
size = 0;
}
/**
* @brief Destructor for the SinglyLinkedList class.
*
* Clears the singly linked list and deletes the head and tail pointers.
*/
~SinglyLinkedList()
{
Node* cur = head;
while (cur != nullptr) {
Node* next = cur->next;
delete cur;
cur = next;
}
}
/* Add functions */
/**
* @brief Adds a new node at the start of the list.
*
* This is achieved by linking the new node to the node pointed by head
* and updating head to point to the new node. If the list was empty
* earlier then the tail is updated. The size is incremented by unity.
*
* @param data The data of the node to be added.
*/
void add_first(const T& data)
{
Node* x = new Node(data);
x->next = head->next;
head->next = x;
if (size == 0) {
tail = x;
}
size++;
}
/**
* @brief Adds a new node at the end of the list.
*
* This is achieved by linking the node pointed by the tail to the new
* node and then updating the tail to point to the new node. The size is
* incremented by unity.
*
* @param data The data of the node to be added.
*/
void add_last(const T& data)
{
Node* x = new Node(data);
tail->next = x;
tail = x;
size++;
}
/**
* @brief Adds a new node at a valid position index.
*
* This is achieved by first checking the position index to make sure
* its valid using the private @ref check_position_index() function. If
* its the last valid position then we directly use the @ref add_last()
* function, otherwise we link the node before the position we want to
* add at to the new node and then link the new node to the node after
* it. The size is incremented by unity.
*
* @param pos_index Position index at which the new node is to be
* added.
* @param data Data of the node to be added.
*/
void add(int pos_index, const T& data)
{
check_position_index(pos_index);
if (pos_index == int(size)) {
add_last(data);
return;
}
Node* prev = head;
for (int i = 0; i < pos_index; i++) {
prev = prev->next;
}
Node* x = new Node(data);
x->next = prev->next;
prev->next = x;
size++;
}
/* Erase functions */
/**
* @brief Deletes the first node in the list.
*
* This is achieved by first using the utility @ref is_empty() function
* to check that the singly linked list is not empty and then linking
* the head to the node after the first node and updating the tail if the
* list contains only a single node after the operation. The size is
* updated, the node's memory freed and its old value returned.
*
* @return The data of the first node that will be deleted.
* @throws std::out_of_range if the list is empty. The exception message
* states that there are "No nodes to remove".
*/
T del_first()
{
if (is_empty()) {
throw std::out_of_range("No nodes to remove");
}
Node* first = head->next;
head->next = first->next;
if (size == 1) {
tail = head;
}
size--;
T data = first->data;
delete first;
return data;
}
/**
* @brief Deletes the last node in the list.
*
* This is achieved by first using the utility @ref is_empty() function
* to check that the singly linked list is not empty and then getting
* the data of the last node, freeing its memory and linking the node
* before it @c nullptr. The size and tail pointer are updated and the
* old data returned.
*
* @return The data of the ending node that will be deleted.
* @throws std::out_of_range if the list is empty. The exception message
* states that there are "No nodes to remove".
*/
T del_last()
{
if (is_empty()) {
throw std::out_of_range("No nodes to remove");
}
Node* prev = head;
while (prev->next != tail) {
prev = prev->next;
}
T data = tail->data;
delete tail;
prev->next = nullptr;
tail = prev;
size--;
return data;
}
/**
* @brief Delete a node at a particular index.
*
* This is achieved by using the private @ref check_element_index()
* function to first check if the index is valid and then linking the
* node before the node to be deleted to the node after the node to be
* deleted. If the index happens to be the last index of the list then
* the tail pointer is updated. Lastly, the size is decremented by
* unity, the node's memory freed and its old data returned.
*
* @return The data of the node that will be deleted.
*/
T del(int index)
{
check_element_index(index);
Node* prev = head;
for (int i = 0; i < index; i++) {
prev = prev->next;
}
Node* x = prev->next;
prev->next = x->next;
if (index == int(size) - 1) {
tail = prev;
}
size--;
T data = x->data;
delete x;
return data;
}
/* Get functions */
/**
* @brief Get the data stored in the first node.
*
* Uses the utility @ref is_empty() function to check if the first
* element exists and if it does, returns the data in the first node.
*
* @return The data in the first node.
* @throws std::out_of_range when the list is empty. The exception
* message states that there are "No nodes in the list".
*/
T get_first() const
{
if (is_empty()) {
throw std::out_of_range("No nodes in the list");
}
return head->next->data;
}
/**
* @brief Get the data stored in the last node.
*
* Uses the utility @ref is_empty() function to check if the last
* element exists and if it does, returns the data in the last node.
*
* @return The data in the last node.
* @throws std::out_of_range when the list is empty. The exception
* message states that there are "No nodes in the list".
*/
T get_last() const
{
if (is_empty()) {
throw std::out_of_range("No nodes in the list");
}
return tail->data;
}
/**
* @brief Get the data stored in a node at a particular index.
*
* Uses the private @ref get_node() function to check if the index is
* valid and returns the requested node's data.
*
* @param index Index of the element.
* @return The data of the requested node.
*/
T get(int index) const
{
Node* p = get_node(index);
return p->data;
}
/* Set function */
/**
* @brief Set the data in a node at a particular index.
*
* Uses the private @ref get_node() function to check if the index
* given is valid, sets its data and returns the old data.
*
* @param index Index of the node to be set.
* @param data Value to be set to.
*/
T set(int index, const T& data)
{
Node* p = get_node(index);
T x = p->data;
p->data = data;
return x;
}
/* Utility functions */
/**
* @brief Get the current size of the singly linked list.
* @return Current size of the singly linked list.
*/
size_t get_size() const { return size; }
/**
* @brief Check if the singly linked list is empty.
* @return true if the list contains no elements, false otherwise.
*/
bool is_empty() const { return size == 0; }
/**
* @brief Display the values in the nodes of the singly linked list.
*/
void display() const
{
std::cout << "Size = " << size << '\n';
Node* p = head->next;
while (p != tail) {
std::cout << p->data << "->";
p = p->next;
}
std::cout << "nullptr" << '\n';
std::cout << '\n';
}
private:
/** @brief Struct representing a node in a singly linked list */
struct Node {
T data; /** The data stored in the node. */
Node* next; /** Pointer to the next node. */
/**
* @brief Constructs a new node with given data and
* initializes the value of the next pointer.
*
* @param val The value of the data to store in the node.
*/
Node(T val) : data(val), next(nullptr) {}
};
/** Dummy head node. */
Node* head;
/** Actual tail node. */
Node* tail;
/** Current size of the linked list. */
size_t size;
/**
* @brief Get the requested node.
*
* Checks if the requested element index is valid and efficiently gets
* the required node based on the requested index.
*
* @param index Index of the desired node.
* @return Pointer to the requested node.
*/
Node* get_node(int index) const
{
check_element_index(index);
Node* p = head->next;
for (int i = 0; i < index; i++) {
p = p->next;
}
return p;
}
/**
* @brief Checks if a given element index is valid i.e in [0, size).
* @param index Index to be checked.
* @return true if valid, false otherwise.
*/
bool is_element_index(int index) const
{
return index >= 0 && index < int(size);
}
/**
* @brief Checks if a position index is valid i.e in [0, size].
* @param pos_index Position index to be checked.
* @return true if valid, false otherwise.
*/
bool is_position_index(int pos_index) const
{
return pos_index >= 0 && pos_index <= int(size);
}
/**
* @brief Checks the given index and throws an exception if out of
* range.
* @param index Index to be checked.
* @throws std::out_of_range is thrown when the index is out of the
* valid range i.e [0, size). The exception message includes
* the invalid index and the current size of the singly
* linked list.
*/
void check_element_index(int index) const
{
if (!is_element_index(index)) {
throw std::out_of_range(
"Index: " + std::to_string(index) +
", Size: " + std::to_string(size));
}
}
/**
* @brief Checks the given position index and throws an exception if
* out of range.
* @param pos_index Position index to be checked.
* @throws std::out_of_range is thrown when the position index is out
* of the valid range i.e [0, size]. The exception message
* includes the invalid position index and the current size of
* the singly linked list.
*/
void check_position_index(int pos_index) const
{
if (!is_position_index(pos_index)) {
throw std::out_of_range(
"Index: " + std::to_string(pos_index) +
", Size: " + std::to_string(size));
}
}
};
} // namespace DataStructures
#endif // DATA_STRUCTURES_LINKED_LIST_H