-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenLinkList.h
More file actions
286 lines (263 loc) · 5.59 KB
/
OpenLinkList.h
File metadata and controls
286 lines (263 loc) · 5.59 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
#include <assert.h>
#include <ostream>
using namespace std;
template <class T>
class OpenLinkList
{
protected:
template <class T>
struct ListNode
{
T data;
ListNode<T> * pointer = NULL;
};
ListNode<T> * first;
ListNode<T> * curr; //The node currently being accessed, AKA cursor
ListNode<T> * prev; //The last node
int size = 0; //The current size of the link list
int index = 0; //The node index currently being accessed
public:
OpenLinkList();
~OpenLinkList();
void add(T /*data*/); //Add after the current position of the list
void addToStart(T /*data*/); //Add to the start of the list
void add(int /*index*/, T/*data*/); //Add after the index location
void del(); //Delete the current node
void del(int /*Index*/); //delete the node at a given location
T get(); //Get the data for the current node
T get(int /**/); //Get an node by its index
bool next(); //Get the next link list member, returns false when at the end of the list
void reset(); //Reset curr to be at the start of the list
T getCurrentIndex() { return index; }; //gets the current index
void printList(ostream&); //Output all nodes from the link list
void moveTo(int /*Index to move to*/);
};
template <class T>
OpenLinkList<T>::OpenLinkList()
{
first = new ListNode<T>;
curr = first;
assert(first);
assert(curr);
}
//Delete all data
template <class T>
OpenLinkList<T>::~OpenLinkList()
{
reset();
//If there is only one node in the list
if (size == 1)
{
ListNode<T> * delNode = first->pointer;
delete(delNode);
}
else
{
ListNode<T> * nextNode = curr;
while (nextNode != NULL)
{
nextNode = nextNode->pointer;
delete(curr);
curr = nextNode;
}
delete(nextNode);
}
delete(first);
delete(curr);
delete(prev);
}
//Add a new node at the current position
template <class T>
void OpenLinkList<T>::add(T newData)
{
ListNode<T> * newNode = new ListNode<T>;
assert(newNode);
newNode->data = newData;
if (curr->pointer != NULL)
newNode->pointer = curr->pointer;
curr->pointer = newNode;
curr = newNode;
size++;
index++;
}
//Add a new node to a specific location in a link list
template <class T>
void OpenLinkList<T>::add(int pos, T newData)
{
if (pos > size)
throw "out of link list bounds";
moveTo(pos);
ListNode<T> * newNode = new ListNode<T>;
assert(newNode);
newNode->data = newData;
prev->pointer = newNode;
newNode->pointer = curr;
size++;
index++;
}
template <class T>
void OpenLinkList<T>::addToStart(T newData)
{
//If there is data in the list
if (size > 0)
{
ListNode<T> * newNode = new ListNode<T>;
newNode->data = newData;
newNode->pointer = first->pointer;
first->pointer = newNode;
size++;
}
//If the list is empty
else
add(newData);
}
// Get the next link list member, returns false when at the end of the list
template <class T>
bool OpenLinkList<T>::next()
{
if (curr->pointer != NULL)
{
prev = curr;
curr = curr->pointer;
index++;
return true;
}
else
return false;
}
//Delete the current node
template <class T>
void OpenLinkList<T>::del()
{
//If the link list is NOT empty
if (first->pointer != NULL)
{
//If there is only one node in the list
if (size == 1)
{
ListNode<T> * delNode = first->pointer;
assert(delNode);
delete(delNode);
delete(first);
first = new ListNode<T>;
curr = first;
assert(first);
assert(curr);
size--;
}
//When curr is the first node
else if (curr == first->pointer)
{
ListNode<T> * delNode = curr;
assert(delNode);
first->pointer = curr->pointer;
delete(delNode);
curr = first->pointer;
size--;
}
//When curr is the last node
else if (curr->pointer == NULL)
{
ListNode<T> * delNode = curr;
assert(delNode);
delete(delNode);
curr = prev;
curr->pointer = NULL;
size--;
}
//Any other position
else
{
ListNode<T> * delNode = curr;
assert(delNode);
curr = curr->pointer;
prev->pointer = curr;
delete(delNode);
size--;
}
}
}
//Delete a specific index
template <class T>
void OpenLinkList<T>::del(int pos)
{
//only delete if the pos is within the list
if (pos < size && pos >= 0)
{
moveTo(pos);
del();
}
}
//Reset to the start of the list
template <class T>
void OpenLinkList<T>::reset()
{
curr = first->pointer;
index = 0;
}
/* Output all nods from the link list
* Only works with objects that can be output as a string
*/
template <class T>
void OpenLinkList<T>::printList(ostream& out)
{
//The current position to be used to show the current location of the cursor
ListNode<T> * nodePos = curr;
ListNode<T> * nodePosPrev = prev;
int numPos = index;
out << "Link List" << endl;
if (first->pointer != NULL)
{
reset();
if (curr == nodePos)
out << '(' << get() << ')' << endl;
else
out << get() << endl;
while (next())
{
if (curr == nodePos)
out << '(' << get() << ')' << endl;
else
out << get() << endl;
}
}
out << endl << "Length: " << size << endl;
curr = nodePos;
prev = nodePosPrev;
index = numPos;
nodePos = NULL; //This will "disconnect" curr and nodePos so that delete dosent delete both
nodePosPrev = NULL;
delete(nodePos);
delete(nodePosPrev);
}
//Get the data at the current node
template <class T>
T OpenLinkList<T>::get()
{
return curr->data;
}
//Get the data at a specific index
template <class T>
T OpenLinkList<T>::get(int pos)
{
//Crash if out of bounds
if (pos > size)
throw "out of link list bounds";
if (index > pos)
reset();
moveTo(pos);
return curr->data;
}
//Moves curr to the specified index
template <class T>
void OpenLinkList<T>::moveTo(int pos)
{
//check if pos is in range
if (pos < size && pos >= 0)
{
if (pos < index)
reset();
while (index < pos)
next();
}
}