00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069 #ifndef KYRA_LIST_INCLUDED
00070 #define KYRA_LIST_INCLUDED
00071
00072 #include "gldebug.h"
00073
00074
00077 template <class T>
00078 struct GlSListNode
00079 {
00080 GlSListNode* next;
00081 T data;
00082 };
00083
00084
00089 template <class T>
00090 class GlSList
00091 {
00092 public:
00093 GlSList() { root = 0; }
00094 ~GlSList() { Clear(); }
00095
00097 int Size() const { return Count(); }
00099 int Count() const { GlSListNode<T>* node;
00100 int count = 0;
00101 for( node = root; node; node = node->next )
00102 ++count;
00103 return count;
00104 }
00106 bool Empty() const { return root == 0; }
00108 T& Front() const { return root->data; }
00110 GlSListNode<T>* FrontNode() const { return root; }
00111
00113 void Clear() { GlSListNode<T>* temp;
00114
00115 while( root )
00116 {
00117 temp = root;
00118 root = root->next;
00119 delete temp;
00120 }
00121 }
00122
00124 void PushFront( const T& insert )
00125 {
00126 GlSListNode<T>* node = new GlSListNode<T>;
00127 node->data = insert;
00128 node->next = root;
00129 root = node;
00130 }
00131
00133 void PushBack( const T& insert )
00134 {
00135 GlSListNode<T>* end;
00136 for ( end=root; end && end->next; end = end->next )
00137 {}
00138
00139 if ( !end )
00140 {
00141 PushFront( insert );
00142 }
00143 else
00144 {
00145 GlSListNode<T>* node = new GlSListNode<T>;
00146 node->data = insert;
00147 node->next = 0;
00148 end->next = node;
00149 }
00150 }
00151
00154 void PopFront()
00155 {
00156 if ( root )
00157 {
00158 GlSListNode<T>* temp = root->next;
00159 delete root;
00160 root = temp;
00161 }
00162 }
00163
00167 void Pop( const T& thisone )
00168 {
00169 GlSListNode<T>* node;
00170 GlSListNode<T>* prev;
00171
00172 for( node = root, prev = 0;
00173 node;
00174 prev = node, node = node->next )
00175 {
00176 if ( node->data == thisone )
00177 {
00178 if ( prev )
00179 {
00180 prev->next = node->next;
00181 }
00182 else
00183 {
00184 root = node->next;
00185 }
00186 delete node;
00187 break;
00188 }
00189 }
00190 }
00191
00193 GlSListNode<T>* Find( const T& findthis )
00194 {
00195 GlSListNode<T>* node;
00196 for( node=root; node; node=node->next )
00197 {
00198 if ( node->data == findthis )
00199 return node;
00200 }
00201 return 0;
00202 }
00203
00205 bool FindAndDelete( const T& findthis )
00206 {
00207 GlSListNode<T>* node;
00208 GlSListNode<T>* prev;
00209
00210 for( node=root, prev = 0;
00211 node;
00212 prev = node, node=node->next )
00213 {
00214 if ( node->data == findthis )
00215 {
00216 if ( prev )
00217 prev->next = node->next;
00218 else
00219 root = node->next;
00220 delete node;
00221 return true;
00222 }
00223 }
00224 return false;
00225 }
00226
00227
00228 private:
00229 GlSListNode<T>* root;
00230 GlSList( const GlSList& that );
00231 GlSList operator=( const GlSList& that );
00232 };
00233
00234
00237 template <class T>
00238 class GlSListIterator
00239 {
00240 public:
00241 GlSListIterator( const GlSList<T>& _list ) : current( 0 ), list( &_list ) {}
00242
00243 void Begin() { current = list->FrontNode(); }
00244 void Next() { current = current->next; }
00245 bool Done() { return ( current == 0 ); }
00246
00248 T& Current() { return (current->data); }
00249
00250 GlSListNode<T>* CurrentNode() { return current; }
00251
00252 private:
00253 GlSListNode<T>* current;
00254 const GlSList<T>* list;
00255 };
00256
00257
00258 #endif