forked from ProAlgos/ProAlgos-Cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
singly_linked_list.cpp
336 lines (261 loc) · 5.45 KB
/
singly_linked_list.cpp
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
/*
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.
Author
------
Sarvesh P.
*/
#include "singly_linked_list.hpp"
/*
==========================================================================
Node class
==========================================================================
*/
/*
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(void) {
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(void) {
return this->next;
}
template<class T>
void set_next(Node<T> * const next) {
this->next = next;
}
/*
==========================================================================
SinglyLinkedList class
==========================================================================
*/
/*
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(void) {
return size == 0;
}
/*
size
----
Returns the number of elements in this list
*/
template<class T>
int SinglyLinkedList<T>::length(void) const {
return size;
}
/*
=========
Insertion
=========
*/
/*
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;
}
/*
========
Deletion
========
*/
/*
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(void) {
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(void) {
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;
}
/*
========
Indexing
========
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 *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);
}
/*
clear
-----
This is used to empty the list and remove all the elements.
*/
template<class T>
void SinglyLinkedList<T>::clear(void) {
Node<T> *temp = head;
while (temp != nullptr) {
temp = temp->get_next();
delete head;
head = temp;
}
head = tail = nullptr;
size = 0;
}