]>
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 | void print() const; | |
29 | ListNode < T > *Find( T * ); | |
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 | { | |
47 | #ifdef DEBUGMODE | |
48 | log("Destroying Nodes"); | |
49 | #endif | |
50 | ||
51 | ListNode<T> *currentPtr = firstPtr, *tempPtr; | |
52 | ||
53 | while (currentPtr) | |
54 | { | |
55 | tempPtr = currentPtr; | |
56 | currentPtr = currentPtr->Next(); | |
57 | ||
58 | #ifdef DEBUGMODE | |
59 | log("Deleting Memory address: %s", tempPtr->getData()); | |
60 | #endif | |
61 | ||
62 | delete tempPtr; | |
63 | } | |
64 | #ifdef DEBUGMODE | |
65 | log("All Nodes destroyed"); | |
66 | #endif | |
67 | } | |
68 | } | |
69 | ||
70 | template<class T> | |
71 | T *List<T>::insertAtFront( const T &value ) | |
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 | } | |
84 | return firstPtr->getData(); | |
85 | } | |
86 | ||
87 | template<class T> | |
88 | T *List<T>::insertAtBack(T *&value ) | |
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 | } | |
102 | return lastPtr->getData(); | |
103 | } | |
104 | ||
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 | ||
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 | |
167 | { return firstPtr == 0 && lastPtr == 0; } | |
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 | } | |
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 | { | |
187 | cout << currentPtr->getData() << endl << value << endl; | |
188 | if (currentPtr->getData() == value) | |
189 | return currentPtr; | |
190 | currentPtr = currentPtr->Next(); | |
191 | } | |
192 | return NULL; | |
193 | } | |
194 | ||
195 | #ifdef DEBUGMODE | |
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 | { | |
209 | cout << "aClient: " << *currentPtr->getData() << flush; | |
210 | ||
211 | if (currentPtr->getData()->stats) | |
212 | cout << " Player Name: " << currentPtr->getData()->stats->name | |
213 | << " Password: " << currentPtr->getData()->stats->password << flush; | |
214 | cout << endl; | |
215 | currentPtr = currentPtr->next; | |
216 | } | |
217 | } | |
218 | #endif | |
219 | ||
220 | template <class T> | |
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) | |
234 | { | |
235 | ListNode<T> *newPtr = firstPtr; | |
236 | T *testPtr; | |
237 | ||
238 | if (isEmpty()) | |
239 | return false; | |
240 | ||
241 | while (newPtr) | |
242 | { | |
243 | testPtr = newPtr->getData(); | |
244 | if (testPtr == remPtr) | |
245 | { | |
246 | if (firstPtr == lastPtr) | |
247 | { | |
248 | #ifdef DEBUGMODE | |
249 | log("One Element. Deleting it"); | |
250 | #endif | |
251 | firstPtr = lastPtr = NULL; | |
252 | return newPtr; | |
253 | } | |
254 | else if (newPtr != lastPtr && newPtr != firstPtr) | |
255 | { | |
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 | |
259 | newPtr->prev->next = newPtr->next; | |
260 | newPtr->next->prev = newPtr->prev; | |
261 | return newPtr; | |
262 | } | |
263 | else if (newPtr == lastPtr) | |
264 | { | |
265 | #ifdef DEBUGMODE | |
266 | log("This was the last element. Deleting it, and pointing the tail to its previous element."); | |
267 | #endif | |
268 | lastPtr = newPtr->prev; | |
269 | lastPtr->next = 0; | |
270 | return newPtr; | |
271 | } | |
272 | else if (newPtr == firstPtr) | |
273 | { | |
274 | #ifdef DEBUGMODE | |
275 | log("This was the first element. Deleting it, and pointing the head to its next element."); | |
276 | #endif | |
277 | firstPtr = newPtr->next; | |
278 | firstPtr->prev = 0; | |
279 | return newPtr; | |
280 | } | |
281 | } | |
282 | newPtr = newPtr->next; | |
283 | } | |
284 | return NULL; | |
285 | } | |
286 | #endif |