-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDynamicList.h
186 lines (166 loc) · 3.1 KB
/
DynamicList.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
#pragma once
namespace AlgoGraph {
template<class T>
class DynamicList
{
private:
struct Item {
T value;
Item* next;
Item* prev;
};
int size;
Item* head;
Item* tail;
public:
DynamicList() : head(nullptr), tail(nullptr)
{
size = 0;
// start with an empty list (null head & tail)
}
~DynamicList()
{
// free all items in the list
while (head != nullptr) {
Item* temp = head;
head = head->next;
delete temp;
}
}
int getSize()
{
return size;
}
DynamicList(const DynamicList& other) : DynamicList()
{
// copy entire contents of other list to this one
Item* otherhead = other.head;
while (otherhead != nullptr) {
AddItemToTail(otherhead->value);
otherhead = otherhead->next;
}
}
void AddItemToHead(T value)
{
size++;
// create new item and set as head of list
Item* item = new Item;
item->value = value;
item->next = head;
item->prev = nullptr;
head = item;
// handle empty list
if (tail == nullptr)
tail = head;
}
void AddItemToTail(T value)
{
size++;
// create new item and set as tail of list
Item* item = new Item;
item->value = value;
item->next = nullptr;
if (head == nullptr)
{
head = tail = item;
item->prev = nullptr;
}
else
{
item->prev = tail;
tail->next = item;
tail = item;
}
}
T DeleteItemFromHead()
{
// remove head item from list
Item* temp = head;
head = head->next;
head->prev = nullptr;
// handle empty list
if (head == temp)
tail = nullptr;
// return value of removed item
T value = temp->value;
delete temp;
return value;
}
T DeleteItemFromTail()
{
// remove tail item from list
Item* temp = tail;
tail = tail->prev;
tail->next = nullptr;
// handle empty list
if (head == temp)
head = nullptr;
// return value of removed item
T value = temp->value;
delete temp;
return value;
}
T GetValueOfHead() const
{
return head->value;
}
T GetValueOfTail() const
{
return tail->value;
}
T* ReturnRefrenceToItemByValue(T value)
{
Item* current = head;
while(current!=nullptr)
{
if(current->value == value)
return &(current->value);
else current = current->next;
}
return nullptr;
}
void DeleteItemByValue(T value)
{
if(head->value == value)
{
DeleteItemFromHead();
return;
}
if(tail->value == value)
{
DeleteItemFromTail();
return;
}
Item* prev = head;
Item* current = prev->next;
while(current != nullptr)
{
if(current->value == value)
{
prev->next = current->next;
}
else
{
current = current-> next;
prev = prev->next;
}
}
delete current;
}
//Not in use for us but dont understand how it works.
T& GetItemByIndex(int i_IndexOfItemInList)
{
return const_cast<T&>(getItem(i_IndexOfItemInList));
}
const T& getItem(int idx) const
{
// scan the list and reutrn item at index idx
Item* temp = head;
for (int i = 0; i < idx; ++i)
{
temp = temp->next;
}
return temp->value;
}
};
}