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