-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTileList.cpp
More file actions
executable file
·101 lines (91 loc) · 1.94 KB
/
TileList.cpp
File metadata and controls
executable file
·101 lines (91 loc) · 1.94 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
#include "TileList.h"
#include <iostream>
TileList::TileList() : head(nullptr), tail(nullptr), length(0)
{}
TileList::~TileList()
{
clear();
}
int TileList::size()
{
return length;
}
void TileList::clear()
{
Node* currentNode = head;
while (currentNode != nullptr)
{
Node* nextNode = currentNode->getNextNode();
delete currentNode;
currentNode = nextNode;
}
head = nullptr;
tail = nullptr;
length = 0;
}
void TileList::addFront(TileType tile)
{
head = new Node(tile, head);
if (tail == nullptr)
tail = head;
++length;
}
void TileList::addBack(TileType tile)
{
if (tail == nullptr)
{
addFront(tile);
}
else
{
tail->setNextNode(new Node(tile));
tail = tail->getNextNode();
++length;
}
}
TileType TileList::removeFront()
{
TileType removedTile = NOTILE;
if (head != nullptr)
{
Node* toRemove = head;
head = head->getNextNode();
if (head == nullptr || head->getNextNode() == nullptr)
tail = head;
removedTile = toRemove->getValue();
delete toRemove;
--length;
}
return removedTile;
}
TileType TileList::removeBack()
{
TileType removedTile = NOTILE;
if (head != tail)
{
Node* toRemove = tail;
Node* currentNode = head;
while (currentNode->getNextNode() != tail)
{
currentNode = currentNode->getNextNode();
}
tail = currentNode;
currentNode->setNextNode(nullptr);
removedTile = toRemove->getValue();
delete toRemove;
--length;
}
else removedTile = removeFront();
return removedTile;
}
std::string TileList::toString()
{
std::string result = "";
Node* currentNode = head;
while (currentNode != nullptr)
{
result += char(currentNode->getValue());
currentNode = currentNode->getNextNode();
}
return result;
}