其他分享
首页 > 其他分享> > 双循环链表

双循环链表

作者:互联网

 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 };
 32 
 33 template<class ElemType>
 34 typename dCycleList<ElemType>::Node* dCycleList<ElemType>::Move(int i) const
 35 {
 36     Node* p = Head;
 37     while (--i >= 0)
 38         p = p->next;
 39     return p;
 40 }
 41 
 42 template<class ElemType>
 43 void dCycleList<ElemType>::Clear()
 44 {
 45     if (Head == nullptr)
 46         return;
 47     Node* phead = nullptr;
 48     Node*tail = Head->pre;
 49     for (phead = Head; phead != tail; )
 50     {
 51         Node* del = phead;
 52         phead = phead->next;
 53         delete del;
 54     }
 55     delete phead;
 56     CurrentLength = 0;
 57 }
 58 
 59 template<class ElemType>
 60 void dCycleList<ElemType>::Insert(const int& i, const ElemType& X)
 61 {
 62     if (i == 0)
 63     {
 64         if (Head == nullptr)
 65         {
 66             Head = new Node(X);
 67             Head->pre = Head->next = Head;
 68         }
 69         else
 70         {
 71             Node* newNode = new Node(X, Head->pre, Head);
 72             Node* tail = Head->pre;
 73             tail->next = newNode;
 74             Head->pre = newNode;
 75             Head = newNode;
 76         }
 77     }
 78     else
 79     {
 80         Node* pos = Move(i);
 81         Node* newNode = new Node(X,pos->pre,pos);
 82         pos->pre->next = newNode;
 83         pos->pre = newNode;
 84     }
 85     ++CurrentLength;
 86 }
 87 
 88 template<class ElemType>
 89 void dCycleList<ElemType>::ReMove(const int& i)
 90 {
 91     Node* del = Move(i);
 92     if (del == Head)
 93     {
 94         if (CurrentLength == 1)
 95         {
 96             delete Head;
 97             --CurrentLength;
 98             Head = nullptr;
 99             return;
100         }
101         Node* pre = Head->pre;
102         Node* next = Head->next;
103         pre->next = next;
104         next->pre = pre;
105         Head = next;
106         delete del;
107     }
108     else
109     {
110         Node* pre = del->pre;
111         Node* next = del->next;
112         pre->next = next;
113         next->pre = pre;
114         delete del;
115     }
116     --CurrentLength;
117 }
118 
119 template<class ElemType>
120 void dCycleList<ElemType>::Erase(const ElemType& X)
121 {
122     if (Head == nullptr)
123         return;
124     int num = CurrentLength;
125     Node *p = Head;
126     Node *tail = Head->pre;
127     Node *pre = Head;
128     while (num--)
129     {
130         if (num + 1 == CurrentLength)
131         {
132             if (p->data == X)
133             {
134                 Head = Head->next;
135                 tail->next = Head;
136                 Head->pre = tail;
137                 delete p;
138                 p = Head;
139                 --CurrentLength;
140             }
141             else
142             {
143                 pre = p;
144                 p = p->next;
145             }
146         }
147         else
148         {
149             if (p->data == X)
150             {
151                 if (p == Head)
152                 {
153                     Head = Head->next;
154                     tail->next = Head;
155                     Head->pre = tail;
156                     delete p;
157                     p = Head;
158                 }
159                 else
160                 {
161                     p = p->next;
162                     delete pre->next;
163                     pre->next = p;
164                     p->pre = pre;
165                 }
166                 --CurrentLength;
167             }
168             else
169             {
170                 pre = p;
171                 p = p->next;
172             }
173         }
174     }
175 }
176 
177 template<class ElemType>
178 int dCycleList<ElemType>::Search(const ElemType& X) const
179 {
180     Node* p = Head;
181     int i = 0;
182     while (p->data != X)
183     {
184         ++i;
185         p = p->next;
186         if (i == CurrentLength)
187         {
188             return -1;
189         }
190     }
191     return i;
192 }
193 
194 template<class ElemType>
195 void dCycleList<ElemType>::Traverse() const
196 {
197     if (Head == nullptr)
198         return;
199     Node* p = Head;
200     for (int i = 0; i < CurrentLength; ++i)
201     {
202         std::cout << p->data << std::endl;
203         p = p->next;
204     }
205 }
206 
207 template<class ElemType>
208 ElemType dCycleList<ElemType>::Visit(const int& i) const
209 {
210     return Move(i)->data;
211 }
212 
213 template<class ElemType>
214 void dCycleList<ElemType>::Inverse()
215 {
216     Node* pn = Head->next;
217     Node* tmp = Head;
218     while (pn != tmp)
219     {
220         Node* pt = pn->next;
221         Node* pr = Head;
222         Head = pn;
223         Head->next = pr;
224         Head->pre = tmp->pre;
225         pn = pt;
226     }
227     pn->next = Head;
228 }
229 #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 int main()
149 {
150     IntTest();
151     StringTest();
152     ObjectTest();
153     return 0;
154 }
main.cpp

 

标签:Node,pre,Head,const,双循环,int,next,链表
来源: https://www.cnblogs.com/jiangnansytle/p/12331895.html