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