双循环链表(迭代器版)

 1 #ifndef _LIST_H_
 2 #define _LIST_H_
 3 //虚基类
 4 template<class ElemType>
 5 class List
 6 {
 7 public:
 8     List() {};
 9     virtual ~List() {};
10     virtual void Clear() = 0;//清空数据结构
11     virtual void Insert(const int& i, const ElemType& X) = 0;//插入元素
12     virtual void ReMove(const int &i) = 0;//移除指定位置的元素
13     virtual void Erase(const ElemType& X) = 0;//删除表中所有X元素
14     virtual int Search(const ElemType& X) const = 0;//搜索某个元素
15     virtual void Traverse() const = 0;//遍历数据结构
16     virtual ElemType Visit(const int& i)const = 0;//访问某个元素
17 };
18 #endif // !_LIST_H_
List.h
  1 #ifndef __DCYCLELIST_H__
  2 #define __DCYCLELIST_H__
  3 #include "List.h"
  4 template<class ElemType>
  5 class dCycleList :public List<ElemType>
  6 {
  7 private:
  8     struct Node
  9     {
 10         Node* pre;
 11         Node* next;
 12         ElemType data;
 13         Node(const ElemType& X, Node* p = nullptr, Node* n = nullptr):data(X),pre(p),next(n){}
 14         Node():pre(nullptr),next(nullptr){}
 15         ~Node(){}
 16     };
 17     Node* Head;
 18     int CurrentLength;
 19     Node* Move(int i)const;
 20 public:
 21     dCycleList():Head(nullptr), CurrentLength(0){}
 22     virtual ~dCycleList() { Clear(); }
 23     virtual void Clear() override;//清空数据结构
 24     virtual void Insert(const int& i, const ElemType& X) override;//插入元素
 25     virtual void ReMove(const int& i) override;//移除指定位置的元素
 26     virtual void Erase(const ElemType& X) override;//删除表中所有X元素
 27     virtual int Search(const ElemType& X) const override;//搜索某个元素
 28     virtual void Traverse() const override;//遍历数据结构
 29     virtual ElemType Visit(const int& i)const override;//访问某个元素
 30     void Inverse();//逆置
 31     int GetSize() const;
 32     /*---------------迭代器---------------*/
 33     class Iterator
 34     {
 35         friend dCycleList;
 36     private:
 37         Node* cur;
 38     public:
 39         Iterator(Node *p = nullptr):cur(p){}
 40         ElemType operator *()
 41         {
 42             return cur->data;
 43         }
 44         Iterator operator ++(int X)
 45         {
 46             Iterator* tmp = *this;
 47             cur = cur->next;
 48             return tmp;
 49         }
 50         Iterator operator ++()
 51         {
 52             return cur = cur->next;
 53         }
 54         Iterator operator --(int X)
 55         {
 56             Iterator* tmp = *this;
 57             cur = cur->pre;
 58             return tmp;
 59         }
 60         Iterator operator --()
 61         {
 62             return cur = cur->pre;
 63         }
 64         bool operator ==(const Iterator& a)
 65         {
 66             return cur = a.cur;
 67         }
 68         bool operator !=(const Iterator& a)
 69         {
 70             return cur != a.cur;
 71         }
 72         bool isNull()
 73         {
 74             return nullptr == cur;
 75         }
 76     };
 77     Iterator begin()const;
 78     void insert(Iterator& p, const ElemType& a);
 79     void erase(Iterator&p);
 80     Iterator search(const ElemType& a);
 81 };
 82 
 83 template<class ElemType>
 84 typename dCycleList<ElemType>::Node* dCycleList<ElemType>::Move(int i) const
 85 {
 86     Node* p = Head;
 87     while (--i >= 0)
 88         p = p->next;
 89     return p;
 90 }
 91 
 92 template<class ElemType>
 93 void dCycleList<ElemType>::Clear()
 94 {
 95     if (Head == nullptr)
 96         return;
 97     Node* phead = nullptr;
 98     Node*tail = Head->pre;
 99     for (phead = Head; phead != tail; )
100     {
101         Node* del = phead;
102         phead = phead->next;
103         delete del;
104     }
105     delete phead;
106     CurrentLength = 0;
107 }
108 
109 template<class ElemType>
110 void dCycleList<ElemType>::Insert(const int& i, const ElemType& X)
111 {
112     if (i == 0)
113     {
114         if (Head == nullptr)
115         {
116             Head = new Node(X);
117             Head->pre = Head->next = Head;
118         }
119         else
120         {
121             Node* newNode = new Node(X, Head->pre, Head);
122             Node* tail = Head->pre;
123             tail->next = newNode;
124             Head->pre = newNode;
125             Head = newNode;
126         }
127     }
128     else
129     {
130         Node* pos = Move(i);
131         Node* newNode = new Node(X,pos->pre,pos);
132         pos->pre->next = newNode;
133         pos->pre = newNode;
134     }
135     ++CurrentLength;
136 }
137 
138 template<class ElemType>
139 void dCycleList<ElemType>::ReMove(const int& i)
140 {
141     Node* del = Move(i);
142     if (del == Head)
143     {
144         if (CurrentLength == 1)
145         {
146             delete Head;
147             --CurrentLength;
148             Head = nullptr;
149             return;
150         }
151         Node* pre = Head->pre;
152         Node* next = Head->next;
153         pre->next = next;
154         next->pre = pre;
155         Head = next;
156         delete del;
157     }
158     else
159     {
160         Node* pre = del->pre;
161         Node* next = del->next;
162         pre->next = next;
163         next->pre = pre;
164         delete del;
165     }
166     --CurrentLength;
167 }
168 
169 template<class ElemType>
170 void dCycleList<ElemType>::Erase(const ElemType& X)
171 {
172     if (Head == nullptr)
173         return;
174     int num = CurrentLength;
175     Node *p = Head;
176     Node *tail = Head->pre;
177     Node *pre = Head;
178     while (num--)
179     {
180         if (num + 1 == CurrentLength)
181         {
182             if (p->data == X)
183             {
184                 Head = Head->next;
185                 tail->next = Head;
186                 Head->pre = tail;
187                 delete p;
188                 p = Head;
189                 --CurrentLength;
190             }
191             else
192             {
193                 pre = p;
194                 p = p->next;
195             }
196         }
197         else
198         {
199             if (p->data == X)
200             {
201                 if (p == Head)
202                 {
203                     Head = Head->next;
204                     tail->next = Head;
205                     Head->pre = tail;
206                     delete p;
207                     p = Head;
208                 }
209                 else
210                 {
211                     p = p->next;
212                     delete pre->next;
213                     pre->next = p;
214                     p->pre = pre;
215                 }
216                 --CurrentLength;
217             }
218             else
219             {
220                 pre = p;
221                 p = p->next;
222             }
223         }
224     }
225 }
226 
227 template<class ElemType>
228 int dCycleList<ElemType>::Search(const ElemType& X) const
229 {
230     Node* p = Head;
231     int i = 0;
232     while (p->data != X)
233     {
234         ++i;
235         p = p->next;
236         if (i == CurrentLength)
237         {
238             return -1;
239         }
240     }
241     return i;
242 }
243 
244 template<class ElemType>
245 void dCycleList<ElemType>::Traverse() const
246 {
247     if (Head == nullptr)
248         return;
249     Node* p = Head;
250     for (int i = 0; i < CurrentLength; ++i)
251     {
252         std::cout << p->data << std::endl;
253         p = p->next;
254     }
255 }
256 
257 template<class ElemType>
258 ElemType dCycleList<ElemType>::Visit(const int& i) const
259 {
260     return Move(i)->data;
261 }
262 
263 template<class ElemType>
264 void dCycleList<ElemType>::Inverse()
265 {
266     Node* pn = Head->next;
267     Node* tmp = Head;
268     while (pn != tmp)
269     {
270         Node* pt = pn->next;
271         Node* pr = Head;
272         Head = pn;
273         Head->next = pr;
274         Head->pre = tmp->pre;
275         pn = pt;
276     }
277     pn->next = Head;
278 }
279 
280 
281 template<class ElemType>
282 typename dCycleList<ElemType>::Iterator dCycleList<ElemType>::begin() const
283 {
284     return Head;
285 }
286 
287 template<class ElemType>
288 void dCycleList<ElemType>::insert(Iterator& p, const ElemType& a)
289 {
290     if (p.cur == nullptr)
291     {
292         Head = p.cur = new Node(a);
293         Head->next = Head;
294         Head->pre = Head;
295     }
296     else
297     {
298         p.cur->next = p.cur->next->pre = new Node(a, p.cur, p.cur->next);
299         p.cur = p.cur->next;
300     }
301 }
302 
303 template<class ElemType>
304 void dCycleList<ElemType>::erase(Iterator& p)
305 {
306     if (p.cur == nullptr)
307         return;
308     if (p.cur->next == p.cur)
309     {
310         delete p.cur;
311         Head = p.cur = nullptr;
312     }
313     else
314     {
315         Node* q = p.cur;
316         q->next->pre = q->pre;
317         q->pre->next = q->next;
318         p.cur = q->next;
319         if (q==Head)
320         {
321             Head = q->next;
322             delete q;
323         }
324     }
325 }
326 
327 
328 template<class ElemType>
329 int dCycleList<ElemType>::GetSize() const
330 {
331     return CurrentLength;
332 }
333 
334 
335 template<class ElemType>
336 typename dCycleList<ElemType>::Iterator dCycleList<ElemType>::search(const ElemType& a)
337 {
338     Node* p = Head;
339     if (nullptr==p)
340         return nullptr;
341     do 
342     {
343         if (a==p->data)
344             return p;
345         p = p->next;
346     } while (p!=Head);
347 }
348 #endif
dCycleList.h
  1 #include <iostream>
  2 #include <string>
  3 #include "dCycleList.h"
  4 void IntTest()
  5 {
  6     std::cout << "\n----------------------IntTest----------------------" << std::endl;
  7     dCycleList<int> sList;
  8     for (int i = 0; i < 10; ++i)
  9         sList.Insert(i, i + 10);
 10     sList.Traverse();
 11     std::cout << "---------ReMove---------" << std::endl;
 12     sList.ReMove(0);
 13     sList.Traverse();
 14     std::cout << "---------Insert---------" << std::endl;
 15     sList.Insert(9, 9999);
 16     sList.Insert(0, 777);
 17     sList.Insert(4, 777);
 18     sList.Insert(4, 777);
 19     sList.Insert(10, 777);
 20     sList.Traverse();
 21     std::cout << "---------Erase---------" << std::endl;
 22     sList.Erase(777);
 23     sList.Erase(-111);
 24     sList.Traverse();
 25     std::cout << "---------Search---------" << std::endl;
 26     std::cout << sList.Search(1111111) << std::endl;
 27     std::cout << "---------Visit---------" << std::endl;
 28     std::cout << sList.Visit(11) << std::endl;
 29     std::cout << "---------Inverse---------" << std::endl;
 30     sList.Inverse();
 31     sList.Traverse();
 32 }
 33 
 34 void StringTest()
 35 {
 36     std::cout << "\n----------------------StringTest----------------------" << std::endl;
 37     std::string sarr[] = { "aaaa", "bbb", "ccc", "ddd","eee", "fff", "ggg", "hhh","iii","jjj" };
 38     dCycleList<std::string> sList;
 39     for (int i = 0; i < 10; ++i)
 40         sList.Insert(i, sarr[i]);
 41     sList.Traverse();
 42     std::cout << "---------ReMove---------" << std::endl;
 43     sList.ReMove(0);
 44     sList.Traverse();
 45     std::cout << "---------Insert---------" << std::endl;
 46     sList.Insert(0, "ggg");
 47     sList.Insert(3, "ggg");
 48     sList.Traverse();
 49     std::cout << "---------Erase---------" << std::endl;
 50     sList.Erase("ggg");
 51     sList.Traverse();
 52     std::cout << "---------Search---------" << std::endl;
 53     std::cout << sList.Search("ddd") << std::endl;
 54     std::cout << "---------Visit---------" << std::endl;
 55     std::cout << sList.Visit(5) << std::endl;
 56     std::cout << "---------Inverse---------" << std::endl;
 57     sList.Inverse();
 58     sList.Traverse();
 59 }
 60 
 61 struct MyStruct
 62 {
 63     int id;
 64     char name[20];
 65     void copy(const MyStruct& tmp)
 66     {
 67         id = tmp.id;
 68         for (int i = 0; i < 20; ++i)
 69         {
 70             name[i] = tmp.name[i];
 71         }
 72     }
 73     bool operator ==(const MyStruct&tmp) const
 74     {
 75         if (this->id == tmp.id)
 76         {
 77             for (int i = 0; i < 20; ++i)
 78             {
 79                 if (this->name[i] != tmp.name[i])
 80                     return false;
 81             }
 82         }
 83         else
 84         {
 85             return false;
 86         }
 87         return true;
 88     }
 89     MyStruct& operator =(const MyStruct& right)
 90     {
 91         copy(right);
 92         return *this;
 93     }
 94 
 95     bool operator !=(const MyStruct& right)
 96     {
 97         if (right == *this)
 98             return false;
 99         return true;
100     }
101 
102     friend std::ostream& operator <<(std::ostream &os, const MyStruct&self)
103     {
104         os << self.id << "  " << self.name;
105         return os;
106     }
107 };
108 static MyStruct Stdudent[10] =
109 {
110     {1,"ge"},
111     {2,"sheng"},
112     {3,"lu"},
113     {4,"ge1"},
114     {5,"sheng1"},
115     {6,"lu1"},
116     {7,"ge2"},
117     {8,"sheng2"},
118     {9,"lu2"},
119     {10,"lu3"},
120 };
121 
122 void ObjectTest()
123 {
124     std::cout << "\n----------------------ObjectTest----------------------" << std::endl;
125     dCycleList<MyStruct> sList;
126     for (int i = 0; i < 10; ++i)
127         sList.Insert(i, Stdudent[i]);
128     sList.Traverse();
129     std::cout << "---------ReMove---------" << std::endl;
130     sList.ReMove(0);
131     sList.Traverse();
132     std::cout << "---------Insert---------" << std::endl;
133     sList.Insert(0, Stdudent[7]);
134     sList.Insert(3, Stdudent[7]);
135     sList.Traverse();
136     std::cout << "---------Erase---------" << std::endl;
137     sList.Erase(Stdudent[7]);
138     sList.Traverse();
139     std::cout << "---------Search---------" << std::endl;
140     std::cout << sList.Search(Stdudent[5]) << std::endl;
141     std::cout << "---------Visit---------" << std::endl;
142     std::cout << sList.Visit(5) << std::endl;
143     std::cout << "---------Inverse---------" << std::endl;
144     sList.Inverse();
145     sList.Traverse();
146 }
147 
148 void TestIterator()
149 {
150     std::cout << "\n----------------------TestIterator----------------------" << std::endl;
151     dCycleList<MyStruct> sList;
152     dCycleList<MyStruct>::Iterator Itr;
153 
154     for (int i = 0; i < 10; ++i)
155         sList.Insert(i, Stdudent[i]);
156     sList.Traverse();
157     //sList.traverse(sList);
158     dCycleList<MyStruct>::Iterator Itr2(sList.begin());
159 
160     std::cout << "---------begin()  ++  end()---------" << std::endl;
161     int i = 0;
162     for (Itr = sList.begin(); i++ <= sList.GetSize(); ++Itr)
163     {
164         std::cout << *Itr << std::endl;
165     }
166     Itr = sList.begin();
167     std::cout << "--------- ==  != ---------" << std::endl;
168     if (Itr2 == Itr)
169     {
170         std::cout << "==" << std::endl;
171     }
172     else
173     {
174         std::cout << "!=" << std::endl;
175     }
176     std::cout << "---------insert---------" << std::endl;
177     sList.insert(Itr, Stdudent[2]);
178     sList.Traverse();
179 
180     std::cout << "---------erase---------" << std::endl;
181     sList.erase(Itr);
182     sList.Traverse();
183 
184     std::cout << "---------search---------" << std::endl;
185     ++Itr;
186     ++Itr;
187     dCycleList<MyStruct>::Iterator Itr3(sList.search(*(Itr)));
188     if (Itr3 != nullptr)
189     {
190         std::cout << *Itr3 << std::endl;
191     }
192 }
193 
194 int main()
195 {
196     /*IntTest();
197     StringTest();
198     ObjectTest();*/
199     TestIterator();
200     return 0;
201 }
main.cpp

猜你喜欢

转载自www.cnblogs.com/jiangnansytle/p/12391802.html
今日推荐