00001
00002 #ifndef LIST_H
00003 #define LIST_H
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00035 template <class T>
00036 class LIST
00037 {
00038 public:
00039 typedef struct LIST_TYPE {
00040 T data;
00041 struct LIST_TYPE *next;
00042 } list_type;
00043
00044 private:
00045 list_type *list;
00046
00047 public:
00048 typedef list_type *handle;
00049
00050 public:
00051 LIST(void)
00052 {
00053 list = 0;
00054 }
00055
00056
00058 void dealloc(void)
00059 {
00060 while ( remove() != 0 )
00061 ;
00062 }
00063
00064
00065 void add( T data )
00066 {
00067 list_type *t;
00068
00069 t = new list_type;
00070 t->data = data;
00071 t->next = 0;
00072 if (list == 0) {
00073 list = t;
00074 }
00075 else {
00076 t->next = list;
00077 list = t;
00078 }
00079 }
00080
00081
00082
00083 void reverse(void)
00084 {
00085 list_type *elem, *prev, *next;
00086
00087 prev = 0;
00088 next = 0;
00089
00090 for (elem = list; elem != 0; prev = elem, elem = next) {
00091 next = elem->next;
00092 elem->next = prev;
00093 }
00094 list = prev;
00095 }
00096
00097
00099 unsigned int length(void)
00100 {
00101 list_type *elem;
00102 unsigned int cnt = 0;
00103
00104 for (elem = list; elem != 0; elem = elem->next)
00105 cnt++;
00106 return cnt;
00107 }
00108
00114 handle remove(void)
00115 {
00116 list_type *t;
00117
00118 if (list != 0) {
00119 t = list;
00120 list = t->next;
00121 delete t;
00122 }
00123 return list;
00124 }
00125
00126
00128 T get_item( handle h)
00129 {
00130
00131 return h->data;
00132 }
00133
00134
00136 handle first(void)
00137 {
00138 return list;
00139 }
00140
00141
00143 handle next(handle h)
00144 {
00145 list_type *next = 0;
00146
00147 if (h != 0) {
00148 next = h->next;
00149 }
00150
00151 return next;
00152 }
00153
00154 };
00155
00156
00157 #endif
00158