[ 数据结构作业 ] 游标链表 / 池子链表

作业居然要求用int做地址指针,狂死… 结果没有迭代器,重载了一个“[]"。算是唯一的新鲜内容了。

池子链表与测试函数
  1 template <class T>
  2 struct __pool_list_node{
  3     typedef __pool_list_node<T> node;
  4 
  5     T data;
  6     int succ;
  7     int next;
  8 
  9     __pool_list_node() { next = -1; }
 10     __pool_list_node(const T& t){
 11         data = t;
 12         next = -1;
 13     }
 14 };
 15 template <class T>
 16 class __pool_list{
 17 private:
 18     typedef __pool_list_node <T> node;
 19     //Here to test method erase()
 20     //and to provide the memory management is right
 21     //set MAXN = 2^4;
 22     //or else
 23     //const static int MAXN = (1 << 16);
 24     const static int MAXN = (1 << 4);
 25 
 26     size_t __list_size;
 27     node __pool[MAXN];
 28     int __pp;
 29     int __tail;
 30     int __last;
 31 public:
 32     __pool_list(){
 33         int i;
 34         for(i = 0; i < MAXN; i++)
 35             __pool[i].succ = i + 1;
 36 
 37         __pp = 1;
 38         __tail = 0;
 39         __last = MAXN - 1;
 40         __pool[0].next = 1;
 41         __pool[__last].succ = -1;
 42 
 43         __list_size = 0;
 44     }
 45 
 46     T operator [] (const int& i) {
 47         return __pool[i].data;
 48     }
 49 
 50     int begin(){
 51         return __pool[0].next;
 52     }
 53     int end(){
 54         return -1;
 55     }
 56 
 57     size_t size() const{
 58         return __list_size;
 59     }
 60     bool empty() const{
 61         return __pool[0].next == -1;
 62     }
 63 
 64     int GetPrevious(int p){
 65         int i;
 66         for(i = 0; i != -1 && __pool[i].next != p; i = __pool[i].next);
 67         return i;
 68     }
 69     void append(const T& t){
 70         __pool[__pp].data = t;
 71         __pool[__pp].next = -1;
 72 
 73         __pool[__tail].next = __pp;
 74         __tail = __pp;
 75 
 76         __pp = __pool[__pp].succ;
 77         __list_size ++;
 78     }
 79     int insert(int __pos, const T& t){
 80         __pool[__pp].data = t;
 81         __pool[__pp].next = __pos;
 82 
 83         __pool[GetPrevious(__pos)].next = __pp;
 84 
 85         __list_size ++;
 86         return __pp = __pool[__pp].succ;
 87     }
 88     int insert_after(int __pos, const T& t){
 89         __pool[__pp].data = t;
 90         __pool[__pp].next = __pool[__pos].next;
 91         __pool[__pos].next = __pp;
 92 
 93         __list_size ++;
 94         if(__pos == __tail) __tail = __pp;
 95         return __pp = __pool[__pp].succ;
 96     }
 97     int locate(const T& t){
 98         int i;
 99         for(i = __pool[0].next; i != -1 && __pool[i].data != t; i = __pool[i].next);
100         return i;
101     }
102     int modify(int __pos, const T& t){
103         __pool[__pos].data = t;
104         return __pos;
105     }
106     int next(int& i){
107         return __pool[i].next;
108     }
109     void erase(int __pos){
110         int __p = GetPrevious(__pos);
111         __pool[__p].next = __pool[__pos].next;
112         __pool[__last].succ = __pos;
113         __pool[__pos].succ = -1;
114         __last = __pos;
115 
116         if(__tail == __pos) __tail = __p;
117         __list_size --;
118     }
119 };
120 
121 //测试函数
122 void pool_list_test()
123 {
124         int i;
125     const int test_tab[] = {
126         1345,
127         125197,
128         19880812-1
129     };
130 
131     __pool_list <int> list;
132     cout << "------ I'm Dividing Line ------" << endl;
133     cout << "single append test" << endl;
134     list.append(6);
135     cout << list[list.begin()] << endl;
136 
137     cout << "------ I'm Dividing Line ------" << endl;
138     cout << "single locate test" << endl;
139     if(list.locate(5== list.end())
140         cout << "5 isn't found" << endl;
141     else
142         cout << "5 is found" << endl;
143 
144     cout << "------ I'm Dividing Line ------" << endl;
145     cout << "single modify test" << endl;
146     list.modify(list.locate(6), 5);
147     cout << list[list.begin()] << endl;
148 
149     cout << "------ I'm Dividing Line ------" << endl;
150     cout << "single erase && empty test" << endl;
151     list.erase(list.locate(5));
152     if(list.empty())
153         cout << "successfully erased" << endl;
154     else
155         cout << "unsuccessfully erased" << endl;
156 
157     cout << "------ I'm Dividing Line ------" << endl;
158     cout << "mutiple append && iterator test" << endl;
159     for(i = 0; test_tab[i] != -1; i++){
160         list.append(test_tab[i]);
161     }
162     for(i = list.begin(); i != list.end(); i = list.next(i)){
163         cout << list[i] << endl;
164     }
165 
166     cout << "------ I'm Dividing Line ------" << endl;
167     cout << "insert && insert_after test" << endl;
168     list.insert(list.locate(197), 196);
169     list.insert_after(list.locate(197), 198);
170     list.insert(list.locate(1), 0);
171     list.insert_after(list.locate(1), 2);
172     list.insert(list.locate(19880812), 19880811);
173     list.insert_after(list.locate(19880812), 19880813);
174     for(i = list.begin(); i != list.end(); i = list.next(i)){
175         cout << list[i] << endl;
176     }
177 
178     cout << "------ I'm Dividing Line ------" << endl;
179     cout << "memory management test" << endl;
180     list.erase(list.locate(0));
181     list.erase(list.locate(1));
182     list.erase(list.locate(2));
183 
184     list.append(6);
185     list.append(7);
186     list.append(8);
187     for(i = list.begin(); i != list.end(); i = list.next(i)){
188         cout << list[i] << endl;
189     }
190 }


 

posted @ 2010-04-16 21:25  刘一佳  阅读(217)  评论(0编辑  收藏  举报