Main Page   Packages   Class Hierarchy   Alphabetical List   Data Structures   File List   Namespace Members   Data Fields   Globals  

List.h

Go to the documentation of this file.
00001 /*
00002  $Id: List.h,v 1.8 2002/08/09 06:23:24 mindstorm2600 Exp $
00003 
00004  This class is responsible for the management of linked lists using templates
00005  please take in account that this is not a thread safe class, so use it
00006  wisely
00007  */
00008 #ifndef __CLAW_LIST_H__
00009 #define __CLAW_LIST_H__
00010 #include<Object.h>
00011 #include<Exception.h>
00012 
00013 namespace clawsoft{
00014 
00021         template<class T>
00022         class List:public Object{
00023                 protected:
00030                         class nodeT:public Object{
00031                                 public:
00033                                 T data;
00035                                 nodeT *next;
00037                                 nodeT *before;
00043                                 nodeT():Object("List::nodeT"){
00044                                         next = 0;
00045                                         before = 0;
00046                                 }
00052                                 nodeT(T d):Object("List::nodeT"){
00053                                         data = d;
00054                                         next = 0;
00055                                         before = 0;
00056                                 }
00057                         };
00062                         unsigned int num_data;
00066                         unsigned int middle;
00070                         nodeT *list;
00074                         nodeT *end;
00075                 public:
00080                         List():Object("List"){
00081                                 list = 0;
00082                                 end = 0;
00083                                 num_data = 0;
00084                         }
00089                         ~List(){
00090                                 destroy();
00091                         }
00096                         void destroy(){
00097                                 nodeT *q = 0, *t = 0;
00098                                 q = list;
00099                                 while(q != 0){
00100                                         t = q;
00101                                         q = q->next;
00102                                         delete t;
00103                                 }
00104                         }
00108                         void add(T data){
00109                                 nodeT *q;
00110                                 q = new nodeT(data);
00111                                 if(list == 0){
00112                                         list = q;
00113                                         end = list;
00114                                 }
00115                                 else{
00116                                         end->next = q;
00117                                         q->before = end;
00118                                         end = q;
00119                                 }
00120                                 num_data++;
00121                                 middle = num_data / 2;
00122                         }
00126                         void remove(int idx){
00127                                 nodeT *q, *b, *a, *t;
00128                                 int i;
00129                                 q = list;
00130                                 b = list;
00131                                 if(idx == 0){
00132                                         delete list;
00133                                         list = 0;
00134                                         end = 0;
00135                                         return;
00136                                 }
00137                                 for(i = 0; q != 0; i++, q = q->next){
00138                                         if(i == idx){
00139                                                 t = q;
00140                                                 a = q->next;
00141                                                 b->next = a;
00142                                                 if(a != 0)
00143                                                         a->before = b;
00144                                                 delete t;
00145                                                 break;
00146                                         }
00147                                 }
00148                         }
00149 
00153                         T &get(unsigned int j){
00154                                 nodeT *r;
00155                                 nodeT *t;
00156                                 nodeT *q;
00157                                 unsigned int i;
00158                                 q = list;
00159                                 r = end;
00160                                 if(j >= num_data){
00161                                         throw ArrayOutOfBoundsException();
00162                                 }
00163                                 if(j < middle){
00164                                         for(i = 0; i != j; i++)
00165                                                 q=q->next;
00166                                         t = q;
00167                                 }
00168                                 else{
00169                                         for(i = num_data - 1; i != j; i--)
00170                                                 r=r->before;
00171                                         t = r;
00172                                 }
00173                                 return t->data;
00174                         }
00175 
00179                         T &operator[](unsigned int i){
00180                                 return get(i);
00181                         }
00182         };
00183 
00184 };
00185 
00186 #endif

Powered by:

SourceForge Logo