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

orderedList.h

Go to the documentation of this file.
00001 00006 00007 // This software is distributed as part of the Improla library. 00008 // Improla is a library of classes for image processing. 00009 // 00010 // Copyright (c) 2004, B. R. Siva Chandra, India 00011 // 00012 // This program is free software; you can redistribute it and/or 00013 // modify it under the terms of the GNU General Public License 00014 // as published by the Free Software Foundation; either version 2 00015 // of the License, or (at your option) any later version. 00016 // 00017 // This program is distributed in the hope that it will be useful, 00018 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00020 // GNU General Public License for more details. 00021 // 00022 // You should have received a copy of the GNU General Public License 00023 // along with this program; if not, write to the Free Software 00024 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00025 // 00026 // In case you would like to contact the author, use the following e-mail 00027 // address: sivachandra_br@yahoo.com 00029 00031 // First Created : 21st September 2004 00032 // Author : Siva Chandra 00033 // Purpose : Declares a class which is a doubly-linked list and can be ordered. 00035 00036 #ifndef __ORDERED_LIST_H__ 00037 #define __ORDERED_LIST_H__ 00038 00039 #include <templateNode.h> 00040 00041 namespace tutk 00042 { // Start of namespace brace. 00043 00052 template < class T > 00053 class OrderedList 00054 { 00055 public: 00058 OrderedList(void); 00059 00079 OrderedList(const OrderedList &ol); 00080 00083 virtual ~OrderedList(); 00084 00092 void push(T *data, double key); 00093 00099 void pop(void); 00100 00103 int getSize(void); 00104 00107 void sortAscendingBubbleSort(void); 00108 00111 void sortDescendingBubbleSort(void); 00112 00115 tutk::TemplateNode< T >* getFirstNodePtr(void); 00116 00120 //void operator=(const OrderedList &ol); 00121 00122 private: 00123 tutk::TemplateNode< T > *firstNodePtr; 00124 tutk::TemplateNode< T > *lastNodePtr; 00125 tutk::TemplateNode< T > *currentNodePtr; 00126 00127 int *size; 00128 int *refCount; 00129 }; 00130 00131 template < class T > 00132 OrderedList< T >::OrderedList(void) 00133 { 00134 firstNodePtr = lastNodePtr = currentNodePtr = NULL; 00135 00136 size = new int; 00137 refCount = new int; 00138 00139 *size = 0; 00140 *refCount = 1; 00141 } 00142 00143 template < class T > 00144 OrderedList< T >::OrderedList(const OrderedList &ol) 00145 { 00146 firstNodePtr = ol.firstNodePtr; 00147 lastNodePtr = ol.lastNodePtr; 00148 currentNodePtr = ol.currentNodePtr; 00149 00150 size = ol.size; 00151 refCount = ol.refCount; 00152 (*refCount)++; 00153 } 00154 00155 template < class T > 00156 OrderedList< T >::~OrderedList() 00157 { 00158 if (*refCount <= 1) 00159 { 00160 while (*size != 0) 00161 pop(); 00162 00163 delete size; size = NULL; 00164 delete refCount; refCount = NULL; 00165 } 00166 else 00167 { 00168 (*refCount)--; 00169 00170 size = NULL; 00171 refCount = NULL; 00172 } 00173 } 00174 00175 template < class T > 00176 void OrderedList< T >::push(T *data, double key) 00177 { 00178 if (firstNodePtr == NULL) 00179 { 00180 firstNodePtr = new tutk::TemplateNode< T >(); 00181 currentNodePtr = lastNodePtr = firstNodePtr; 00182 00183 firstNodePtr->data = data; 00184 firstNodePtr->key = key; 00185 00186 (*size)++; 00187 } 00188 else 00189 { 00190 lastNodePtr->nextNodePtr = new tutk::TemplateNode< T >(); 00191 (lastNodePtr->nextNodePtr)->prevNodePtr = lastNodePtr; 00192 00193 lastNodePtr = lastNodePtr->nextNodePtr; 00194 lastNodePtr->data = data; 00195 lastNodePtr->key = key; 00196 00197 (*size)++; 00198 } 00199 00200 return; 00201 } 00202 00203 template < class T > 00204 void OrderedList< T >::pop(void) 00205 { 00206 if (lastNodePtr == NULL) 00207 return; 00208 00209 if (lastNodePtr->prevNodePtr == NULL) 00210 { 00211 delete lastNodePtr->data; lastNodePtr->data = NULL; 00212 delete lastNodePtr; 00213 00214 lastNodePtr = firstNodePtr = currentNodePtr = NULL; 00215 *size = 0; 00216 00217 return; 00218 } 00219 00220 delete lastNodePtr->data; lastNodePtr->data = NULL; 00221 lastNodePtr == lastNodePtr->prevNodePtr; 00222 if (currentNodePtr == lastNodePtr->nextNodePtr) 00223 currentNodePtr = lastNodePtr; 00224 00225 delete lastNodePtr->nextNodePtr; 00226 lastNodePtr->nextNodePtr = NULL; 00227 00228 (*size)--; 00229 00230 return; 00231 } 00232 00233 template < class T > 00234 int OrderedList< T >::getSize(void) 00235 { 00236 return *size; 00237 } 00238 00239 template < class T > 00240 tutk::TemplateNode< T >* OrderedList< T >::getFirstNodePtr(void) 00241 { 00242 return firstNodePtr; 00243 } 00244 00245 template < class T > 00246 void OrderedList< T >::sortAscendingBubbleSort(void) 00247 { 00248 int s = *size; 00249 for (int i = s - 1; i > 0; i--) 00250 { 00251 tutk::TemplateNode< T >* temp = firstNodePtr; 00252 T *tempT; double tempk; 00253 if (temp->key > (temp->nextNodePtr)->key) 00254 { 00255 tempT = temp->data; 00256 tempk = temp->key; 00257 00258 temp->data = (temp->nextNodePtr)->data; 00259 temp->key = (temp->nextNodePtr)->key; 00260 00261 (temp->nextNodePtr)->data = tempT; 00262 (temp->nextNodePtr)->key = tempk; 00263 } 00264 00265 for (int j = 2; j <= i; j++) 00266 { 00267 temp = temp->nextNodePtr; 00268 00269 if (temp->key > (temp->nextNodePtr)->key) 00270 { 00271 tempT = temp->data; 00272 tempk = temp->key; 00273 00274 temp->data = (temp->nextNodePtr)->data; 00275 temp->key = (temp->nextNodePtr)->key; 00276 00277 (temp->nextNodePtr)->data = tempT; 00278 (temp->nextNodePtr)->key = tempk; 00279 } 00280 } 00281 } 00282 00283 return; 00284 } 00285 00286 template < class T > 00287 void OrderedList< T >::sortDescendingBubbleSort(void) 00288 { 00289 int s = *size; 00290 for (int i = s - 1; i > 0; i--) 00291 { 00292 tutk::TemplateNode< T >* temp = firstNodePtr; 00293 T *tempT; double tempk; 00294 if (temp->key < (temp->nextNodePtr)->key) 00295 { 00296 tempT = temp->data; 00297 tempk = temp->key; 00298 00299 temp->data = (temp->nextNodePtr)->data; 00300 temp->key = (temp->nextNodePtr)->key; 00301 00302 (temp->nextNodePtr)->data = tempT; 00303 (temp->nextNodePtr)->key = tempk; 00304 } 00305 00306 for (int j = 2; j <= i; j++) 00307 { 00308 temp = temp->nextNodePtr; 00309 00310 if (temp->key < (temp->nextNodePtr)->key) 00311 { 00312 tempT = temp->data; 00313 tempk = temp->key; 00314 00315 temp->data = (temp->nextNodePtr)->data; 00316 temp->key = (temp->nextNodePtr)->key; 00317 00318 (temp->nextNodePtr)->data = tempT; 00319 (temp->nextNodePtr)->key = tempk; 00320 } 00321 } 00322 } 00323 00324 return; 00325 } 00326 00327 } // End of namespace brace. 00328 00329 #endif // __ORDERED_LIST_H__ 00330

Generated on Mon Nov 22 11:12:41 2004 for ImprolaLib by doxygen 1.3.7