]>
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 "extern.h" | |
9 | #include "options.h" | |
10 | ||
11 | using std::cout; | |
12 | using std::endl; | |
13 | using std::flush; | |
14 | ||
15 | template <class T> | |
16 | class List { | |
17 | public: | |
18 | List(); //constructor | |
19 | ~List(); //deconstructor | |
20 | T *insertAtFront( const T & ); | |
21 | T *insertAtBack( T *&); | |
22 | ListNode<T> *insertAtBack_RLN( T *&); | |
23 | bool removeFromFront( T & ); | |
24 | bool removeFromBack( T & ); | |
25 | bool del( T *); | |
26 | ListNode < T > *remove( T * ); | |
27 | bool isEmpty() const; | |
28 | #ifdef DEBUGMODE | |
29 | void print() const; | |
30 | #endif | |
31 | ||
32 | ListNode < T > *Find( T * ); | |
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 | { | |
50 | #ifdef DEBUGMODE | |
51 | log("Destroying Nodes"); | |
52 | #endif | |
53 | ||
54 | ListNode<T> *currentPtr = firstPtr, *tempPtr; | |
55 | ||
56 | while (currentPtr) | |
57 | { | |
58 | tempPtr = currentPtr; | |
59 | currentPtr = currentPtr->Next(); | |
60 | ||
61 | #ifdef DEBUGMODE | |
62 | log("Deleting Memory address: %s", tempPtr->getData()); | |
63 | #endif | |
64 | ||
65 | delete tempPtr; | |
66 | } | |
67 | #ifdef DEBUGMODE | |
68 | log("All Nodes destroyed"); | |
69 | #endif | |
70 | } | |
71 | } | |
72 | ||
73 | template<class T> | |
74 | T *List<T>::insertAtFront( const T &value ) | |
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 | } | |
87 | return firstPtr->getData(); | |
88 | } | |
89 | ||
90 | template<class T> | |
91 | T *List<T>::insertAtBack(T *&value ) | |
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 | } | |
105 | return lastPtr->getData(); | |
106 | } | |
107 | ||
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 | ||
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 | |
170 | { return firstPtr == 0 && lastPtr == 0; } | |
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 | } | |
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 | { | |
190 | cout << currentPtr->getData() << endl << value << endl; | |
191 | if (currentPtr->getData() == value) | |
192 | return currentPtr; | |
193 | currentPtr = currentPtr->Next(); | |
194 | } | |
195 | return NULL; | |
196 | } | |
197 | ||
198 | #ifdef DEBUGMODE | |
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 | { | |
212 | /* | |
213 | cout << "aClient: " << *currentPtr->getData() << flush; | |
214 | ||
215 | if (currentPtr->getData()->stats) | |
216 | cout << " Player Name: " << currentPtr->getData()->stats->name | |
217 | << " Password: " << currentPtr->getData()->stats->password << flush; | |
218 | cout << endl; | |
219 | */ | |
220 | cout << currentPtr->getData()->getString() << endl; | |
221 | currentPtr = currentPtr->next; | |
222 | } | |
223 | } | |
224 | #endif | |
225 | ||
226 | template <class T> | |
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) | |
240 | { | |
241 | ListNode<T> *newPtr = firstPtr; | |
242 | T *testPtr; | |
243 | ||
244 | if (isEmpty()) | |
245 | return false; | |
246 | ||
247 | while (newPtr) | |
248 | { | |
249 | testPtr = newPtr->getData(); | |
250 | if (testPtr == remPtr) | |
251 | { | |
252 | if (firstPtr == lastPtr) | |
253 | { | |
254 | #ifdef DEBUGMODE | |
255 | log("One Element. Deleting it"); | |
256 | #endif | |
257 | firstPtr = lastPtr = NULL; | |
258 | return newPtr; | |
259 | } | |
260 | else if (newPtr != lastPtr && newPtr != firstPtr) | |
261 | { | |
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 | |
265 | newPtr->prev->next = newPtr->next; | |
266 | newPtr->next->prev = newPtr->prev; | |
267 | return newPtr; | |
268 | } | |
269 | else if (newPtr == lastPtr) | |
270 | { | |
271 | #ifdef DEBUGMODE | |
272 | log("This was the last element. Deleting it, and pointing the tail to its previous element."); | |
273 | #endif | |
274 | lastPtr = newPtr->prev; | |
275 | lastPtr->next = 0; | |
276 | return newPtr; | |
277 | } | |
278 | else if (newPtr == firstPtr) | |
279 | { | |
280 | #ifdef DEBUGMODE | |
281 | log("This was the first element. Deleting it, and pointing the head to its next element."); | |
282 | #endif | |
283 | firstPtr = newPtr->next; | |
284 | firstPtr->prev = 0; | |
285 | return newPtr; | |
286 | } | |
287 | } | |
288 | newPtr = newPtr->next; | |
289 | } | |
290 | return NULL; | |
291 | } | |
292 | #endif |