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