-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.hpp
More file actions
389 lines (306 loc) · 7.15 KB
/
Copy pathlist.hpp
File metadata and controls
389 lines (306 loc) · 7.15 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
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
#ifndef LIST_HPP
#define LIST_HPP
#include "exceptions.hpp"
//error handling and data protection should be handled by classes that use this object
template <typename T> class ListNode {
public:
ListNode();
ListNode(T d);
~ListNode();
T data;
//links to nodes around current
ListNode * next;
ListNode * prev;
};
// implementation
template <typename T> ListNode<T>::ListNode() {
//NULL should prevent data mistyping
data = NULL;
next = NULL;
prev = NULL;
}
template <typename T> ListNode<T>::ListNode(T d) {
data = d;
next = NULL;
prev = NULL;
}
template <typename T> ListNode<T>::~ListNode() {
next = NULL;
prev = NULL;
delete prev;
delete next;
}
template <typename T> class List {
public:
//insertFront
//param: data - is inserted at the front
virtual void insertFront(T data)=0;
//insertBack
//param: data - inserted in the back
virtual void insertBack(T data)=0;
//removeThat
//param: key - the data being removed, assumed to exist
//return: data
virtual T removeThat(T key)=0;
//removeFront
//return: data - delted and returned the front
virtual T removeFront()=0;
//removeBack
//return: data - delted anf returned the back
virtual T removeBack()=0;
//deletePos
//param: pos - index of data, assumed to exist
//return data at that index and delete it
virtual T deletePos(int pos)=0;
//getData
//param: pos - index of data, assumed to exist
//return: data at index
virtual T getData(int pos)=0;
//print
//prints each data to a newline
virtual void print()=0;
//fint
//param: data - that need to be found
//return: index - for that data, -1 if not found
virtual int find(T data)=0;
//getSize
//return: size - total number of list elements
virtual unsigned int getSize()=0;
//isEmpty
//return: true/false - true if empty
virtual bool isEmpty()=0;
};
template <typename T> class DoublyLList : public List<T> {
private:
ListNode<T> *front;
ListNode<T> *back;
unsigned int size;
public:
DoublyLList();
~DoublyLList();
void insertFront(T data);
void insertBack(T data);
T removeThat(T key);
T removeFront();
T removeBack();
T deletePos(int pos);
T getData(int pos);
void print();
int find(T data);
unsigned int getSize();
bool isEmpty();
};
// implementation
template <typename T> DoublyLList<T>::DoublyLList() {
size = 0;
front = NULL;
back = NULL;
}
template <typename T> DoublyLList<T>::~DoublyLList() {
//loop through and delete everything
while (front != NULL) {
ListNode<T> * temp = front;
front = front -> next;
temp = NULL;
delete temp;
}
}
template <typename T> unsigned int DoublyLList<T>::getSize() {
return size;
}
template <typename T> void DoublyLList<T>::print() {
ListNode<T> * current = front;
while (current -> next != NULL) {
std::cout << current -> data << endl;
current = current -> next;
}
std::cout << current -> data << '\n';
}
template <typename T> void DoublyLList<T>::insertFront(T data) {
ListNode<T> *node = new ListNode<T>(data);
if (isEmpty()) {
back = node;
} else {
front -> prev = node;
node -> next = front;
}
front = node;
size++;
}
template <typename T> bool DoublyLList<T>::isEmpty() {
return (size == 0);
}
template <typename T> void DoublyLList<T>::insertBack(T data) {
ListNode<T> * node = new ListNode<T>(data);
if (isEmpty()) {
front = node;
} else {
back -> next = node;
node -> prev = back;
}
back = node;
size++;
}
template <typename T> T DoublyLList<T>::removeThat(T key) {
ListNode<T> * curr = front;
while (curr -> data != key) {
curr = curr -> next;
//not found
if (curr == NULL) {
throw EmptyException();
}
}
//checks if the node is the front or the back first
if (curr == front) {
front = curr -> next;
} else {
curr -> prev -> next = curr -> next;
}
if (curr == back) {
back = curr -> prev;
} else {
curr -> next -> prev = curr -> prev;
}
curr -> next = NULL;
curr -> prev = NULL;
size--;
T tempData = curr -> data;
delete curr;
return tempData;
}
template <typename T> T DoublyLList<T>::removeFront() {
if (isEmpty()) {
throw EmptyException();
} else {
ListNode<T> * tempFront = front;
front = tempFront -> next;
tempFront -> next = NULL;
tempFront -> next = NULL;
size--;
T tempData = tempFront -> data;
delete tempFront;
return tempData;
}
}
template <typename T> T DoublyLList<T>::removeBack() {
if (isEmpty()) {
throw EmptyException();
} else {
ListNode<T> * tempBack = back;
back = tempBack -> prev;
tempBack -> next = NULL;
tempBack -> next = NULL;
size--;
T tempData = tempBack -> data;
delete tempBack;
return tempData;
}
}
template <typename T> int DoublyLList<T>::find(T data) {
int index = -1;
ListNode<T> * current = front;
while (current != NULL) {
++index;
if (current -> data == data) {
break;
} else {
current = current -> next;
}
}
if (current == NULL) {
index = -1;
}
return index;
}
template <typename T> T DoublyLList<T>::deletePos(int pos) {
if (pos < 0) {
throw NotFoundException();
}
unsigned int j = (unsigned) pos;
if (j - 1 > getSize() || isEmpty()) {
throw NotFoundException();
}
unsigned int i = 0;
ListNode<T> * current = front;
ListNode<T> * prev = front;
while (i != j) { //update pointers
prev = current;
current = current -> next;
i++;
}
prev -> next = current -> next;
current -> next = NULL;
T temp = current -> data;
delete current;
size--;
return temp;
}
template <typename T> T DoublyLList<T>::getData(int pos) {
if (pos < 0) {
throw NotFoundException();
}
unsigned int j = (unsigned) pos;
if (j - 1 > getSize() || isEmpty()) {
throw NotFoundException();
}
unsigned int i = 0;
ListNode<T> * current = front;
while (i != j) {
current = current -> next;
i++;
}
return current -> data;
}
template <typename T> class Queue {
public:
Queue();
~Queue();
//push
//param: data - added to the end of the queque
void push(T data);
//pop
//return: data - also deleted
T pop();
//peek
//return: first in que
T peek();
//isEmpty
//return: true/false - true if empty
bool isEmpty();
//getSize
//return: size - number of elements
int getSize();
private:
DoublyLList<T> line;
int size;
};
template <typename T> Queue<T>::Queue() {
line = DoublyLList<T>();
size = 0;
}
template <typename T> Queue<T>::~Queue() {
//nothing to delete for now
}
template <typename T> void Queue<T>::push(T data) {
line.insertBack(data);
size++;
}
template <typename T> T Queue<T>::peek() {
return line.getData(0);
}
template <typename T> T Queue<T>::pop() {
if (isEmpty()) {
throw EmptyException();
} else {
T tempData = line.removeFront();
size--;
return tempData;
}
}
template <typename T> bool Queue<T>::isEmpty() {
return (size == 0);
}
template <typename T> int Queue<T>::getSize() {
return size;
}
#endif