00001 /*************************************************************************** 00002 queue.h - Definition of Queue template class. 00003 ------------------- 00004 begin : October 14 2002 00005 copyright : (C) 2003 by Vojtìch Toman 00006 email : vtoman@lit.cz 00007 ***************************************************************************/ 00008 00009 /*************************************************************************** 00010 * * 00011 * This program is free software; you can redistribute it and/or modify * 00012 * it under the terms of the GNU General Public License as published by * 00013 * the Free Software Foundation; either version 2 of the License, or * 00014 * (at your option) any later version. * 00015 * * 00016 ***************************************************************************/ 00017 00025 #ifndef QUEUE_H 00026 #define QUEUE_H 00027 00028 #include "defs.h" 00029 #include "collection.h" 00030 #include "debug.h" 00031 00032 00036 template<class T_> struct QueueLinkedList 00037 { 00039 struct QueueLinkedList<T_> *next_; 00040 00042 T_ *data_; 00043 00044 }; //QueueLinkedList 00045 00046 00050 template<class T_> class Queue : public Collection<T_> 00051 { 00052 public: 00058 Queue(void) : Collection<T_>() { initQueue(); } 00059 00065 virtual ~Queue(void) { clear(); } 00066 00067 00068 00076 virtual T_ *first(void) 00077 { 00078 if (!firstNode_) 00079 return 0; 00080 else 00081 { 00082 return firstNode_->data_; 00083 } 00084 } 00085 00086 00094 virtual T_ *last(void) 00095 { 00096 if (!lastNode_) 00097 return 0; 00098 else 00099 { 00100 return lastNode_->data_; 00101 } 00102 } 00103 00104 00105 00111 virtual void enqueue(T_ *item) 00112 { 00113 QueueLinkedList<T_> *pom; 00114 00115 NEW(pom, QueueLinkedList<T_>); 00116 00117 pom->data_ = item; 00118 pom->next_ = 0; 00119 00120 if (!lastNode_) 00121 firstNode_ = pom; 00122 else 00123 lastNode_->next_ = pom; 00124 00125 lastNode_ = pom; 00126 00127 cnt_++; 00128 } 00129 00130 00131 00137 virtual T_ *dequeue(void) 00138 { 00139 if (!firstNode_) 00140 return 0; 00141 else 00142 { 00143 QueueLinkedList<T_> *pom; 00144 T_ *data; 00145 00146 pom = firstNode_; 00147 data = firstNode_->data_; 00148 00149 firstNode_ = firstNode_->next_; 00150 00151 if (!firstNode_) 00152 lastNode_ = 0; 00153 00154 cnt_--; 00155 00156 DELETE(pom); 00157 00158 return data; 00159 } 00160 } 00161 00162 00163 00171 virtual void clear(void) 00172 { 00173 QueueLinkedList<T_> *pom; 00174 00175 while (firstNode_) 00176 { 00177 pom = firstNode_; 00178 firstNode_ = firstNode_->next_; 00179 00180 if (autoDelete_) 00181 DELETE(pom->data_); 00182 00183 DELETE(pom); 00184 } 00185 00186 firstNode_ = 0; 00187 lastNode_ = 0; 00188 cnt_ = 0; 00189 } 00190 00191 00192 00193 00194 protected: 00196 struct QueueLinkedList<T_> *firstNode_; 00197 00199 struct QueueLinkedList<T_> *lastNode_; 00200 00202 virtual void initQueue(void) 00203 { 00204 firstNode_ = 0; 00205 lastNode_ = 0; 00206 } 00207 00208 }; //Queue 00209 00210 00211 #endif //QUEUE_H