-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.h
More file actions
211 lines (195 loc) · 5.26 KB
/
LinkedList.h
File metadata and controls
211 lines (195 loc) · 5.26 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
#pragma once
#include <Arduino.h>
namespace JustusDevTools
{
template <class T>
class LinkedList
{
private:
class LinkedNode
{
public:
T data;
LinkedNode *forward;
LinkedNode *back;
LinkedNode()
{
this->data = NULL;
this->forward = NULL;
this->back = NULL;
}
LinkedNode(T data)
{
this->data = data;
this->forward = NULL;
this->back = NULL;
}
LinkedNode(LinkedNode *back, T data)
{
this->data = data;
this->back = back;
this->forward = NULL;
}
};
size_t size;
LinkedNode *firstNode;
LinkedNode *currentNode;
public:
//inits the list
LinkedList()
{
size = 0;
firstNode = NULL;
currentNode = NULL;
}
//returns size of the list
size_t length()
{
return size;
}
//adds a node to the last of the list
void add(T data)
{
size += 1;
if (firstNode == NULL)
{
currentNode = new LinkedNode(data);
firstNode = currentNode;
}
else
{
goto_last();
LinkedNode *tempNode = new LinkedNode(currentNode, data);
currentNode->forward = tempNode;
currentNode = tempNode;
}
}
//removes the current node and preserves the list
void remove()
{
size--;
if (currentNode->forward == NULL)
{
if (currentNode->back != NULL)
{ //forward false | back true
currentNode = currentNode->back;
delete currentNode->forward;
}
else
delete currentNode; //forward false | back false
}
else
{
if (currentNode->back != NULL)
{ //forward true | back true
LinkedNode *tempNode = currentNode->forward;
currentNode = currentNode->back;
delete currentNode->forward;
currentNode->forward = tempNode;
}
else
{ //forward true | back false
currentNode = currentNode->forward;
delete currentNode->back;
firstNode = currentNode;
}
}
}
//gets the current data
T get_data()
{
if (currentNode != NULL)
return currentNode->data;
else
return NULL;
}
//gets the current data then increments forward
T get_next()
{
T data;
bool exists = false;
if (currentNode != NULL)
{
exists = true;
data = currentNode->data;
}
currentNode = currentNode->forward;
if (exists)
{
return data;
}
else
return NULL;
}
//gets the current data then increments back
T get_back()
{
T data;
bool exists = false;
if (currentNode != NULL)
{
exists = true;
data = currentNode->data;
}
currentNode = currentNode->back;
if (exists)
{
return data;
}
else
return NULL;
}
//goes to the first node
void goto_first()
{
currentNode = firstNode;
}
//goes to the last node
void goto_last()
{
while (currentNode->forward != NULL)
{
currentNode = currentNode->forward;
}
}
//moves 1 forward
void move_forward()
{
if (currentNode->forward != NULL)
currentNode = currentNode->forward;
}
//moves 1 back
void move_back()
{
if (currentNode->back != NULL)
currentNode = currentNode->back;
}
//converts the linked list to an array of the given type
T *to_array()
{
T *output = new T[size];
LinkedNode *tempNode = firstNode;
for (int i = 0; i < size; i++)
{
output[i] = tempNode->data;
tempNode = tempNode->forward;
}
delete tempNode;
return output;
}
//deletes the list, keeps everything clean :)
~LinkedList()
{
if (firstNode != NULL)
{
goto_last();
while (currentNode->back != NULL)
{
currentNode = currentNode->back;
delete currentNode->forward;
}
delete currentNode;
}
}
};
} // namespace JustusDevTools