Go to the documentation of this file.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 #ifndef MARTI_DATA_STRUCTURES_LINKED_LIST_H_
00031 #define MARTI_DATA_STRUCTURES_LINKED_LIST_H_
00032
00033 #ifndef NULL
00034 #define NULL 0
00035 #endif
00036
00037 namespace marti_data_structures
00038 {
00039 template<class T>
00040 class LinkedList
00041 {
00042 public:
00043
00044 LinkedList()
00045 {
00046 NumElements = 0;
00047 }
00048
00049
00050 LinkedList(const LinkedList<T> &src)
00051 {
00052 NumElements = 0;
00053 this->CopyList(src, *this);
00054 }
00055
00056 ~LinkedList()
00057 {
00058 ctr *tptr;
00059 while (this->size() > 0)
00060 {
00061 tptr = HEAD;
00062 HEAD = HEAD->next;
00063 this->remove(tptr);
00064 }
00065 }
00066
00067
00068 LinkedList<T>& operator=(const LinkedList<T>& src)
00069 {
00070 this->CopyList(src, *this);
00071 return *this;
00072 }
00073
00074 void initialize()
00075 {
00076 ctr *tptr;
00077 while (this->size() > 0)
00078 {
00079 tptr = HEAD;
00080 HEAD = HEAD->next;
00081 this->remove(tptr);
00082 }
00083 }
00084
00085 int size()
00086 {
00087 return NumElements;
00088 }
00089
00090 void add(T &newElem)
00091 {
00092 if (this->size() == 0)
00093 {
00094 this->CreateNewLinkedList(&newElem);
00095 }
00096 else
00097 {
00098 this->addToTail(&newElem);
00099 }
00100 }
00101
00102 void addCopy(const T &newElem)
00103 {
00104 T* copyElem = new T;
00105 *copyElem = newElem;
00106 this->add(*copyElem);
00107 }
00108
00109
00110
00111
00112 void insertAt(T &newElem, int i)
00113 {
00114 this->addInPosition(&newElem, i);
00115 }
00116
00117 void insertCopyAt(const T &newElem, int i)
00118 {
00119 T* copyElem = new T;
00120 *copyElem = newElem;
00121 this->addInPosition(copyElem, i);
00122 }
00123
00124 void remove(int i)
00125 {
00126 ctr *tptr;
00127 tptr = this->get(i);
00128 if (tptr)
00129 {
00130 this->remove(tptr);
00131 }
00132 }
00133
00134 T* ReturnElement(int i)
00135 {
00136
00137 ctr* temp = this->get(i);
00138 if (NULL == temp)
00139 {
00140 return NULL;
00141 }
00142 else
00143 {
00144 return (temp->Data);
00145 }
00146 }
00147
00148
00149 void CropList(int i)
00150 {
00151 while (this->ReturnElement(i))
00152 {
00153 this->remove(i);
00154 }
00155 }
00156
00157 private:
00158 struct ctr
00159 {
00160 T *Data;
00161 ctr *next;
00162 ctr *prev;
00163 };
00164 ctr *temp;
00165 ctr *HEAD;
00166 ctr *TAIL;
00167 int NumElements;
00168
00169
00170 void CreateNewLinkedList(T *firstElement)
00171 {
00172 temp = this->alloc_elem(firstElement);
00173 HEAD = temp;
00174 TAIL = temp;
00175 temp->next = NULL;
00176 temp->prev = NULL;
00177 NumElements++;
00178 }
00179
00180 void addToTail(T *newElem)
00181 {
00182 temp = this->alloc_elem(newElem);
00183 temp->prev = TAIL;
00184 TAIL = temp;
00185 temp->next = NULL;
00186 temp->prev->next = temp;
00187 NumElements++;
00188 }
00189
00190 void addInPosition(T *elem, int i)
00191 {
00192 ctr *itemToMove = this->get(i);
00193 if (itemToMove == NULL)
00194 {
00195 this->add(*elem);
00196 }
00197 else
00198 {
00199 temp = this->alloc_elem(elem);
00200 temp->next = itemToMove;
00201 temp->prev = itemToMove->prev;
00202 if (temp->prev != NULL)
00203 {
00204 itemToMove->prev->next = temp;
00205 }
00206 else
00207 {
00208 this->HEAD = temp;
00209 }
00210 itemToMove->prev = temp;
00211 NumElements++;
00212 }
00213 }
00214
00215 ctr* alloc_elem(T *elem)
00216 {
00217 ctr *tptr = new ctr;
00218 tptr->Data = elem;
00219 return tptr;
00220 }
00221
00222 void release_elem(ctr *elem)
00223 {
00224 delete elem->Data;
00225 delete elem;
00226 }
00227
00228 ctr* get(int i)
00229 {
00230 if (i >= this->size() || i < 0)
00231 {
00232 return NULL;
00233 }
00234 ctr* temp = HEAD;
00235 for (int j = 0; j < i; j++)
00236 {
00237 temp = temp->next;
00238 }
00239 return temp;
00240 }
00241
00242 void remove(ctr *node)
00243 {
00244 if (node->prev != NULL)
00245 {
00246 node->prev->next = node->next;
00247 }
00248 else
00249 {
00250 HEAD = node->next;
00251 if (HEAD != NULL)
00252 {
00253 HEAD->prev = NULL;
00254 }
00255 }
00256 if (node->next != NULL)
00257 {
00258 node->next->prev = node->prev;
00259 }
00260 else
00261 {
00262 TAIL = node->prev;
00263 if (TAIL != NULL)
00264 {
00265 TAIL->next = NULL;
00266 }
00267 }
00268 this->release_elem(node);
00269 NumElements--;
00270 }
00271
00272
00273 void CopyList(const LinkedList<T> &src, LinkedList<T> &dest)
00274 {
00275 dest.initialize();
00276 int N = src.size();
00277 for (int i = 0; i < N; ++i)
00278 {
00279 dest.addCopy(*(src.ReturnElement(i)));
00280 }
00281 }
00282 };
00283
00284 template<class T>
00285 class LinkedList_NoDealloc
00286 {
00287 public:
00288
00289 LinkedList_NoDealloc()
00290 {
00291 NumElements = 0;
00292 }
00293
00294
00295 LinkedList_NoDealloc(const LinkedList_NoDealloc<T> &src)
00296 {
00297 this->CopyList(src, *this);
00298 }
00299
00300 ~LinkedList_NoDealloc()
00301 {
00302 ctr *tptr;
00303 while (this->size() > 0)
00304 {
00305 tptr = HEAD;
00306 HEAD = HEAD->next;
00307 this->remove(tptr);
00308 }
00309 }
00310
00311
00312 LinkedList_NoDealloc<T>& operator=(const LinkedList_NoDealloc<T>& src)
00313 {
00314 this->CopyList(src, *this);
00315 return *this;
00316 }
00317
00318 void initialize()
00319 {
00320 ctr *tptr;
00321 while (this->size() > 0)
00322 {
00323 tptr = HEAD;
00324 HEAD = HEAD->next;
00325 this->remove(tptr);
00326 }
00327 }
00328
00329 int size()
00330 {
00331 return NumElements;
00332 }
00333
00334 void add(T &newElem)
00335 {
00336 if (this->size() == 0)
00337 {
00338 this->CreateNewLinkedList(&newElem);
00339 }
00340 else
00341 {
00342 this->addToTail(&newElem);
00343 }
00344 }
00345
00346 void addCopy(const T &newElem)
00347 {
00348 T* copyElem = new T;
00349 *copyElem = newElem;
00350 this->add(*copyElem);
00351 }
00352
00353
00354
00355
00356 void insertAt(T &newElem, int i)
00357 {
00358 this->addInPosition(&newElem, i);
00359 }
00360
00361 void insertCopyAt(const T &newElem, int i)
00362 {
00363 T* copyElem = new T;
00364 *copyElem = newElem;
00365 this->addInPosition(copyElem, i);
00366 }
00367
00368 void remove(int i)
00369 {
00370 ctr *tptr;
00371 tptr = this->get(i);
00372 if (tptr)
00373 {
00374 this->remove(tptr);
00375 }
00376 }
00377
00378 T* ReturnElement(int i)
00379 {
00380
00381 ctr* temp = this->get(i);
00382 if (NULL == temp)
00383 {
00384 return NULL;
00385 }
00386 else
00387 {
00388 return (temp->Data);
00389 }
00390 }
00391
00392
00393 void CropList(int i)
00394 {
00395 while (this->ReturnElement(i))
00396 {
00397 this->remove(i);
00398 }
00399 }
00400
00401 private:
00402 struct ctr
00403 {
00404 T *Data;
00405 ctr *next;
00406 ctr *prev;
00407 };
00408 ctr *temp;
00409 ctr *HEAD;
00410 ctr *TAIL;
00411 int NumElements;
00412
00413
00414 void CreateNewLinkedList(T *firstElement)
00415 {
00416 temp = this->alloc_elem(firstElement);
00417 HEAD = temp;
00418 TAIL = temp;
00419 temp->next = NULL;
00420 temp->prev = NULL;
00421 NumElements++;
00422 }
00423
00424 void addToTail(T *newElem)
00425 {
00426 temp = this->alloc_elem(newElem);
00427 temp->prev = TAIL;
00428 TAIL = temp;
00429 temp->next = NULL;
00430 temp->prev->next = temp;
00431 NumElements++;
00432 }
00433
00434 void addInPosition(T *elem, int i)
00435 {
00436 ctr *itemToMove = this->get(i);
00437 if (itemToMove == NULL)
00438 {
00439 this->add(*elem);
00440 }
00441 else
00442 {
00443 temp = this->alloc_elem(elem);
00444 temp->next = itemToMove;
00445 temp->prev = itemToMove->prev;
00446 if (temp->prev != NULL)
00447 {
00448 itemToMove->prev->next = temp;
00449 }
00450 else
00451 {
00452 this->HEAD = temp;
00453 }
00454 itemToMove->prev = temp;
00455 NumElements++;
00456 }
00457 }
00458
00459 ctr* alloc_elem(T *elem)
00460 {
00461 ctr *tptr = new ctr;
00462 tptr->Data = elem;
00463 return tptr;
00464 }
00465
00466 void release_elem(ctr *elem)
00467 {
00468 delete elem;
00469 }
00470
00471 ctr* get(int i)
00472 {
00473 if (i >= this->size() || i < 0)
00474 {
00475 return NULL;
00476 }
00477 ctr* temp = HEAD;
00478 for (int j = 0; j < i; j++)
00479 {
00480 temp = temp->next;
00481 }
00482 return temp;
00483 }
00484
00485 void remove(ctr *node)
00486 {
00487 if (node->prev != NULL)
00488 {
00489 node->prev->next = node->next;
00490 }
00491 else
00492 {
00493 HEAD = node->next;
00494 if (HEAD != NULL)
00495 {
00496 HEAD->prev = NULL;
00497 }
00498 }
00499 if (node->next != NULL)
00500 {
00501 node->next->prev = node->prev;
00502 }
00503 else
00504 {
00505 TAIL = node->prev;
00506 if (TAIL != NULL)
00507 {
00508 TAIL->next = NULL;
00509 }
00510 }
00511 this->release_elem(node);
00512 NumElements--;
00513 }
00514
00515
00516 void CopyList(
00517 const LinkedList_NoDealloc<T> &src,
00518 LinkedList_NoDealloc<T> &dest)
00519 {
00520 dest.initialize();
00521 int N = src.size();
00522 for (int i = 0; i < N; ++i)
00523 {
00524 dest.addCopy(*(src.ReturnElement(i)));
00525 }
00526 }
00527 };
00528 }
00529
00530 #endif // MARTI_DATA_STRUCTURES_LINKED_LIST_H_