00001 #ifndef __MY_LIST_H
00002 #define __MY_LIST_H
00003
00004 #ifndef NULL
00005 #define NULL 0
00006 #endif
00007
00008 template<class T>
00009 class CList
00010 {
00011 struct node
00012 {
00013 T data;
00014 node* next;
00015 node(T _data, node* _next = NULL) : data(_data), next(_next) {}
00016 node() : next(NULL) {};
00017 };
00018 protected:
00019 node* front;
00020 node* back;
00021 public:
00022 CList() : front(NULL), back(NULL) {}
00023 ~CList()
00024 {
00025 if (front != NULL)
00026 {
00027 while (front != NULL)
00028 {
00029 node *p = front;
00030 front = p->next;
00031 delete p;
00032 }
00033 }
00034 }
00035 T& first() { return front->data; }
00036 T& last() { return back->data; }
00037 T pop()
00038 {
00039 T data = front->data;
00040 node* n = front;
00041 front = front->next;
00042 delete n;
00043 return data;
00044 }
00045 T* operator[](int n)
00046 {
00047 node* current = front;
00048 while (n-- > 0)
00049 {
00050 if ((current = current->next) == NULL)
00051 return NULL;
00052 }
00053 return &(current->data);
00054 }
00055 void push_front(const T& t)
00056 {
00057 node* n = new node(t,front);
00058 if (front == NULL)
00059 {
00060 front = back = n;
00061 }
00062 else
00063 front = n;
00064 }
00065 void push_back(const T& t)
00066 {
00067 node* n = new node(t);
00068 if (front == NULL)
00069 {
00070 front = back = n;
00071 }
00072 else
00073 {
00074 back->next = n;
00075 back = n;
00076 }
00077 }
00078 bool isEmpty() { return (front == NULL); }
00079 void erase(unsigned int n)
00080 {
00081 node* p = front;
00082 node* last = front;
00083 while (n-- > 0)
00084 {
00085 last = p;
00086 p = p->next;
00087 if (p == NULL) return;
00088 }
00089 if (p == front)
00090 {
00091 front = p->next;
00092 }
00093 else
00094 {
00095 last->next = p->next;
00096 }
00097 if (p == back)
00098 {
00099 back = last;
00100 }
00101 delete p;
00102 }
00103 void sort()
00104 {
00105 int i,j,inc,n;
00106 T v;
00107 T* item;
00108 node* t;
00109 t = front;
00110 n = 0;
00111 while (t != NULL)
00112 {
00113 n++;
00114 t = t->next;
00115 }
00116 if (n >= 2)
00117 {
00118 item = new T[n];
00119 i = 0;
00120 t = front;
00121 for (t = front, i = 0; t != NULL; t = t->next, i++)
00122 {
00123 item[i] = t->data;
00124 }
00125
00126 for (inc = 1; inc <= n; inc = 3*inc+1);
00127
00128 do
00129 {
00130 inc /= 3;
00131 for (i = inc; i < n; i++)
00132 {
00133 v = item[i];
00134 for (j = i; v < item[j-inc] && j >= inc; j -= inc)
00135 {
00136 item[j] = item[j-inc];
00137 }
00138 item[j] = v;
00139 }
00140 }
00141 while (inc > 1);
00142 for (t = front, i = 0; t != NULL; t = t->next, i++)
00143 {
00144 t->data = item[i];
00145 }
00146
00147 delete [] item;
00148 }
00149 }
00150 class iterator
00151 {
00152 node* current;
00153 public:
00154 iterator(node* _c) : current(_c) {}
00155 iterator& operator++()
00156 {
00157 current = current->next;
00158 return *this;
00159 }
00160 iterator& operator++(int)
00161 {
00162 current = current->next;
00163 return *this;
00164 }
00165 T operator*()
00166 {
00167 return current->data;
00168 }
00169 T* operator->()
00170 {
00171 return &(current->data);
00172 }
00173 T* pContent()
00174 {
00175 return &(current->data);
00176 }
00177 bool operator!=(iterator t)
00178 {
00179 return (current != t.current);
00180 }
00181 bool operator==(iterator t)
00182 {
00183 return (current == t.current);
00184 }
00185 };
00186 iterator begin()
00187 {
00188 return iterator(front);
00189 }
00190 iterator end()
00191 {
00192 return iterator(NULL);
00193 }
00194 };
00195 #endif