Main Page | Class Hierarchy | Class List | File List | Class Members | Related Pages

D:/Programming/GUI Editor (Source)/DoubleList.h

00001 #ifndef DOUBLE_LIST_TEMPLATE_INCLUDED 00002 #define DOUBLE_LIST_TEMPLATE_INCLUDED 00003 00004 #include <stdio.h> 00005 #include <malloc.h> 00006 00008 00011 template <class T> class CDoubleList 00012 { 00013 private: 00014 00015 struct tNode 00016 { 00017 T Data; 00018 tNode *m_pPrev; 00019 tNode *m_pNext; 00020 00021 tNode() { m_pPrev = NULL; m_pNext = NULL; } 00022 }; 00023 00024 tNode *m_pHead; 00025 tNode *m_pTail; 00026 tNode *pPtr; 00028 int m_iSize; 00031 public: 00032 00033 CDoubleList() { m_pHead = NULL; m_pTail = NULL; m_iSize = 0; pPtr = NULL;} 00034 ~CDoubleList() {}; 00035 00041 int index(T data) 00042 { 00043 if(size() == 0) 00044 return -1; 00045 00046 tNode *tmp = m_pHead; // MAKE TEMPORARY NODE POINTING TO HEAD 00047 00048 int count = 0; 00049 while(tmp !=NULL ) // LOOP THRU LIST AND INCREMENT THE COUNT UNTIL COUNT ISNT EQUAL TO PASSED INTEGER 00050 { 00051 if(tmp->Data == data) 00052 return count; 00053 00054 tmp = tmp->m_pNext; 00055 count++; 00056 } 00057 }; 00062 void erase(bool bDataErase = true) 00063 { 00064 if(m_pHead == NULL && m_pTail == NULL) 00065 return; 00066 else 00067 { 00068 tNode *tmp = m_pHead; 00069 00070 while(tmp !=NULL) 00071 { 00072 tNode *next = tmp->m_pNext; 00073 if(bDataErase) 00074 delete tmp->Data; 00075 00076 if(tmp->Data !=NULL) 00077 delete tmp; 00078 00079 tmp = next; 00080 } 00081 m_pTail = NULL; 00082 m_pHead = NULL; 00083 m_iSize = 0; 00084 } 00085 }; 00090 void push_front(T data) 00091 { 00092 tNode* Temp = new tNode; // CREATE NEW CONTAINER FOR DATA 00093 Temp->Data = data; // ASSIGN DATA FROM CONTAINER TO POINT TO THE PASSED DATA 00094 00095 if(m_pHead == NULL && m_pTail == NULL) // IF OUR LIST IS EMPTY 00096 { 00097 Temp->m_pNext = NULL; 00098 Temp->m_pPrev = NULL; 00099 m_pHead = Temp; // ASSIGN HEAD AND TAIL TO NEWLY ADDED CONTAINER 00100 m_pTail = Temp; 00101 } 00102 else 00103 { 00104 Temp->m_pNext = m_pHead; // OTHERWISE MAKE TEMP A NEW HEAD 00105 m_pHead->m_pPrev = Temp; 00106 Temp->m_pPrev = NULL; 00107 m_pHead = Temp; 00108 } 00109 m_iSize++; // WE SUCCESSEFULLY ADDED A CONTAINER, INCREMENT COUNT BY 1 00110 } 00115 void push_back(T data) 00116 { 00117 tNode* Temp = new tNode; // CREATE NEW CONTAINER FOR DATA 00118 Temp->Data = data; // ASSIGN DATA FROM CONTAINER TO POINT TO THE PASSED DATA 00119 00120 if(m_pHead == NULL && m_pTail == NULL) // IF OUR LIST IS EMPTY 00121 { 00122 Temp->m_pNext = NULL; 00123 Temp->m_pPrev = NULL; 00124 m_pHead = Temp; // ASSIGN HEAD AND TAIL TO NEWLY ADDED CONTAINER 00125 m_pTail = Temp; 00126 } 00127 else 00128 { 00129 Temp->m_pPrev = m_pTail; // OTHERWISE MAKE TEMP A NEW TAIL 00130 Temp->m_pNext = NULL; 00131 m_pTail->m_pNext = Temp; 00132 m_pTail = Temp; 00133 } 00134 m_iSize++; // WE SUCCESSEFULLY ADDED A CONTAINER, INCREMENT COUNT BY 1 00135 } 00140 T pop_front() 00141 { 00142 if(m_pHead !=NULL) // IF OUR HEAD ISNT NULL (WHAT MEANS THAT OUR LIST IS EMPTY) 00143 { 00144 T data = m_pHead->Data; // MAKE TEMPORARY STORAGE FOR DATA 00145 tNode *new_head = m_pHead->m_pNext; // CREATE NEW HEAD 00146 delete m_pHead; // DELETE OLD HEAD 00147 m_pHead = NULL; 00148 m_pHead = new_head; // ASSIGN HEAD TO POINT TO NEW HEAD 00149 00150 if(m_pHead == NULL) // IF ITS NULL, MAKE SURE OUR TAIL IS ALSO NULL 00151 { 00152 m_pTail = NULL; 00153 m_iSize = 0; 00154 } 00155 else 00156 m_pHead->m_pPrev = NULL; // MAKE SURE ITS NOT A CIRCULAR LINKED LIST, SO PREVIOUS OF THE HEAD IS NOTHING 00157 00158 m_iSize--; // WE SUCCESSEFULLY REMOVED A CONTAINER, DECREMENT COUNT BY 1 00159 if(m_iSize < 0) 00160 m_iSize = 0; 00161 00162 return data; // RETURN OUR REMOVED DATA 00163 00164 } 00165 return NULL; // IF LIST IS EMPTY, RETURN NULL 00166 } 00171 T pop_back() 00172 { 00173 if(m_pTail !=NULL) 00174 { 00175 T data = m_pTail->Data; 00176 tNode *new_tail = m_pTail->m_pPrev; 00177 delete m_pTail; 00178 m_pTail = NULL; 00179 m_pTail = new_tail; 00180 00181 if(m_pTail == NULL) 00182 m_pHead = NULL; 00183 else 00184 m_pTail->m_pNext = NULL; 00185 00186 m_iSize--; 00187 if(m_iSize < 0) 00188 m_iSize = 0; 00189 return data; 00190 } 00191 00192 return NULL; 00193 } 00199 T at(int i) // note that array starts at 0, not 1!!! So, to get member index = 5, pass 4 here 00200 { 00201 if(this == NULL) 00202 return NULL; 00203 00204 tNode *tmp = m_pHead; // MAKE TEMPORARY NODE POINTING TO HEAD 00205 00206 int count = 0; 00207 while(tmp !=NULL && count !=i) // LOOP THRU LIST AND INCREMENT THE COUNT UNTIL COUNT ISNT EQUAL TO PASSED INTEGER 00208 { 00209 tmp = tmp->m_pNext; 00210 count++; 00211 } 00212 00213 if(tmp !=NULL) // IF OUR NODE ISNT NULL, RETURN ITS DATA 00214 return tmp->Data; 00215 else 00216 return NULL; // OTHERWISE RETURN NULL 00217 } 00223 void move_to(unsigned int index, T Element) 00224 { 00225 if(index == 0) 00226 return; 00227 00228 unsigned int count = 0; 00229 00230 T Data = remove(Element); 00231 00232 tNode *tmp = m_pHead; 00233 while(tmp !=NULL) 00234 { 00235 if(count == index) 00236 { 00237 tNode *pPrev = tmp; 00238 tNode *pNext = tmp->m_pNext; 00239 00240 tNode *pNewNode = new tNode; 00241 pNewNode->Data = Data; 00242 00243 pPrev->m_pNext = pNewNode; 00244 pNewNode->m_pPrev = pPrev; 00245 pNewNode->m_pNext = pNext; 00246 pNext->m_pPrev = pNewNode; 00247 00248 return; 00249 } 00250 00251 count++; 00252 tmp = tmp->m_pNext; 00253 } 00254 }; 00260 T remove_from(int i) 00261 { 00262 if( i >= 0) 00263 { 00264 tNode *tmp = m_pHead; 00265 00266 int count = 0; 00267 while(tmp !=NULL && count !=i) 00268 { 00269 tmp = tmp->m_pNext; 00270 count++; 00271 } 00272 00273 if(tmp !=NULL) 00274 { 00275 T ret_data = tmp->Data; 00276 tNode *tmp1 = tmp->m_pPrev; 00277 tNode *tmp2 = tmp->m_pNext; 00278 if(tmp2 == NULL) // we got the tail 00279 { 00280 return pop_back(); 00281 } 00282 00283 if(tmp1 == NULL) // we hit the head 00284 { 00285 return pop_front(); 00286 } 00287 00288 if(tmp1 !=NULL && tmp2 !=NULL) 00289 { 00290 tmp1->m_pNext = tmp2; 00291 tmp2->m_pPrev = tmp1; 00292 delete tmp; 00293 tmp = NULL; 00294 m_iSize--; 00295 if(m_iSize < 0) 00296 m_iSize = 0; 00297 return ret_data; 00298 } 00299 00300 } 00301 } 00302 return NULL; 00303 } 00309 tNode *remove_raw(T data) 00310 { 00311 tNode *tmp = m_pHead; 00312 while(tmp !=NULL) 00313 { 00314 if(tmp->Data == data) 00315 { 00316 tNode *prev = tmp->m_pPrev; 00317 tNode *next = tmp->m_pNext; 00318 00319 if(prev !=NULL) 00320 prev->m_pNext = next; 00321 00322 if(next !=NULL) 00323 next->m_pPrev = prev; 00324 00325 if(tmp == m_pHead && tmp == m_pTail) 00326 m_pHead = m_pTail = pPtr = NULL; 00327 00328 m_iSize--; 00329 if(m_iSize < 0) 00330 m_iSize = 0; 00331 return tmp; 00332 } 00333 00334 tmp = tmp->m_pNext; 00335 } 00336 00337 return NULL; 00338 } 00343 void add_raw(tNode *node) 00344 { 00345 tNode *head = m_pHead; 00346 node->m_pNext = head; 00347 node->m_pPrev = NULL; 00348 00349 if(head !=NULL) 00350 { 00351 head->m_pPrev = node; 00352 m_pHead = head; 00353 } 00354 else 00355 m_pHead = m_pTail = node; 00356 00357 m_iSize++; 00358 } 00364 T remove(T data) 00365 { 00366 tNode *tmp = m_pHead; 00367 int count = 0; 00368 00369 while(tmp !=NULL) 00370 { 00371 if(tmp->Data == data) 00372 return remove_from(count); 00373 else 00374 { 00375 tmp = tmp->m_pNext; 00376 count++; 00377 } 00378 } 00379 return NULL; 00380 } 00386 T operator [] (int i) 00387 { return at(i); } 00392 void set_ptr(T data) 00393 { 00394 tNode *find = m_pHead; 00395 00396 while(find !=NULL) 00397 { 00398 if(find->Data == data) 00399 { 00400 pPtr = find; 00401 return; 00402 } 00403 else 00404 find = find->m_pNext; 00405 } 00406 } 00411 T next() 00412 { 00413 if(pPtr !=NULL) 00414 { 00415 pPtr = pPtr->m_pNext; 00416 if(pPtr) 00417 return pPtr->Data; 00418 } 00419 00420 return NULL; 00421 } 00426 T prev() 00427 { 00428 if(pPtr !=NULL) 00429 { 00430 pPtr = pPtr->m_pPrev; 00431 if(pPtr) 00432 return pPtr->Data; 00433 } 00434 00435 return NULL; 00436 } 00441 T ptr() 00442 { 00443 if(pPtr !=NULL) 00444 return pPtr->Data; 00445 00446 return NULL; 00447 } 00452 int size() { return m_iSize; } 00457 T begin() 00458 { 00459 if(m_pHead == NULL) 00460 return NULL; 00461 00462 return m_pHead->Data; 00463 } 00468 T end() 00469 { 00470 if(m_pTail == NULL) 00471 return NULL; 00472 00473 return m_pTail->Data; 00474 } 00475 }; 00476 00477 #endif

Generated on Sun Jul 17 21:34:26 2005 for OpenGL GUI by doxygen 1.3.8