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