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