-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList.h
509 lines (414 loc) · 13.6 KB
/
LinkedList.h
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
//
// Created by luise on 5/3/2020.
//
#ifndef ALGORITHMS_LINKEDLIST_H
#define ALGORITHMS_LINKEDLIST_H
#include "Node.h"
#include <iomanip>
#include <fstream>
#include <iostream>
using std::cout;
using std::endl;
using std::fstream;
template<class T>
class LinkedList{
Node<T> * head;
Node<T> * tail;
Node<T> * iterator;
int size{}; // Size of Linked List
private:
void remove(Node<T>*); // Remove node
public:
LinkedList();
explicit LinkedList(T);
LinkedList(const LinkedList<T>&);
void print(); // print all values in Linked List
void printMatrix(int); // prints matrix
void printGraph_ToFile(fstream&);
void printMatrix_ToFile(fstream&,int);
void clear(); // Clears the Linked List, also used to destruct memory from heap
void append(T); // add node to Linked List
void removeAt(int); // remove specific index value (passed in parameter)
void popLastNode();
T getAt(T); // returns element at specific index
LinkedList<T>& operator=(const LinkedList<T>&);
bool operator!=(const LinkedList<T>&) const;
int getListSize() const;
// Custom Iterator
T& getIterator(); // Custom iterator functions
T& getNextIterator();
void resetIterator();
void iterateForward();
bool iteratorIsValid();
void setLinkedList_head(Node<T> *);
void setLinkedList_tail(Node<T> *);
void setLinkedList_iterator(Node<T> *);
Node<T> * getLinkedList_head();
Node<T> * getLinkedList_tail();
Node<T> * getLinkedList_iterator();
~LinkedList();
};
template<class T> // Default constructor
LinkedList<T>::LinkedList(): head(nullptr), tail(nullptr), iterator(nullptr), size(0){
}
template<class T>
LinkedList<T>::LinkedList(T x) {
head = new Node<T>(x);
size++;
}
template<class T>
LinkedList<T>::LinkedList(const LinkedList<T>& originalLinkedList){
if(originalLinkedList.head == nullptr){ // if original Linked List is empty
head = tail = nullptr; // head is = to nullptr
}
else{
Node<T>* aCurrent = originalLinkedList.head;
head = tail = nullptr;
auto count = 0;
while(aCurrent != nullptr){
auto* newNode = new Node<T>();
newNode->setPreviousNode(nullptr);
newNode->setNextNode(nullptr);
newNode->setData(aCurrent->getData());
if(count == 0){
head = tail = newNode;
}
else {
tail->setNextNode(newNode);
newNode->setPreviousNode(tail);
tail = tail->getNextNode();
}
++count;
aCurrent = aCurrent->getNextNode();
}
}
}
template<class T>
LinkedList<T>& LinkedList<T>::operator=(const LinkedList<T>& v2LinkedList) {
if(this != &v2LinkedList){
if(v2LinkedList.head == nullptr){
head = tail = nullptr;
}
else {
this->clear();
Node<T>* newHead;
Node<T>* newTail;
newHead = v2LinkedList.getLinkedList_head();
newTail = v2LinkedList.getLinkedList_tail();
setLinkedList_head(newHead);
Node<T>* temp;
while(newHead != nullptr){
temp = new Node<T>(newHead->getData());
newTail->setNextNode(temp);
newTail = newTail->getNextNode();
newHead = newHead->getNextNode();
}
}
}
return *this;
}
template<class T>
void LinkedList<T>::append(T x){
auto* newNode = new Node<T>(x);
if(head == nullptr){ // if Linked List is empty
head = newNode; // declare new node as the head and tail
tail = newNode;
}
else{
tail->setNextNode(newNode); // if linked list contains node already, set the node after the tail = to new node(previously nullptr)
newNode->setPreviousNode(tail); // at the end, the previous node prior to declaring new node, is the tail
tail = newNode; // declare new node as the tail
}
size++; // increment Linked List Size
}
template<class T>
void LinkedList<T>::resetIterator(){
this->iterator = this->head; // set's the iterator Nodeptr to the head of LinkedList
}
template<class T>
T& LinkedList<T>::getIterator(){
return *(iterator->getData()); // return iterator node data
}
template<class T>
T& LinkedList<T>::getNextIterator(){ // points to the next node in the LinkedList
this->iterator = this->iterator->getNextNode();
return *(iterator->getData());
}
template<class T>
void LinkedList<T>::iterateForward(){
if(this->iterator != nullptr || this->iterator->getNextNode() != nullptr){
this->iterator = this->iterator->getNextNode();
}
}
template<class T>
bool LinkedList<T>::iteratorIsValid(){
return this->iterator != nullptr;
}
template<class T>
int LinkedList<T>::getListSize() const{
return size;
}
template<class T>
void LinkedList<T>::removeAt(int index){
if (index >= size) { // if index is greater than Linked List Size
std::cout << "\nNothing here: (index >= size)" << index << std::endl << std::endl;
}
else {
Node<T>* temp = head; // set the head = to temp Current node
int count = 0; // counter
while (count != index) {
temp = temp->getNextNode(); // set the iterator node = to the next node
count++; // increment counter until index is reached
}
remove(temp);
size--; // decrement Linked List size
}
}
template<class T>
void LinkedList<T>::print(){
if(head == nullptr){
cout << "Nothing Available" << endl;
}
else{
Node<T>* aCurrent = head;
while(aCurrent != nullptr){ // cycles & prints all values in linked list
cout << aCurrent->getData()->getVertexNode_B() << " (";
cout << aCurrent->getData()->getWeight() << ")";
aCurrent = aCurrent->getNextNode();
if(aCurrent != nullptr){
cout << "--> ";
}
}
cout << endl;
}
}
template<class T>
void LinkedList<T>::printMatrix(int x){
if(head == nullptr){
cout << "Nothing Available" << endl;
}
else{
Node<T>* vertex_Iterator = head;
int counter = 1;
int vertexNode_A = stoi(vertex_Iterator->getData()->getVertexNode_A());
while(vertex_Iterator != nullptr){
int vertexNode_B = stoi(vertex_Iterator->getData()->getVertexNode_B());
if(vertexNode_A == vertexNode_B || vertexNode_A == counter){
if(vertexNode_A == counter){
cout << std::setw(4) << std::left << "0";
counter++;
vertex_Iterator = vertex_Iterator->getNextNode();
continue;
}
}
else if(vertexNode_B < counter){
if(counter > x){
counter = vertexNode_A + 1;
}
}
if(vertexNode_B != counter){
while(vertexNode_A != counter && counter <= x){
counter++;
}
}
else if(vertexNode_B == counter){
if(vertex_Iterator->getData()->getWeight() == INT_MAX){
cout << std::setw(4) << std::left << "INF";
counter++;
}
else if(vertex_Iterator->getData()->getWeight() != INT_MAX) {
cout << std::setw(4) << std::left << vertex_Iterator->getData()->getWeight();
counter++;
}
if(counter > x){
break;
}
}
vertex_Iterator = vertex_Iterator->getNextNode();
if(vertex_Iterator == nullptr){
while(counter <= x){
cout << std::setw(4) << std::left << "0";
counter++;
}
vertex_Iterator == nullptr;
}
}
cout << endl;
}
}
template<class T>
void LinkedList<T>::printGraph_ToFile(fstream& fout){
if(head == nullptr){
fout << "Nothing Available" << endl;
}
else{
Node<T>* aCurrent = head;
while(aCurrent != nullptr){ // cycles & prints all values in linked list
fout << aCurrent->getData()->getVertexNode_B() << " (";
fout << aCurrent->getData()->getWeight() << ")";
aCurrent = aCurrent->getNextNode();
if(aCurrent != nullptr){
fout << "--> ";
}
}
fout << endl;
}
}
template<class T>
void LinkedList<T>::printMatrix_ToFile(fstream& fout, int x){
if(head == nullptr){
fout << "Nothing Available" << endl;
}
else{
Node<T>* vertex_Iterator = head;
int counter = 1;
int vertexNode_A = stoi(vertex_Iterator->getData()->getVertexNode_A());
while(vertex_Iterator != nullptr){
int vertexNode_B = stoi(vertex_Iterator->getData()->getVertexNode_B());
if(vertexNode_A == vertexNode_B || vertexNode_A == counter){
if(vertexNode_A == counter){
fout << std::setw(4) << std::left << "0";
counter++;
vertex_Iterator = vertex_Iterator->getNextNode();
continue;
}
}
else if(vertexNode_B < counter){
if(counter > x){
counter = vertexNode_A + 1;
}
}
if(vertexNode_B != counter){
while(vertexNode_A != counter && counter <= x){
counter++;
}
}
else if(vertexNode_B == counter){
if(vertex_Iterator->getData()->getWeight() == INT_MAX){
fout << std::setw(4) << std::left << "INF";
counter++;
}
else if(vertex_Iterator->getData()->getWeight() != INT_MAX) {
fout << std::setw(4) << std::left << vertex_Iterator->getData()->getWeight();
counter++;
}
if(counter > x){
break;
}
}
vertex_Iterator = vertex_Iterator->getNextNode();
if(vertex_Iterator == nullptr){
while(counter <= x){
fout << std::setw(4) << std::left << "0";
counter++;
}
vertex_Iterator == nullptr;
}
}
fout << endl;
}
}
template<class T>
void LinkedList<T>::remove(Node<T>* iteratorNode){
Node<T>* nextNode = iteratorNode->next;
Node<T>* previousNode = iteratorNode->previous;
if (nextNode != nullptr) {
nextNode->previous = previousNode;
}
if (previousNode != nullptr) {
previousNode->next = nextNode;
}
if (iteratorNode == head) {
head = nextNode; // deletes head and new head is declared
}
if (iteratorNode == tail) { // deletes tail
tail = previousNode;
}
delete iteratorNode;
resetIterator();
}
template<class T>
void LinkedList<T>::popLastNode(){
remove(tail);
size--;
}
template<class T>
T LinkedList<T>::getAt(T x){
if(head == nullptr){
return -1;
}
else{
Node<T>* aCurrent = head; // begin at head
for(int i = 0; i < x; i++){
aCurrent = aCurrent->next; // cycle through Linked List
}
return aCurrent->data; // return element from linked list (specified in parameter
}
}
template<class T>
void LinkedList<T>::clear(){
Node<T>* c = getLinkedList_head();
while(c != nullptr){
c = c->getNextNode();
delete getLinkedList_head();
setLinkedList_head(c);
}
}
template<class T>
void LinkedList<T>::setLinkedList_head(Node<T> * newHead){
head = newHead;
}
template<class T>
void LinkedList<T>::setLinkedList_tail(Node<T> * newTail){
tail = newTail;
}
template<class T>
void LinkedList<T>::setLinkedList_iterator(Node<T> * newIterator){
iterator = newIterator;
}
template<class T>
Node<T> * LinkedList<T>::getLinkedList_head(){
return head;
}
template<class T>
Node<T> * LinkedList<T>::getLinkedList_tail(){
return tail;
}
template<class T>
Node<T> * LinkedList<T>::getLinkedList_iterator(){
return iterator;
}
template<class T>
bool LinkedList<T>::operator!=(const LinkedList<T>& aLinkedList)const {
if(this->head != aLinkedList.head){
return true;
}
if(this->tail != aLinkedList.tail){
return true;
}
if(this->iterator != aLinkedList.iterator){
return true;
}
if(size != aLinkedList.size){
return true;
}
if(head != nullptr){
Node<T>* c = head;
for(int i = 0; i < size; i++){
if(c == nullptr){
return false;
}
c = c->next;
}
return true;
}
if(this->iterator != aLinkedList.iterator){
return true;
}
return this->iterator->data != aLinkedList.iterator->data;
}
template<class T>
LinkedList<T>::~LinkedList<T>(){
clear(); // call the clear member function to deconstruct elements from HEAP
}
#endif //ALGORITHMS_LINKEDLIST_H