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

LinkedList.h

Go to the documentation of this file.
00001 
00007 #ifndef _LINKEDLIST_H_
00008 #define _LINKEDLIST_H_
00009 
00010 #include "Log.h"
00011 
00012 template <class TYPE>
00013 class LLNode {
00014         public:
00015                 TYPE obj;
00016                 LLNode *prev;
00017                 LLNode *next;
00018 };
00019 
00020 template <class TYPE>
00021 class LinkedList {
00022         public:
00023                 LinkedList();
00024                 virtual ~LinkedList();
00025 
00026                 inline void clear();
00027                 inline const unsigned int getCount();
00028                 inline LLNode <TYPE> *getFirst();
00029                 inline LLNode <TYPE> *getLast();
00030                 inline LLNode <TYPE> *popFirst();
00031                 inline void addFirst(const TYPE &object);
00032                 inline void addLast(const TYPE &object);
00033 
00034                 inline void addNodeFirst(LLNode <TYPE> *node);
00035                 inline void addNodeLast(LLNode <TYPE> *node);
00036                 inline void removeNode(LLNode <TYPE> *node);
00037 
00038         protected:
00039                 unsigned int size;
00040                 LLNode <TYPE> *first, *last;
00041 };
00042 
00043 template <class TYPE>
00044 LinkedList<TYPE>::LinkedList() {
00045         size = 0;
00046         first = last = NULL;
00047 }
00048 
00049 template <class TYPE>
00050 LinkedList<TYPE>::~LinkedList() {
00051         clear();
00052 }
00053 
00054 template <class TYPE>
00055 void LinkedList<TYPE>::clear() {
00056         LLNode <TYPE> *node = first;
00057 
00058         while (node != NULL) {
00059                 LLNode <TYPE> *delNode = node;
00060                 node = node->next;
00061                 delete delNode;
00062         }
00063 
00064         size = 0;
00065         first = last = NULL;
00066 }
00067 
00068 template <class TYPE>
00069 const unsigned int LinkedList<TYPE>::getCount() {
00070         return size;
00071 }
00072 
00073 template <class TYPE>
00074 LLNode <TYPE> *LinkedList<TYPE>::popFirst() {
00075         LLNode <TYPE> *node = new LLNode <TYPE> (*first);
00076         removeNode(first);
00077         return node;
00078 }
00079 
00080 template <class TYPE>
00081 LLNode <TYPE> *LinkedList<TYPE>::getFirst() {
00082         return first;
00083 }
00084 
00085 template <class TYPE>
00086 LLNode <TYPE> *LinkedList<TYPE>::getLast() {
00087         return last;
00088 }
00089 
00090 template <class TYPE>
00091 void LinkedList<TYPE>::addFirst(const TYPE &object) {
00092         LLNode <TYPE> *node = new LLNode <TYPE>;
00093         node->obj = object;
00094         addNodeFirst(node);
00095 }
00096 
00097 template <class TYPE>
00098 void LinkedList<TYPE>::addLast(const TYPE &object) {
00099         LLNode <TYPE> *node = new LLNode <TYPE>;
00100         node->obj = object;
00101         addNodeLast(node);
00102 }
00103 
00104 template <class TYPE>
00105 void LinkedList<TYPE>::addNodeFirst(LLNode <TYPE> *node) {
00106         if (first == NULL) {
00107                 first = node;
00108                 last = node;
00109                 node->prev = NULL;
00110                 node->next = NULL;
00111         }
00112         else {
00113                 first->prev = node;
00114                 node->next = first;
00115                 first = node;
00116                 node->prev = NULL;
00117         }
00118 
00119         size++;
00120 }
00121 
00122 template <class TYPE>
00123 void LinkedList<TYPE>::addNodeLast(LLNode <TYPE> *node) {
00124         if (last == NULL) {
00125                 first = node;
00126                 last = node;
00127                 node->prev = NULL;
00128                 node->next = NULL;
00129         }
00130         else {
00131                 last->next = node;
00132                 node->prev = last;
00133                 last = node;
00134                 node->next = NULL;
00135         }
00136 
00137         size++;
00138 }
00139 
00140 template <class TYPE>
00141 void LinkedList<TYPE>::removeNode(LLNode <TYPE> *node) {
00142         if (node->prev == NULL) {
00143                 first = node->next;
00144         }
00145         else {
00146                 node->prev->next = node->next;
00147         }
00148 
00149         if (node->next == NULL) {
00150                 last = node->prev;
00151         }
00152         else {
00153                 node->next->prev = node->prev;
00154         }
00155 
00156         delete node;
00157         size--;
00158 }
00159 
00160 #endif

Generated on Sun Jun 5 15:47:04 2005 for Defacto by  doxygen 1.4.3