-
Notifications
You must be signed in to change notification settings - Fork 0
/
List.cpp
417 lines (345 loc) · 8.51 KB
/
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
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#include "List.h"
List::List() {};
List::~List()
{
if (length() > 0) //if our list has elements in it by the end then we deallocate that memory
{
Node* current = head;
while (current != nullptr)
{
Node* deleteMe = current;
current = current->next;
delete deleteMe;
}
}
}
int List::getInvalidCount() { return invalidCount; }
void List::setInvalidCount(int x) { invalidCount = x; }
Node* List::getHead() { return head; }
Node* List::getTail() { return tail; }
void List::setHead(Node* newHead) { head = newHead; }
void List::setTail(Node* newTail) { tail = newTail; }
void List::adder(string expression) //adds a node to the linked list
{
if (expression == "") return; //checks that we're not trying to add a blank line
//else
Node* toAdd = new Node(expression);
if (toAdd->expressionIsGood == 1) //checks that the node should be added (that it is valid)
{
if (head == nullptr) //we have an empty list
{
head = toAdd;
tail = toAdd;
}
else //we are adding to the end;
{
tail->next = toAdd;
toAdd->previous = tail;
tail = toAdd;
}
}
else
{
++invalidCount;
return;
}
}
/*
int List::length()
{
int count = 0;
for (Node* current = head; current != nullptr; current = current->next)
++count;
return count;
}
*/
int List::length()
{
Node* cur = head;
return length(cur);
}
int List::length(Node* cur) //helper recursive function
{
if (cur == nullptr) return 0;
else if (cur->next == nullptr) return 1;
else return 1 + length(cur->next);
}
void List::removeByPos(int index)
{
Node* deleteMe;
int listLength = length();
if (index == 0) //we are removing the head
{
deleteMe = head;
head->next->previous = nullptr;
head = head->next;
delete deleteMe;
}
else if (index == listLength - 1) //we are removing the tail
{
deleteMe = tail;
tail->previous->next = nullptr;
tail = tail->previous;
delete deleteMe;
}
else //we are removing within the list
{
Node* current = head;
for (int i = 1; i <= index; ++i) //moves current to the right index
current = current->next;
deleteMe = current;
current->previous->next = current->next;
current->next->previous = current->previous;
delete deleteMe;
}
}
void List::bubbleSort()
{
for (Node* left = head; left != nullptr; left = left->next)
{
for (Node* right = left->next; right != nullptr; right = right->next)
{
if (left->evaluated > right->evaluated)
insert(left, right);
}
}
}
void List::insert(Node*& left, Node*& right)
{
Node* oLp = left->previous;
Node* oRp = right->previous;
Node* oLn = left->next;
Node* oRn = right->next;
if (left->previous == nullptr) //left is head, dont need to change left->previous->next;
{
right->previous = nullptr;
head = right;
}
else //left isn't head, we do need to change left->previous->next
{
right->previous = oLp;
oLp->next = right;
}
if (right->next == nullptr) //right is the tail, don't need to change right->next->previous
{
oRp->next = nullptr;
tail = oRp;
}
else //right isn't the tail, we need to change right->next->previous
{
right->next->previous = oRp;
oRp->next = oRn;
}
right->next = left;
left->previous = right;
Node* hold = left;
left = right;
right = hold;
}
void List::removeNegs(List& second)
{
//make a stack of negative numbers in first using recursion
stack<int> negsToRemove, posToRemove;
getNegs(negsToRemove, posToRemove);
//recursively try to remove that value's positive
//remove negative values from host list
Node* cur = head;
rBvH1(cur, negsToRemove);
//remove positives from host list
cur = head;
rBvH1(cur, posToRemove);
//if posToRemove is not empty remove values in posToRemove from second
if (!posToRemove.empty())
rBvH2(second, posToRemove);
}
void List::getNegs(stack<int>& negsToRemove, stack<int>& posToRemove)
{
Node* cur = head;
gNh(head, negsToRemove, posToRemove);
}
void List::gNh(Node* cur, stack<int>& negsToRemove, stack<int>& posToRemove)
{
if (cur == nullptr) return;
else
{
if (cur->evaluated < 0)
{
negsToRemove.push(cur->evaluated);
posToRemove.push(-1 * (cur->evaluated));
}
gNh(cur->next, negsToRemove, posToRemove);
}
}
void List::rBvH1(Node* cur, stack<int>& removables)
{
if (cur == nullptr || removables.empty()) return;
Node* deleteMe;
int val = removables.top();
if (cur->evaluated == val)
{
if (cur->previous == nullptr && cur->next == nullptr) //we are removing the only node
{
deleteMe = head;
head = nullptr;
tail = nullptr;
delete deleteMe;
}
else if (cur->previous == nullptr) //we are removing the head
{
deleteMe = head;
head->next->previous = nullptr;
head = head->next;
delete deleteMe;
}
else if (cur->next == nullptr) //we are removing the tail
{
deleteMe = tail;
tail->previous->next = nullptr;
tail = tail->previous;
delete deleteMe;
}
else //we are removing within the list
{
deleteMe = cur;
cur->previous->next = cur->next;
cur->next->previous = cur->previous;
delete deleteMe;
}
removables.pop(); //removes the item in removables if cur->evaluted is the same as removeables.top();
cur = head;
rBvH1(cur, removables);
}
if (cur == nullptr) return;
//else...
rBvH1(cur->next, removables);
}
void List::rBvH2(List& second, stack<int>& removables)
{
Node* head = second.getHead();
Node* cur = head;
rBvH2_1(cur, removables, second);
}
void List::rBvH2_1(Node* cur, stack<int>& removables, List& second)
{
if (cur == nullptr || removables.empty()) return;
//else
Node* deleteMe;
Node* sHead = second.getHead();
Node* sTail = second.getTail();
int val = removables.top();
if (cur->evaluated == val)
{
if (cur->previous == nullptr && cur->next == nullptr) //we are removing the only node
{
deleteMe = sHead;
second.setHead(nullptr);
second.setTail(nullptr);
delete deleteMe;
}
else if (cur->previous == nullptr) //we are removing the head
{
deleteMe = sHead;
sHead->next->previous = nullptr;
second.setHead(sHead->next);
cur = second.getHead();
delete deleteMe;
}
else if (cur->next == nullptr) //we are removing the tail
{
deleteMe = sTail;
sTail->previous->next = nullptr;
second.setTail(sTail->previous);
cur = second.getTail();
delete deleteMe;
}
else //we are removing within the list
{
deleteMe = cur;
cur->previous->next = cur->next;
cur->next->previous = cur->previous;
cur = cur->previous;
delete deleteMe;
}
removables.pop(); //removes the item in removables if cur->evaluted is the same as removeables.top();
cur = second.getHead();
rBvH2_1(cur, removables, second);
}
rBvH2_1(cur->next, removables, second);
}
void List::printLinkedList(ofstream& obj, Node* curr)
{
if (curr == nullptr && curr == head) //we have an empty linked list
{
obj << "[]\n";
return;
}
else if (curr == head)
{
obj << "[" << curr->evaluated;
printLinkedList(obj, curr->next);
}
else if (curr == NULL)
{
obj << "]\n";
return;
}
else
{
obj << ", " << curr->evaluated;
printLinkedList(obj, curr->next);
}
}
void List::printTreasureCode(ofstream& obj, Node* curr)
{
if (curr == NULL)
{
obj << "\n";
return;
}
if (curr == head)
{
obj << "Decoded passcode: ";
}
obj << "| " << curr->evaluated << " ";
if (curr->next == NULL)
{
obj << "|";
}
printTreasureCode(obj, curr->next);
}
void List::printActualCode(ofstream& obj, Node* curr)
{
if (curr == NULL)
{
obj << "\n";
return;
}
if (curr == head)
{
obj << "Actual passcode: ";
}
obj << "| " << curr->evaluated << " ";
if (curr->next == NULL)
{
obj << "|";
}
printTreasureCode(obj, curr->next);
}
//Make sure you have a funtion that outputs the Actual Passcode similar to the our Decoded one.
//Didn't include it here since I felt like it could be done in the main since it's just a string.
void List::comparePasscode(ofstream& obj, string actualPasscode, Node* curr, int passPosition)
{
bool invalid = 0;
if (curr == NULL && invalid != 1)
{
obj << "Treasure unlocked!" << "\n";
return;
}
if (actualPasscode[passPosition] - 48 != curr->evaluated)
{
cout << "Passcode invalid!" << "\n";
invalid = 1;
return;
}
comparePasscode(obj, actualPasscode, curr->next, ++passPosition);
}