00001 /* 00002 $Id: List.h,v 1.13 2003/07/12 03:44:51 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<Container.h> 00011 00012 namespace clawsoft{ 00013 00020 template<class T> 00021 class List:public Container{ 00022 protected: 00029 class nodeT:public Container{ 00030 public: 00032 T data; 00034 nodeT *next; 00036 nodeT *before; 00042 nodeT():Container(clawsoft::_clawList__nodeT){ 00043 next = 0; 00044 before = 0; 00045 } 00051 nodeT(T d):Container(clawsoft::_clawList__nodeT){ 00052 data = d; 00053 next = 0; 00054 before = 0; 00055 } 00056 }; 00060 unsigned int middle; 00064 nodeT *list; 00068 nodeT *end; 00069 nodeT *act; 00070 public: 00075 List():Container(clawsoft::_clawList){ 00076 list = 0; 00077 end = 0; 00078 act = 0; 00079 } 00084 ~List(){ 00085 destroy(); 00086 } 00091 void destroy(){ 00092 nodeT *q = 0, *t = 0; 00093 q = list; 00094 while(q != 0){ 00095 t = q; 00096 q = q->next; 00097 delete t; 00098 } 00099 } 00103 void add(T data){ 00104 nodeT *q; 00105 q = new nodeT(data); 00106 if(list == 0){ 00107 list = q; 00108 end = list; 00109 } 00110 else{ 00111 end->next = q; 00112 q->before = end; 00113 end = q; 00114 } 00115 num_data++; 00116 middle = num_data / 2; 00117 act = list; 00118 } 00122 void remove(unsigned int idx){ 00123 nodeT *q, *b, *a, *t; 00124 unsigned int i; 00125 q = list; 00126 b = list; 00127 if(idx < 0 || idx >= num_data) 00128 throw ElementNotFoundException(); 00129 for(i = 0; q != 0; i++, q = q->next){ 00130 if(i == idx){ 00131 t = q; 00132 a = q->next; 00133 b->next = a; 00134 if(a != 0) 00135 a->before = b; 00136 if(t == list){ 00137 if(list != 0){ 00138 list = t->next; 00139 if(list != 0) 00140 list->before = 0; 00141 } 00142 } 00143 if(t == end){ 00144 if(end != 0){ 00145 end = end->before; 00146 if(end != 0) 00147 end->next = 0; 00148 } 00149 } 00150 delete t; 00151 num_data--; 00152 break; 00153 } 00154 b = q; 00155 } 00156 } 00157 00161 T &get(unsigned int j){ 00162 nodeT *r; 00163 nodeT *t; 00164 nodeT *q; 00165 unsigned int i; 00166 q = list; 00167 r = end; 00168 if(j >= num_data){ 00169 throw ArrayIndexOutOfLimitsException(); 00170 } 00171 if(j < middle){ 00172 for(i = 0; i != j; i++) 00173 q=q->next; 00174 t = q; 00175 } 00176 else{ 00177 for(i = num_data - 1; i != j; i--) 00178 r=r->before; 00179 t = r; 00180 } 00181 return t->data; 00182 } 00183 00187 T &get(){ 00188 if(act != 0){ 00189 return act->data; 00190 act = act->next; 00191 } 00192 return (T)0; 00193 } 00194 00198 void rewind(){ 00199 act = list; 00200 } 00201 00205 T &operator[](unsigned int i){ 00206 return get(i); 00207 } 00208 00209 00210 }; 00211 00212 }; 00213 00214 #endif
Authors:
Juan V. Guerrero
Jannette C. Mejia
Juan C. Guevara
Powered by: