-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList.cpp
350 lines (296 loc) · 6.94 KB
/
LinkedList.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include "LinkedList.hpp"
/*******
TODO: Figure out if I can remove all of the template<typename T>s some how.
*******/
// Returns last valid index in list
template<typename T>
int List<T>::last_index()
{
if (m_length > 0)
{
return m_length - 1;
}
else
{
return -1;
}
}
template<typename T>
int List<T>::length()
{
return m_length;
}
template<typename T>
nodePTR List<T>::get_node_at_index(int index)
{
if (index > last_index())
{
std::cout << "[-] ERROR: Index out of range" << std::endl;
exit(-1);
}
/*
If the provided index less than or equal to the lower-half of the list,
start from the beggining of the list and itterate forward.
*/
if (index <= last_index()/2)
{
/*
* Start at the beggining of the list and iterates forward over list
* elements until desired index is reached,
* and then return the data contained within the list node at the index.
*/
nodePTR current_node = m_first_node;
for (int i = 0; i <= index; i++)
{
if(¤t_node->m_next != NULL)
{
if (i == index)
{
return current_node;
}
nodePTR next_node = current_node->m_next;
current_node = next_node;
}
}
}
/*
* Starts at the end of the list and iterates backwards through the list
* until the desired index is reached
*/
else
{
nodePTR current_node = m_last_node;
for (int i = last_index(); i >= index; i--)
{
if(¤t_node->m_data != NULL)
{
if (i == index)
{
return current_node;
}
nodePTR prev_node = current_node->m_prev;
current_node = prev_node;
}
}
}
std::cout << "[-] ERROR: Something went wrong while accessing list element at index " << index << std::endl;
exit(-1);
}
// Prints the contents of the list
template<typename T>
void List<T>::print()
{
std::cout << "[";
for(int i = 0; i < m_length; i++)
{
std::cout << at(i);
if (i != last_index())
std::cout << ", ";
}
std::cout << "]" << std::endl;
}
// Returns the data in node at given index
template<typename T>
T List<T>::at(int index)
{
nodePTR node = get_node_at_index(index);
return node->m_data;
}
// Prepends a new node with given data to start of list
template<typename T>
void List<T>::prepend(T data)
{
if (m_has_initialized)
{
// New node to be prepended
nodePTR node = new_node<T>();
// Sets the new node's previous node to that of the first node
node->m_prev = m_first_node->m_prev;
// Sets the previous node of the first node to the new node
m_first_node->m_prev = node;
// Sets the new node's next node to the first_node
node->m_next = m_first_node;
// Assignes the given data to the new node's data member
node->m_data = data;
// Sets the first node to the new node
m_first_node = node;
}
else
{
m_first_node->m_data = data;
m_has_initialized = true;
}
m_length++;
}
// Appends a new node with given data to end of list.
template<typename T>
void List<T>::append(T data)
{
if (m_has_initialized)
{
// New node that will be appended
nodePTR node = new_node<T>();
// Sets the new node's next node to the last_node's next node.
node->m_next = m_last_node->m_next;
// Sets the new node's previous node to the last node
node->m_prev = m_last_node;
// Sets the last_node's next node to the new node
m_last_node->m_next = node;
// Assignes the given data to the new node's data member
node->m_data = data;
// Updates last_node to the new node
m_last_node = node;
}
else
{
m_first_node->m_data = data;
m_has_initialized = true;
}
m_length++;
}
template<typename T>
T List<T>::remove(int index)
{
if (index > last_index())
{
std::cout << "[+] ERROR: Index out of range" << std::endl;
exit(-1);
}
nodePTR target_node = get_node_at_index(index);
T data = target_node->m_data;
if (target_node == m_first_node)
{
if (m_first_node->m_next)
{
m_first_node = m_first_node->m_next;
m_first_node->m_prev = nullptr;
}
}
else if (target_node == m_last_node)
{
if (m_last_node->m_prev)
{
m_last_node = m_last_node->m_prev;
m_last_node->m_next = nullptr;
}
}
else
{
nodePTR before_node = target_node->m_prev;
nodePTR after_node = target_node->m_next;
before_node->m_next = after_node;
after_node->m_prev = before_node;
}
delete target_node;
m_length--;
return data;
}
template<typename T>
T List<T>::pop()
{
nodePTR target_node = m_last_node;
nodePTR before_node = m_last_node->m_prev;
T data = m_last_node->m_data;
if(before_node != NULL)
{
m_last_node = before_node;
m_last_node->m_next = nullptr;
}
delete target_node;
m_length--;
return data;
}
// Inserts a new node with given data after given index
template<typename T>
void List<T>::insert_after(T data, int index)
{
// If true, then given index is out of range
if (index > last_index())
{
std::cerr << "[+] ERROR: Index " << index << " out of range for list that is of size " << m_length << std::endl;
exit(-1);
}
// If true, then we just need to append a new node.
if (index == last_index() || m_length == 0)
{
append(data);
// DO NOT INCREMENT LENGTH AS THIS IS ALREADY DONE IN APPEND METHOD
}
else
{
nodePTR n_node = new_node<T>(); // New node
nodePTR target_node = get_node_at_index(index); // Node to insert n_node after
nodePTR after_node = target_node->m_next;
n_node->m_data = data;
n_node->m_prev = target_node;
n_node->m_next = after_node;
target_node->m_next = n_node;
after_node->m_prev = n_node;
m_length++;
}
}
// Inserts a new node with given data before given index
template<typename T>
void List<T>::insert_before(T data, int index)
{
// If true, then index is out of range
if (index > last_index())
{
std::cout << "[-] ERROR: Index " << index << " out of range for list that is of length " << m_length << std::endl;
exit(-1);
}
// DO NOT INCREMENT SIZE AS THIS IS DONE IN PREPEND AND INSERT_AFTER METHODS
if (index == 0)
{
prepend(data);
}
else
{
insert_after(data, index - 1);
}
}
// Returns the list size in bytes
template<typename T>
int List<T>::size_bytes()
{
int size = 0;
nodePTR current_node = m_first_node;
while (current_node != NULL)
{
nodePTR next_node = current_node->m_next;
current_node = next_node;
size += sizeof(Node<T>);
}
return size;
}
/* Constructor and destructor functions */
// Default
template<typename T>
List<T>::List(void){};
// Initialize list with values of passed array
template<typename T>
List<T>::List(T init_arr[], int size)
{
for (int i = 0; i < size; i++)
{
append(init_arr[i]);
}
}
// Destroy list when it goes out of scope.
template<typename T>
List<T>::~List()
{
if (size_bytes() > sizeof(Node<T>))
{
nodePTR current_node = m_first_node;
while (current_node != nullptr)
{
nodePTR target_node = current_node;
if (target_node != nullptr)
{
delete target_node;
}
current_node = current_node->m_next;
}
}
}