]>
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 *&); | |
85ce9d3e | 22 | bool removeFromFront( T & ); |
23 | bool removeFromBack( T & ); | |
59dc3990 | 24 | bool del( T *); |
25 | ListNode < T > *remove( T * ); | |
85ce9d3e | 26 | bool isEmpty() const; |
27 | void print() const; | |
1cf88153 | 28 | ListNode < T > *Find( T * ); |
85ce9d3e | 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 | { | |
9f8c2acc | 46 | #ifdef DEBUGMODE |
47 | log("Destroying Nodes"); | |
48 | #endif | |
85ce9d3e | 49 | |
50 | ListNode<T> *currentPtr = firstPtr, *tempPtr; | |
51 | ||
0a1518fa | 52 | while (currentPtr) |
85ce9d3e | 53 | { |
54 | tempPtr = currentPtr; | |
0a1518fa | 55 | currentPtr = currentPtr->Next(); |
9f8c2acc | 56 | |
57 | #ifdef DEBUGMODE | |
58 | log("Deleting Memory address: %s", tempPtr->getData()); | |
59 | #endif | |
0a1518fa | 60 | |
85ce9d3e | 61 | delete tempPtr; |
85ce9d3e | 62 | } |
9f8c2acc | 63 | #ifdef DEBUGMODE |
64 | log("All Nodes destroyed"); | |
65 | #endif | |
85ce9d3e | 66 | } |
67 | } | |
68 | ||
69 | template<class T> | |
448a1531 | 70 | T *List<T>::insertAtFront( const T &value ) |
85ce9d3e | 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 | } | |
448a1531 | 83 | return firstPtr->getData(); |
85ce9d3e | 84 | } |
85 | ||
86 | template<class T> | |
448a1531 | 87 | T *List<T>::insertAtBack(T *&value ) |
85ce9d3e | 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 | } | |
448a1531 | 101 | return lastPtr->getData(); |
85ce9d3e | 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 | |
0a1518fa | 148 | { return firstPtr == 0 && lastPtr == 0; } |
85ce9d3e | 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 | } | |
1cf88153 | 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 | } | |
85ce9d3e | 174 | |
9f8c2acc | 175 | #ifdef DEBUGMODE |
85ce9d3e | 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 | { | |
cdc9a6db | 189 | cout << "aClient: " << *currentPtr->getData() << flush; |
85ce9d3e | 190 | |
191 | if (currentPtr->getData()->stats) | |
cdc9a6db | 192 | cout << " Player Name: " << currentPtr->getData()->stats->name |
193 | << " Password: " << currentPtr->getData()->stats->password << flush; | |
85ce9d3e | 194 | cout << endl; |
195 | currentPtr = currentPtr->next; | |
196 | } | |
85ce9d3e | 197 | } |
9f8c2acc | 198 | #endif |
85ce9d3e | 199 | |
200 | template <class T> | |
59dc3990 | 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) | |
85ce9d3e | 214 | { |
215 | ListNode<T> *newPtr = firstPtr; | |
216 | T *testPtr; | |
217 | ||
0a1518fa | 218 | if (isEmpty()) |
219 | return false; | |
220 | ||
85ce9d3e | 221 | while (newPtr) |
222 | { | |
223 | testPtr = newPtr->getData(); | |
224 | if (testPtr == remPtr) | |
225 | { | |
226 | if (firstPtr == lastPtr) | |
227 | { | |
9f8c2acc | 228 | #ifdef DEBUGMODE |
229 | log("One Element. Deleting it"); | |
230 | #endif | |
0a1518fa | 231 | firstPtr = lastPtr = NULL; |
59dc3990 | 232 | return newPtr; |
85ce9d3e | 233 | } |
234 | else if (newPtr != lastPtr && newPtr != firstPtr) | |
235 | { | |
9f8c2acc | 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 | |
85ce9d3e | 239 | newPtr->prev->next = newPtr->next; |
240 | newPtr->next->prev = newPtr->prev; | |
59dc3990 | 241 | return newPtr; |
85ce9d3e | 242 | } |
243 | else if (newPtr == lastPtr) | |
244 | { | |
9f8c2acc | 245 | #ifdef DEBUGMODE |
246 | log("This was the last element. Deleting it, and pointing the tail to its previous element."); | |
247 | #endif | |
85ce9d3e | 248 | lastPtr = newPtr->prev; |
249 | lastPtr->next = 0; | |
59dc3990 | 250 | return newPtr; |
85ce9d3e | 251 | } |
252 | else if (newPtr == firstPtr) | |
253 | { | |
9f8c2acc | 254 | #ifdef DEBUGMODE |
255 | log("This was the first element. Deleting it, and pointing the head to its next element."); | |
256 | #endif | |
85ce9d3e | 257 | firstPtr = newPtr->next; |
258 | firstPtr->prev = 0; | |
59dc3990 | 259 | return newPtr; |
85ce9d3e | 260 | } |
261 | } | |
262 | newPtr = newPtr->next; | |
263 | } | |
59dc3990 | 264 | return NULL; |
85ce9d3e | 265 | } |
266 | #endif |