]>
Commit | Line | Data |
---|---|---|
85ce9d3e | 1 | #ifndef LIST_H |
2 | #define LIST_H | |
3 | ||
fb37ecc7 | 4 | #include <iostream> |
85ce9d3e | 5 | #include <cassert> |
6 | #include "listnode.h" | |
7 | #include "aClient.h" | |
fc9d2643 | 8 | #include "options.h" |
9d057db5 | 9 | |
fb37ecc7 | 10 | using std::cout; |
11 | using std::endl; | |
fc9d2643 | 12 | using std::flush; |
fb37ecc7 | 13 | |
85ce9d3e | 14 | template <class T> |
15 | class List { | |
16 | public: | |
17 | List(); //constructor | |
18 | ~List(); //deconstructor | |
448a1531 | 19 | T *insertAtFront( const T & ); |
20 | T *insertAtBack( T *&); | |
bf3a2ff9 | 21 | T *random(); // Returns a random element in the list |
ae2685f6 | 22 | ListNode<T> *insertAtBack_RLN( T *&); |
85ce9d3e | 23 | bool removeFromFront( T & ); |
24 | bool removeFromBack( T & ); | |
59dc3990 | 25 | bool del( T *); |
26 | ListNode < T > *remove( T * ); | |
85ce9d3e | 27 | bool isEmpty() const; |
c260a8d7 | 28 | #ifdef DEBUGMODE |
85ce9d3e | 29 | void print() const; |
c260a8d7 | 30 | #endif |
5aa1d28d | 31 | void deleteNodes(); |
c260a8d7 | 32 | |
1cf88153 | 33 | ListNode < T > *Find( T * ); |
85ce9d3e | 34 | ListNode < T > *First() { return firstPtr; }; |
35 | ListNode < T > *Last() { return lastPtr; }; | |
bf3a2ff9 | 36 | int elements; // Number of elements in the list |
85ce9d3e | 37 | private: |
38 | ListNode < T > *firstPtr; | |
39 | ListNode < T > *lastPtr; | |
40 | ||
41 | ListNode < T > *getNewNode ( const T & ); | |
bf3a2ff9 | 42 | ListNode < T > *getNewNode ( T & ); |
85ce9d3e | 43 | }; |
44 | ||
45 | template <class T> | |
bf3a2ff9 | 46 | List<T>::List() : firstPtr (0), lastPtr (0) {elements = 0;} |
85ce9d3e | 47 | |
48 | template <class T> | |
5aa1d28d | 49 | void List<T>::deleteNodes() |
85ce9d3e | 50 | { |
5aa1d28d | 51 | if (isEmpty()) |
52 | return; | |
53 | ListNode<T> *it = firstPtr, *tempPtr; | |
54 | while (it) | |
85ce9d3e | 55 | { |
5aa1d28d | 56 | tempPtr = it; |
57 | it = it->Next(); | |
58 | delete tempPtr; | |
85ce9d3e | 59 | } |
3c13eb61 | 60 | firstPtr = NULL; |
61 | lastPtr = NULL; | |
bf3a2ff9 | 62 | elements = 0; |
85ce9d3e | 63 | } |
64 | ||
5aa1d28d | 65 | template <class T> |
66 | List<T>::~List() | |
67 | { | |
68 | deleteNodes(); | |
69 | } | |
70 | ||
bf3a2ff9 | 71 | template<class T> |
72 | T *List<T>::random() | |
73 | { | |
74 | if (isEmpty()) | |
75 | return NULL; | |
76 | ||
77 | int num = rand() % elements; | |
78 | ListNode<T> *it = firstPtr; | |
79 | for (int x = 0; x < num; x++) | |
80 | { | |
81 | it = it->Next(); | |
82 | } | |
83 | return it->getData(); | |
84 | } | |
85 | ||
85ce9d3e | 86 | template<class T> |
448a1531 | 87 | T *List<T>::insertAtFront( const T &value ) |
85ce9d3e | 88 | { |
89 | ListNode<T> *newPtr = getNewNode ( value ); | |
90 | ||
91 | ||
92 | if (isEmpty()) | |
93 | firstPtr = lastPtr = newPtr; | |
94 | else | |
95 | { | |
96 | newPtr->Next = firstPtr; | |
97 | firstPtr->prev = newPtr; | |
98 | firstPtr = newPtr; | |
99 | } | |
bf3a2ff9 | 100 | elements++; |
448a1531 | 101 | return firstPtr->getData(); |
85ce9d3e | 102 | } |
103 | ||
104 | template<class T> | |
448a1531 | 105 | T *List<T>::insertAtBack(T *&value ) |
85ce9d3e | 106 | { |
107 | ListNode<T> *newPtr = getNewNode(*value); | |
108 | ||
109 | if (isEmpty()) | |
110 | { | |
111 | firstPtr = lastPtr = newPtr; | |
112 | } | |
113 | else | |
114 | { | |
115 | newPtr->prev = lastPtr; | |
116 | lastPtr->next = newPtr; | |
117 | lastPtr = newPtr; | |
118 | } | |
bf3a2ff9 | 119 | elements++; |
448a1531 | 120 | return lastPtr->getData(); |
85ce9d3e | 121 | } |
122 | ||
ae2685f6 | 123 | template<class T> |
124 | ListNode<T> *List<T>::insertAtBack_RLN(T *&value ) | |
125 | { | |
126 | ListNode<T> *newPtr = getNewNode(*value); | |
127 | ||
128 | if (isEmpty()) | |
129 | { | |
130 | firstPtr = lastPtr = newPtr; | |
131 | } | |
132 | else | |
133 | { | |
134 | newPtr->prev = lastPtr; | |
135 | lastPtr->next = newPtr; | |
136 | lastPtr = newPtr; | |
137 | } | |
bf3a2ff9 | 138 | elements++; |
ae2685f6 | 139 | return lastPtr; |
140 | } | |
141 | ||
85ce9d3e | 142 | |
143 | template <class T> | |
144 | bool List<T>::removeFromFront( T &value ) | |
145 | { | |
146 | if ( isEmpty()) | |
147 | return false; | |
148 | else | |
149 | { | |
150 | ListNode<T> *tempPtr = firstPtr; | |
151 | ||
152 | if ( firstPtr == lastPtr ) | |
153 | firstPtr = lastPtr = 0; | |
154 | else | |
155 | firstPtr = firstPtr->next; | |
156 | ||
157 | value = tempPtr->getData(); | |
158 | delete tempPtr; | |
bf3a2ff9 | 159 | elements--; |
85ce9d3e | 160 | return true; |
161 | } | |
162 | } | |
163 | ||
164 | template <class T> | |
165 | bool List<T>::removeFromBack( T &value ) | |
166 | { | |
167 | if ( isEmpty() ) | |
168 | return false; | |
169 | else | |
170 | { | |
171 | ListNode<T> *tempPtr = lastPtr; | |
172 | ||
173 | if ( firstPtr == lastPtr ) | |
174 | lastPtr = firstPtr = 0; | |
175 | else | |
176 | lastPtr = lastPtr->prev; | |
177 | ||
178 | value = tempPtr->getData(); | |
179 | delete tempPtr; | |
bf3a2ff9 | 180 | elements--; |
85ce9d3e | 181 | return true; |
85ce9d3e | 182 | } |
183 | } | |
184 | ||
185 | template <class T> | |
186 | bool List<T>::isEmpty() const | |
0a1518fa | 187 | { return firstPtr == 0 && lastPtr == 0; } |
85ce9d3e | 188 | |
189 | template <class T> | |
190 | ListNode<T> *List<T>::getNewNode( const T &value) | |
191 | { | |
192 | ListNode<T> *ptr = new ListNode<T>(value); | |
193 | ||
194 | assert( ptr != 0); | |
195 | ||
196 | return ptr; | |
197 | } | |
bf3a2ff9 | 198 | |
199 | template <class T> | |
200 | ListNode<T> *List<T>::getNewNode( T &value) | |
201 | { | |
202 | ListNode<T> *ptr = new ListNode<T>(value); | |
203 | ||
204 | assert( ptr != 0); | |
205 | ||
206 | return ptr; | |
207 | } | |
208 | ||
1cf88153 | 209 | template<class T> |
210 | ListNode<T> *List<T>::Find( T *value ) | |
211 | { | |
212 | if (isEmpty()) {return NULL;} | |
213 | ||
214 | ListNode<T> *currentPtr; | |
215 | currentPtr = firstPtr; | |
216 | while (currentPtr) | |
217 | { | |
ae2685f6 | 218 | cout << currentPtr->getData() << endl << value << endl; |
1cf88153 | 219 | if (currentPtr->getData() == value) |
220 | return currentPtr; | |
221 | currentPtr = currentPtr->Next(); | |
222 | } | |
223 | return NULL; | |
224 | } | |
85ce9d3e | 225 | |
9f8c2acc | 226 | #ifdef DEBUGMODE |
85ce9d3e | 227 | template <class T> |
228 | void List<T>::print() const | |
229 | { | |
230 | if (isEmpty()) | |
231 | { | |
232 | cout << "Empty list" << endl; | |
233 | return; | |
234 | } | |
235 | ||
236 | ListNode<T> *currentPtr; | |
237 | currentPtr = firstPtr; | |
238 | while (currentPtr) | |
239 | { | |
87137def | 240 | /* |
cdc9a6db | 241 | cout << "aClient: " << *currentPtr->getData() << flush; |
85ce9d3e | 242 | |
243 | if (currentPtr->getData()->stats) | |
cdc9a6db | 244 | cout << " Player Name: " << currentPtr->getData()->stats->name |
245 | << " Password: " << currentPtr->getData()->stats->password << flush; | |
85ce9d3e | 246 | cout << endl; |
87137def | 247 | */ |
bf3a2ff9 | 248 | /* |
87137def | 249 | cout << currentPtr->getData()->getString() << endl; |
85ce9d3e | 250 | currentPtr = currentPtr->next; |
bf3a2ff9 | 251 | */ |
252 | T *dat; | |
253 | dat = currentPtr->getData(); | |
254 | log("name: %s", dat->name); | |
255 | log("weapon: %s", dat->weapon); | |
256 | log("death: %s", dat->death); | |
257 | log("strength: %d", dat->strength); | |
258 | log("exp: %ld", dat->exp); | |
259 | log("gold: %ld", dat->gold); | |
260 | log("maxhp: %d", dat->maxhp); | |
261 | log("hp: %d", dat->hp); | |
262 | currentPtr = currentPtr->next; | |
85ce9d3e | 263 | } |
85ce9d3e | 264 | } |
9f8c2acc | 265 | #endif |
85ce9d3e | 266 | |
267 | template <class T> | |
59dc3990 | 268 | bool List<T>::del(T *remPtr) |
269 | { | |
270 | ListNode<T> *removed = remove( remPtr ); | |
271 | if (removed != NULL) | |
272 | { | |
273 | delete removed; | |
274 | return true; | |
275 | } | |
276 | return false; | |
277 | } | |
278 | ||
279 | template <class T> | |
280 | ListNode<T> *List<T>::remove(T *remPtr) | |
85ce9d3e | 281 | { |
282 | ListNode<T> *newPtr = firstPtr; | |
283 | T *testPtr; | |
284 | ||
0a1518fa | 285 | if (isEmpty()) |
286 | return false; | |
287 | ||
85ce9d3e | 288 | while (newPtr) |
289 | { | |
290 | testPtr = newPtr->getData(); | |
291 | if (testPtr == remPtr) | |
292 | { | |
293 | if (firstPtr == lastPtr) | |
294 | { | |
9f8c2acc | 295 | #ifdef DEBUGMODE |
296 | log("One Element. Deleting it"); | |
297 | #endif | |
0a1518fa | 298 | firstPtr = lastPtr = NULL; |
59dc3990 | 299 | return newPtr; |
85ce9d3e | 300 | } |
301 | else if (newPtr != lastPtr && newPtr != firstPtr) | |
302 | { | |
9f8c2acc | 303 | #ifdef DEBUGMODE |
304 | log("Many elements, this one is in the middle. Deleting it, linking front to back, and back to front."); | |
305 | #endif | |
85ce9d3e | 306 | newPtr->prev->next = newPtr->next; |
307 | newPtr->next->prev = newPtr->prev; | |
59dc3990 | 308 | return newPtr; |
85ce9d3e | 309 | } |
310 | else if (newPtr == lastPtr) | |
311 | { | |
9f8c2acc | 312 | #ifdef DEBUGMODE |
313 | log("This was the last element. Deleting it, and pointing the tail to its previous element."); | |
314 | #endif | |
85ce9d3e | 315 | lastPtr = newPtr->prev; |
316 | lastPtr->next = 0; | |
59dc3990 | 317 | return newPtr; |
85ce9d3e | 318 | } |
319 | else if (newPtr == firstPtr) | |
320 | { | |
9f8c2acc | 321 | #ifdef DEBUGMODE |
322 | log("This was the first element. Deleting it, and pointing the head to its next element."); | |
323 | #endif | |
85ce9d3e | 324 | firstPtr = newPtr->next; |
325 | firstPtr->prev = 0; | |
59dc3990 | 326 | return newPtr; |
85ce9d3e | 327 | } |
bf3a2ff9 | 328 | elements--; |
85ce9d3e | 329 | } |
330 | newPtr = newPtr->next; | |
331 | } | |
59dc3990 | 332 | return NULL; |
85ce9d3e | 333 | } |
334 | #endif |