-
Notifications
You must be signed in to change notification settings - Fork 9
/
list.h
328 lines (274 loc) · 8.62 KB
/
list.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
//
// FoXBot - AI Bot for Halflife's Team Fortress Classic
// Doubly Linked List Data Structure
//
// (http://foxbot.net)
//
// list.h
//
// Copyright (C) 2003 - Tom "Redfox" Simpson
//
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License for more details at:
// http://www.gnu.org/copyleft/gpl.html
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
#ifndef LIST_H
#define LIST_H
// forward reference to the LIter class
template <typename Type> class LIter;
template <typename Type> class List {
// allow LIter to be a friend class
friend class LIter<Type>;
protected:
struct Node {
Type element; // one cell of data to store
Node *prev, *next; // previous and next elements
};
// The total number of elements.
int total;
// Access to the left and right ends of the list.
Node *head, *tail;
public:
// Default constructor
List() : total(0), head(nullptr), tail(nullptr) {}
// Destructor
~List() {
clear();
total = 0;
}
// Copy constructor
List(const List &other) : total(0), head(nullptr), tail(nullptr) {
for (Node *node = other.head; node != nullptr; node = node->next) {
addTail(node->element);
}
}
// Copy assignment operator using copy-and-swap idiom
List &operator=(List other) {
swap(*this, other);
return *this;
}
// Move constructor
List(List &&other) noexcept : total(other.total), head(other.head), tail(other.tail) {
other.head = nullptr;
other.tail = nullptr;
other.total = 0;
}
// Move assignment operator
List &operator=(List &&other) noexcept {
if (this != &other) {
clear();
head = other.head;
tail = other.tail;
total = other.total;
other.head = nullptr;
other.tail = nullptr;
other.total = 0;
}
return *this;
}
// Swap function
friend void swap(List &first, List &second) noexcept {
using std::swap;
swap(first.head, second.head);
swap(first.tail, second.tail);
swap(first.total, second.total);
}
// algorithms
////////////////////
// isEmpty : test if list is empty
bool isEmpty() { return (head == nullptr); }
// add : insert a new node to the beginning / end
void addHead(const Type &v);
void addTail(const Type &v);
// insert / remove : place or remove an item
// in the list at given position
void insert(const Type &v, LIter<Type> &loc);
void remove(LIter<Type> &loc);
// clear : remove all nodes from the list
void clear();
// size : return the number of elements we have.
int size() const;
};
// addHead : create a new item and attach it to
// the list at the head
//
template <typename Type> void List<Type>::addHead(const Type& v) {
// create node and store the value in it.
Node* n = new Node;
n->element = v;
// set prev to NULL, since it will be the new head.
n->prev = NULL;
// attach to the head, and update the head ptr.
n->next = head;
if (head != nullptr)
head->prev = n;
head = n;
total++;
// special case : set tail pointer to n if node is only one
// in the list.
if (tail == nullptr)
tail = n;
}
// addTail : create a new item and attach it to
// the list at the tail
//
template <typename Type> void List<Type>::addTail(const Type& v) {
// create node and store the value in it.
Node* n = new Node;
n->element = v;
// set next to NULL, since it will be the new tail.
n->next = NULL;
// attach to the end of tail (if there is one)
// and update the tail ptr.
if (tail != nullptr)
tail->next = n;
n->prev = tail;
tail = n;
total++;
// special case : set head pointer to n if node is only one
// in the list.
if (head == nullptr)
head = n;
}
// clear : delete all nodes and empty the list
//
template <typename Type> void List<Type>::clear() {
// delete head node over and over again
// until list is empty
while (!isEmpty()) {
const Node* n = head;
head = head->next;
delete n;
}
total = 0;
// set tail to NULL, since it doesn't get
// changed via the code above.
tail = NULL;
}
template <typename Type> int List<Type>::size() const { return total; }
// LIter class defines an iterator for the List class.
//
template <typename Type> class LIter {
// allow List to be a friend class
friend class List<Type>;
private:
// Keep track of the list this iterator is working with
List<Type>* list;
// Also keep track of the current node.
// template <typename Type>
typename List<Type>::Node* currentNode;
public:
// ctr : initialize the iterator class by specifying the list
// it will be used with.
explicit LIter(List<Type>* listToUse) : currentNode(nullptr) { list = listToUse; }
// begin : start the iterator at the head or tail
// of the list to start an iteration.
//
void begin() { currentNode = list->head; }
void beginReverse() { currentNode = list->tail; }
// end : test to see if the iterator has finished going
// through all of the nodes
bool end() { return (currentNode == nullptr); }
// ++operator : increment the current node.
//
// Note : prefix use only (++iter). Postfix not supported.
LIter<Type>& operator++() {
if (currentNode != nullptr)
currentNode = currentNode->next;
return *this;
}
// --operator : decrement the current node.
//
// Note : prefix use only (--iter). Postfix not supported.
LIter<Type>& operator--() {
if (currentNode != nullptr)
currentNode = currentNode->prev;
return *this;
}
// current : return the address of the current item.
//
Type* current() {
if (currentNode == nullptr)
return nullptr;
return &(currentNode->element);
}
};
// insert : place an item at the given iterator pos.
//
// Note : after insert, iterator will be positioned
// on top of the node following the new node.
template <typename Type> void List<Type>::insert(const Type& v, LIter<Type>& loc) {
// store a tmp. ptr to node in the location where
// insert will occur.
Node* nextNode = loc.currentNode;
// inserting on a NULL location (beyond ends of the list)
// is invalid, simply return -> addHead or addTail should be called.
if (nextNode == nullptr)
return;
// create the new node and fill it.
Node* newNode = new Node;
newNode->element = v;
// get a ptr to the node before the current. It will
// end up being the prev node to the new one.
Node* prevNode = nextNode->prev;
// attach the new node into the list.
// connections between prevNode and newNode
if (prevNode != nullptr)
prevNode->next = newNode;
newNode->prev = prevNode;
// connections between newNode and nextNode
newNode->next = nextNode;
nextNode->prev = newNode;
total++;
// special case : handle adding to the beginning of the
// list. If prevNode is NULL, newNode is the head.
if (prevNode == nullptr)
head = newNode;
// fix the iterator to make it point to the new location
loc.currentNode = newNode->next;
}
// remove : remove an item at the given iterator pos.
//
// Note : after remove, iterator will be positioned
// on the node which directly followed the given
// node (or NULL if the node was the tail)
template <typename Type> void List<Type>::remove(LIter<Type>& loc) {
// store a tmp. ptr to node in the location where
// remove will occur.
Node* deletedNode = loc.currentNode;
// attach nodes to the left and right of this node
// to each other. Also note that there may not
// be nodes to the right and left.
Node* leftNode = deletedNode->prev;
Node* rightNode = deletedNode->next;
if (leftNode != nullptr)
leftNode->next = rightNode;
if (rightNode != nullptr)
rightNode->prev = leftNode;
// special cases : we are possibly deleting
// the head and/or tail of the list. Handle
// these cases by updating those pointers if necessary.
if (deletedNode == head)
head = deletedNode->next;
if (deletedNode == tail)
tail = deletedNode->prev;
total--;
// update the iterator to the node after this one.
loc.currentNode = deletedNode->next;
// delete the memory that this node was taking up
delete deletedNode;
}
#endif // LIST_H