]>
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 *&); | |
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 |
31 | ||
1cf88153 | 32 | ListNode < T > *Find( T * ); |
85ce9d3e | 33 | ListNode < T > *First() { return firstPtr; }; |
34 | ListNode < T > *Last() { return lastPtr; }; | |
35 | private: | |
36 | ListNode < T > *firstPtr; | |
37 | ListNode < T > *lastPtr; | |
38 | ||
39 | ListNode < T > *getNewNode ( const T & ); | |
40 | }; | |
41 | ||
42 | template <class T> | |
43 | List<T>::List() : firstPtr (0), lastPtr (0) {} | |
44 | ||
45 | template <class T> | |
46 | List<T>::~List() | |
47 | { | |
48 | if (!isEmpty()) | |
49 | { | |
9f8c2acc | 50 | #ifdef DEBUGMODE |
51 | log("Destroying Nodes"); | |
52 | #endif | |
85ce9d3e | 53 | |
54 | ListNode<T> *currentPtr = firstPtr, *tempPtr; | |
55 | ||
0a1518fa | 56 | while (currentPtr) |
85ce9d3e | 57 | { |
58 | tempPtr = currentPtr; | |
0a1518fa | 59 | currentPtr = currentPtr->Next(); |
9f8c2acc | 60 | |
61 | #ifdef DEBUGMODE | |
62 | log("Deleting Memory address: %s", tempPtr->getData()); | |
63 | #endif | |
0a1518fa | 64 | |
85ce9d3e | 65 | delete tempPtr; |
85ce9d3e | 66 | } |
9f8c2acc | 67 | #ifdef DEBUGMODE |
68 | log("All Nodes destroyed"); | |
69 | #endif | |
85ce9d3e | 70 | } |
71 | } | |
72 | ||
73 | template<class T> | |
448a1531 | 74 | T *List<T>::insertAtFront( const T &value ) |
85ce9d3e | 75 | { |
76 | ListNode<T> *newPtr = getNewNode ( value ); | |
77 | ||
78 | ||
79 | if (isEmpty()) | |
80 | firstPtr = lastPtr = newPtr; | |
81 | else | |
82 | { | |
83 | newPtr->Next = firstPtr; | |
84 | firstPtr->prev = newPtr; | |
85 | firstPtr = newPtr; | |
86 | } | |
448a1531 | 87 | return firstPtr->getData(); |
85ce9d3e | 88 | } |
89 | ||
90 | template<class T> | |
448a1531 | 91 | T *List<T>::insertAtBack(T *&value ) |
85ce9d3e | 92 | { |
93 | ListNode<T> *newPtr = getNewNode(*value); | |
94 | ||
95 | if (isEmpty()) | |
96 | { | |
97 | firstPtr = lastPtr = newPtr; | |
98 | } | |
99 | else | |
100 | { | |
101 | newPtr->prev = lastPtr; | |
102 | lastPtr->next = newPtr; | |
103 | lastPtr = newPtr; | |
104 | } | |
448a1531 | 105 | return lastPtr->getData(); |
85ce9d3e | 106 | } |
107 | ||
ae2685f6 | 108 | template<class T> |
109 | ListNode<T> *List<T>::insertAtBack_RLN(T *&value ) | |
110 | { | |
111 | ListNode<T> *newPtr = getNewNode(*value); | |
112 | ||
113 | if (isEmpty()) | |
114 | { | |
115 | firstPtr = lastPtr = newPtr; | |
116 | } | |
117 | else | |
118 | { | |
119 | newPtr->prev = lastPtr; | |
120 | lastPtr->next = newPtr; | |
121 | lastPtr = newPtr; | |
122 | } | |
123 | return lastPtr; | |
124 | } | |
125 | ||
85ce9d3e | 126 | |
127 | template <class T> | |
128 | bool List<T>::removeFromFront( T &value ) | |
129 | { | |
130 | if ( isEmpty()) | |
131 | return false; | |
132 | else | |
133 | { | |
134 | ListNode<T> *tempPtr = firstPtr; | |
135 | ||
136 | if ( firstPtr == lastPtr ) | |
137 | firstPtr = lastPtr = 0; | |
138 | else | |
139 | firstPtr = firstPtr->next; | |
140 | ||
141 | value = tempPtr->getData(); | |
142 | delete tempPtr; | |
143 | return true; | |
144 | } | |
145 | } | |
146 | ||
147 | template <class T> | |
148 | bool List<T>::removeFromBack( T &value ) | |
149 | { | |
150 | if ( isEmpty() ) | |
151 | return false; | |
152 | else | |
153 | { | |
154 | ListNode<T> *tempPtr = lastPtr; | |
155 | ||
156 | if ( firstPtr == lastPtr ) | |
157 | lastPtr = firstPtr = 0; | |
158 | else | |
159 | lastPtr = lastPtr->prev; | |
160 | ||
161 | value = tempPtr->getData(); | |
162 | delete tempPtr; | |
163 | return true; | |
164 | ||
165 | } | |
166 | } | |
167 | ||
168 | template <class T> | |
169 | bool List<T>::isEmpty() const | |
0a1518fa | 170 | { return firstPtr == 0 && lastPtr == 0; } |
85ce9d3e | 171 | |
172 | template <class T> | |
173 | ListNode<T> *List<T>::getNewNode( const T &value) | |
174 | { | |
175 | ListNode<T> *ptr = new ListNode<T>(value); | |
176 | ||
177 | assert( ptr != 0); | |
178 | ||
179 | return ptr; | |
180 | } | |
1cf88153 | 181 | template<class T> |
182 | ListNode<T> *List<T>::Find( T *value ) | |
183 | { | |
184 | if (isEmpty()) {return NULL;} | |
185 | ||
186 | ListNode<T> *currentPtr; | |
187 | currentPtr = firstPtr; | |
188 | while (currentPtr) | |
189 | { | |
ae2685f6 | 190 | cout << currentPtr->getData() << endl << value << endl; |
1cf88153 | 191 | if (currentPtr->getData() == value) |
192 | return currentPtr; | |
193 | currentPtr = currentPtr->Next(); | |
194 | } | |
195 | return NULL; | |
196 | } | |
85ce9d3e | 197 | |
9f8c2acc | 198 | #ifdef DEBUGMODE |
85ce9d3e | 199 | template <class T> |
200 | void List<T>::print() const | |
201 | { | |
202 | if (isEmpty()) | |
203 | { | |
204 | cout << "Empty list" << endl; | |
205 | return; | |
206 | } | |
207 | ||
208 | ListNode<T> *currentPtr; | |
209 | currentPtr = firstPtr; | |
210 | while (currentPtr) | |
211 | { | |
87137def | 212 | /* |
cdc9a6db | 213 | cout << "aClient: " << *currentPtr->getData() << flush; |
85ce9d3e | 214 | |
215 | if (currentPtr->getData()->stats) | |
cdc9a6db | 216 | cout << " Player Name: " << currentPtr->getData()->stats->name |
217 | << " Password: " << currentPtr->getData()->stats->password << flush; | |
85ce9d3e | 218 | cout << endl; |
87137def | 219 | */ |
220 | cout << currentPtr->getData()->getString() << endl; | |
85ce9d3e | 221 | currentPtr = currentPtr->next; |
222 | } | |
85ce9d3e | 223 | } |
9f8c2acc | 224 | #endif |
85ce9d3e | 225 | |
226 | template <class T> | |
59dc3990 | 227 | bool List<T>::del(T *remPtr) |
228 | { | |
229 | ListNode<T> *removed = remove( remPtr ); | |
230 | if (removed != NULL) | |
231 | { | |
232 | delete removed; | |
233 | return true; | |
234 | } | |
235 | return false; | |
236 | } | |
237 | ||
238 | template <class T> | |
239 | ListNode<T> *List<T>::remove(T *remPtr) | |
85ce9d3e | 240 | { |
241 | ListNode<T> *newPtr = firstPtr; | |
242 | T *testPtr; | |
243 | ||
0a1518fa | 244 | if (isEmpty()) |
245 | return false; | |
246 | ||
85ce9d3e | 247 | while (newPtr) |
248 | { | |
249 | testPtr = newPtr->getData(); | |
250 | if (testPtr == remPtr) | |
251 | { | |
252 | if (firstPtr == lastPtr) | |
253 | { | |
9f8c2acc | 254 | #ifdef DEBUGMODE |
255 | log("One Element. Deleting it"); | |
256 | #endif | |
0a1518fa | 257 | firstPtr = lastPtr = NULL; |
59dc3990 | 258 | return newPtr; |
85ce9d3e | 259 | } |
260 | else if (newPtr != lastPtr && newPtr != firstPtr) | |
261 | { | |
9f8c2acc | 262 | #ifdef DEBUGMODE |
263 | log("Many elements, this one is in the middle. Deleting it, linking front to back, and back to front."); | |
264 | #endif | |
85ce9d3e | 265 | newPtr->prev->next = newPtr->next; |
266 | newPtr->next->prev = newPtr->prev; | |
59dc3990 | 267 | return newPtr; |
85ce9d3e | 268 | } |
269 | else if (newPtr == lastPtr) | |
270 | { | |
9f8c2acc | 271 | #ifdef DEBUGMODE |
272 | log("This was the last element. Deleting it, and pointing the tail to its previous element."); | |
273 | #endif | |
85ce9d3e | 274 | lastPtr = newPtr->prev; |
275 | lastPtr->next = 0; | |
59dc3990 | 276 | return newPtr; |
85ce9d3e | 277 | } |
278 | else if (newPtr == firstPtr) | |
279 | { | |
9f8c2acc | 280 | #ifdef DEBUGMODE |
281 | log("This was the first element. Deleting it, and pointing the head to its next element."); | |
282 | #endif | |
85ce9d3e | 283 | firstPtr = newPtr->next; |
284 | firstPtr->prev = 0; | |
59dc3990 | 285 | return newPtr; |
85ce9d3e | 286 | } |
287 | } | |
288 | newPtr = newPtr->next; | |
289 | } | |
59dc3990 | 290 | return NULL; |
85ce9d3e | 291 | } |
292 | #endif |