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