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