]>
Commit | Line | Data |
---|---|---|
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 | ||
9 | template <class T> | |
10 | class List { | |
11 | public: | |
12 | List(); //constructor | |
13 | ~List(); //deconstructor | |
14 | void insertAtFront( const T & ); | |
15 | void insertAtBack( T *&); | |
16 | bool removeFromFront( T & ); | |
17 | bool removeFromBack( T & ); | |
18 | bool remove( T * ); | |
19 | bool isEmpty() const; | |
20 | void print() const; | |
21 | ListNode < T > *First() { return firstPtr; }; | |
22 | ListNode < T > *Last() { return lastPtr; }; | |
23 | private: | |
24 | ListNode < T > *firstPtr; | |
25 | ListNode < T > *lastPtr; | |
26 | ||
27 | ListNode < T > *getNewNode ( const T & ); | |
28 | }; | |
29 | ||
30 | template <class T> | |
31 | List<T>::List() : firstPtr (0), lastPtr (0) {} | |
32 | ||
33 | template <class T> | |
34 | List<T>::~List() | |
35 | { | |
36 | if (!isEmpty()) | |
37 | { | |
38 | cout << "Destroying Nodes" << endl; | |
39 | ||
40 | ListNode<T> *currentPtr = firstPtr, *tempPtr; | |
41 | ||
42 | while (currentPtr != 0) | |
43 | { | |
44 | tempPtr = currentPtr; | |
45 | currentPtr = currentPtr->next; | |
46 | // if (!tempPtr->getData()->stats || tempPtr->getData()->stats->started == 0) | |
47 | delete tempPtr; | |
48 | // else | |
49 | // tempPtr->getData()->stats->started = 0; | |
50 | } | |
51 | cout << "All Nodes destroyed" << endl; | |
52 | } | |
53 | } | |
54 | ||
55 | template<class T> | |
56 | void List<T>::insertAtFront( const T &value ) | |
57 | { | |
58 | ListNode<T> *newPtr = getNewNode ( value ); | |
59 | ||
60 | ||
61 | if (isEmpty()) | |
62 | firstPtr = lastPtr = newPtr; | |
63 | else | |
64 | { | |
65 | newPtr->Next = firstPtr; | |
66 | firstPtr->prev = newPtr; | |
67 | firstPtr = newPtr; | |
68 | } | |
69 | } | |
70 | ||
71 | template<class T> | |
72 | void List<T>::insertAtBack(T *&value ) | |
73 | { | |
74 | ListNode<T> *newPtr = getNewNode(*value); | |
75 | ||
76 | if (isEmpty()) | |
77 | { | |
78 | firstPtr = lastPtr = newPtr; | |
79 | } | |
80 | else | |
81 | { | |
82 | newPtr->prev = lastPtr; | |
83 | lastPtr->next = newPtr; | |
84 | lastPtr = newPtr; | |
85 | } | |
86 | } | |
87 | ||
88 | ||
89 | template <class T> | |
90 | bool List<T>::removeFromFront( T &value ) | |
91 | { | |
92 | if ( isEmpty()) | |
93 | return false; | |
94 | else | |
95 | { | |
96 | ListNode<T> *tempPtr = firstPtr; | |
97 | ||
98 | if ( firstPtr == lastPtr ) | |
99 | firstPtr = lastPtr = 0; | |
100 | else | |
101 | firstPtr = firstPtr->next; | |
102 | ||
103 | value = tempPtr->getData(); | |
104 | delete tempPtr; | |
105 | return true; | |
106 | } | |
107 | } | |
108 | ||
109 | template <class T> | |
110 | bool List<T>::removeFromBack( T &value ) | |
111 | { | |
112 | if ( isEmpty() ) | |
113 | return false; | |
114 | else | |
115 | { | |
116 | ListNode<T> *tempPtr = lastPtr; | |
117 | ||
118 | if ( firstPtr == lastPtr ) | |
119 | lastPtr = firstPtr = 0; | |
120 | else | |
121 | lastPtr = lastPtr->prev; | |
122 | ||
123 | value = tempPtr->getData(); | |
124 | delete tempPtr; | |
125 | return true; | |
126 | ||
127 | } | |
128 | } | |
129 | ||
130 | template <class T> | |
131 | bool List<T>::isEmpty() const | |
132 | { return firstPtr == 0; } | |
133 | ||
134 | template <class T> | |
135 | ListNode<T> *List<T>::getNewNode( const T &value) | |
136 | { | |
137 | ListNode<T> *ptr = new ListNode<T>(value); | |
138 | ||
139 | assert( ptr != 0); | |
140 | ||
141 | return ptr; | |
142 | } | |
143 | ||
144 | template <class T> | |
145 | void List<T>::print() const | |
146 | { | |
147 | if (isEmpty()) | |
148 | { | |
149 | cout << "Empty list" << endl; | |
150 | return; | |
151 | } | |
152 | ||
153 | ListNode<T> *currentPtr; | |
154 | currentPtr = firstPtr; | |
155 | while (currentPtr) | |
156 | { | |
157 | cout << "aClient: " << *currentPtr->getData(); | |
158 | ||
159 | if (currentPtr->getData()->stats) | |
160 | cout << " Player Name:" << currentPtr->getData()->stats->name; | |
161 | cout << endl; | |
162 | currentPtr = currentPtr->next; | |
163 | } | |
164 | ||
165 | cout << endl; | |
166 | } | |
167 | ||
168 | template <class T> | |
169 | bool List<T>::remove(T *remPtr) | |
170 | { | |
171 | ListNode<T> *newPtr = firstPtr; | |
172 | T *testPtr; | |
173 | ||
174 | while (newPtr) | |
175 | { | |
176 | testPtr = newPtr->getData(); | |
177 | if (testPtr == remPtr) | |
178 | { | |
179 | if (firstPtr == lastPtr) | |
180 | { | |
181 | firstPtr = lastPtr = 0; | |
182 | delete newPtr; | |
183 | return true; | |
184 | } | |
185 | else if (newPtr != lastPtr && newPtr != firstPtr) | |
186 | { | |
187 | newPtr->prev->next = newPtr->next; | |
188 | newPtr->next->prev = newPtr->prev; | |
189 | delete newPtr; | |
190 | return true; | |
191 | } | |
192 | else if (newPtr == lastPtr) | |
193 | { | |
194 | lastPtr = newPtr->prev; | |
195 | lastPtr->next = 0; | |
196 | delete newPtr; | |
197 | return true; | |
198 | } | |
199 | else if (newPtr == firstPtr) | |
200 | { | |
201 | firstPtr = newPtr->next; | |
202 | firstPtr->prev = 0; | |
203 | delete newPtr; | |
204 | return true; | |
205 | } | |
206 | } | |
207 | newPtr = newPtr->next; | |
208 | } | |
209 | return false; | |
210 | } | |
211 | #endif |