-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList.cpp
115 lines (99 loc) · 3.19 KB
/
LinkedList.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
#include "LinkedList.h"
LinkedList::LinkedList() {
this->head = nullptr;
this->tail = nullptr;
this->length = 0;
}
LinkedList::~LinkedList() = default;
bool LinkedList::isEmpty() {
return (this->size() == 0);
}
int LinkedList::size() {
return this->length;
}
SharedTile LinkedList::getTile(int index) {
SharedTile tile = nullptr;
// Check for linkedlist and if index is within range or not.
if (!this->isEmpty() && index >= 0 && index < this->size()) {
std::shared_ptr<Node> curr = head;
for (int i = 0; i < index; i++) {
curr = curr->getNext();
}
tile = curr->getTile();
}
return tile;
}
SharedTile LinkedList::getTile(const Tile & searchTile) {
// Check for linkedlist and if index is within range or not.
SharedTile tile = nullptr;
std::shared_ptr<Node> curr = head;
while (tile == nullptr && curr != nullptr) {
if (curr->getTile()->isEqual(searchTile)) {
tile = curr->getTile();
}
curr = curr->getNext();
}
return tile;
}
// This method will always add Tile at the back of the LinkedList.
void LinkedList::addTile(const SharedTile & tile) {
if (tile != nullptr) {
std::shared_ptr<Node> newNode = std::make_shared<Node>(tile, nullptr);
if (this->head == nullptr) {
this->head = newNode;
this->tail = newNode;
} else {
this->tail->setNext(newNode);
this->tail = newNode;
}
++this->length;
}
}
SharedTile LinkedList::deleteTile(const SharedTile & toRemove) {
SharedTile tile = nullptr;
// Check if the linked list is empty or not.
if (!this->isEmpty()) {
std::shared_ptr<Node> curr = head;
std::shared_ptr<Node> prev = nullptr;
//Loop till the tile is found or reached to end of linked list.
while (curr != nullptr && tile == nullptr) {
// same memory location
if (toRemove == curr->getTile()) {
// middle or end of the list
if (prev != nullptr) {
prev->setNext(curr->getNext());
}
// beginning of list
else {
head = curr->getNext();
}
if (curr == tail) {
this->tail = prev;
}
tile = curr->getTile();
// For development only. Production will never enter this state
if (tile == nullptr)
throw std::runtime_error(
"Reached illegal state Terminating...");
length--;
}
prev = curr;
curr = curr->getNext();
}
}
return tile;
}
string LinkedList::toString(bool isColour, bool isSaving) {
string result = "";
// Get and converts each tile in the list to a string
std::shared_ptr<Node> curr = head;
while (curr != nullptr) {
result += curr->getTile()->toString(isColour, isSaving);
// Does not produce a comma after the last tile
if (curr->getNext() != nullptr) {
result += ",";
}
curr = curr->getNext();
}
return result + "\n";
}