-
Notifications
You must be signed in to change notification settings - Fork 363
/
singly_linked_list.hpp
415 lines (333 loc) · 7.44 KB
/
singly_linked_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
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
/*
Singly linked list
------------------
A linked list is a common data structure used to maintain a dynamic list of
similar elements. The list is maintained so that each element points to the
next element and stores some data. To improve performance, the head and
tail elements are stored to keep a track of insertions and deletions.
*/
#ifndef SINGLY_LINKED_LIST_HPP
#define SINGLY_LINKED_LIST_HPP
#include <climits>
const int ERROR_VAL = INT_MIN;
/*
===========================================================================
Node
===========================================================================
Helper class for the nodes in the list.
*/
template<class T>
class Node {
T value;
Node<T>* next;
Node();
public:
Node(const T&, Node<T>* const);
Node(const Node&);
Node<T>& operator=(const Node<T>&);
~Node();
T& get_value();
void set_value(const T&);
Node<T>* get_next();
void set_next(Node<T>* const);
// inline friend functions:
friend bool operator==(const Node& lhs, const Node& rhs) {
return lhs.value == rhs.value;
}
friend bool operator!=(const Node& lhs, const Node& rhs) {
return !operator==(lhs, rhs);
}
friend bool operator>=(const Node& lhs, const Node& rhs) {
return !operator<(lhs, rhs);
}
friend bool operator<=(const Node& lhs, const Node& rhs) {
return !operator>(lhs, rhs);
}
friend bool operator>(const Node& lhs, const Node& rhs) {
return lhs.value > rhs.value;
}
friend bool operator<(const Node& lhs, const Node& rhs) {
return operator>(rhs,lhs);
}
};
/*
Constructor
-----------
*/
template<class T>
Node<T>::Node(const T& value, Node<T>* const next) {
this->value = value;
this->next = next;
}
/*
Copy constructor
----------------
*/
template<class T>
Node<T>::Node(const Node<T>& n) {
this->value = n.value;
this->next = n.next;
}
/*
Assignment operator
-------------------
This copies the RHS (right-hand-side value) to 'this' instance
*/
template<class T>
Node<T>& Node<T>::operator=(const Node<T>& rhs) {
this->value = rhs.value;
return *this;
}
/*
Destructor
----------
*/
template<class T>
Node<T>::~Node() {
this->next = nullptr;
}
/*
Getters and setters
-------------------
*/
template<class T>
T& Node<T>::get_value() {
return this->value;
}
template<class T>
void Node<T>::set_value(const T& other) {
this->value = other;
}
template<class T>
Node<T>* Node<T>::get_next() {
return this->next;
}
template<class T>
void Node<T>::set_next(Node<T>* const next) {
this->next = next;
}
/*
===========================================================================
SinglyLinkedList
===========================================================================
*/
template<class T>
class SinglyLinkedList {
Node<T>* head;
Node<T>* tail;
int size;
public:
SinglyLinkedList();
~SinglyLinkedList();
bool is_empty();
int length() const;
void insert_front(const T&);
void insert_rear(const T&);
void delete_front();
void delete_rear();
T value_at(int);
T operator[](int);
const T operator[](int) const;
void reverse();
void clear();
};
/*
Constructor
-----------
*/
template<class T>
SinglyLinkedList<T>::SinglyLinkedList() {
size = 0;
head = tail = nullptr;
}
/*
Destructor
----------
*/
template<class T>
SinglyLinkedList<T>::~SinglyLinkedList() {
clear();
}
/*
is_empty
--------
Returns true if the list is empty
*/
template<class T>
bool SinglyLinkedList<T>::is_empty() {
return size == 0;
}
/*
size
----
Returns the number of elements in this list
*/
template<class T>
int SinglyLinkedList<T>::length() const {
return size;
}
/*
insert_front
------------
Inserts an element at the head of the list.
Time complexity
---------------
O(1).
Space complexity
----------------
O(1).
*/
template<class T>
void SinglyLinkedList<T>::insert_front(const T& value) {
Node<T>* temp = new Node<T>(value, head);
if (head == nullptr)
tail = temp;
head = temp;
size++;
}
/*
insert_rear
-----------
Inserts an element at the tail of the list.
Time complexity
---------------
O(1).
Space complexity
----------------
O(1).
*/
template<class T>
void SinglyLinkedList<T>::insert_rear(const T& value) {
Node<T>* temp = new Node<T>(value, nullptr);
if (tail == nullptr)
head = temp;
else
tail->set_next(temp);
tail = temp;
++size;
}
/*
delete_front
------------
Removes the element from at the head.
Time complexity
---------------
O(1).
Space complexity
----------------
O(1).
*/
template<class T>
void SinglyLinkedList<T>::delete_front() {
Node<T>* temp = head;
if (temp == nullptr)
return;
head = head->get_next();
delete temp;
--size;
}
/*
delete_rear
------------
Removes the element from at the tail. To remove an element from the tail,
the list needs to be iterated through since the second-last element is needed.
Time complexity
---------------
O(N), where N is the number of nodes in the list.
Space complexity
----------------
O(1).
*/
template<class T>
void SinglyLinkedList<T>::delete_rear() {
Node<T>* temp = head;
if (temp == nullptr)
return;
if (temp == tail) {
delete temp;
head = tail = nullptr;
--size;
return;
}
while (temp->get_next() != tail)
temp = temp->get_next();
tail = temp;
temp = temp->get_next();
delete temp;
tail->set_next(nullptr);
--size;
}
/*
value_at
--------
This is used to find the value of the element at a particular position in
the list.
Time complexity
---------------
O(N), where N is the number of nodes in the list
Space complexity
----------------
O(1)
*/
template<class T>
T SinglyLinkedList<T>::value_at(int index) {
if (size == 0 or index > size)
return ERROR_VAL;
Node<T>* temp = head;
int i = 0;
while (i++ != index)
temp = temp->get_next();
return temp->get_value();
}
template<class T>
T SinglyLinkedList<T>::operator[](int index) {
return value_at(index);
}
template<class T>
const T SinglyLinkedList<T>::operator[](int index) const {
return value_at(index);
}
/*
reverse
-------
Reverse the list by changing links between nodes.
Time complexity
---------------
O(N), where N is the number of nodes in the list
Space complexity
----------------
O(1)
*/
template<class T>
void SinglyLinkedList<T>::reverse() {
if (size > 1) {
Node<T>* next = head;
Node<T>* current, * previous;
current = previous = nullptr;
tail = head;
while (next) {
current = next;
next = next->get_next();
current->set_next(previous);
previous = current;
}
head = current;
}
}
/*
clear
-----
This is used to empty the list and remove all the elements.
*/
template<class T>
void SinglyLinkedList<T>::clear() {
Node<T>* temp = head;
while (temp != nullptr) {
temp = temp->get_next();
delete head;
head = temp;
}
head = tail = nullptr;
size = 0;
}
#endif // SINGLY_LINKED_LIST_HPP