码迷,mamicode.com
首页 > 其他好文 > 详细

单循环链表

时间:2020-02-18 16:35:38      阅读:68      评论:0      收藏:0      [点我收藏+]

标签:--   private   ide   slist   arc   stream   for   one   tor   

技术图片
 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 __SCYCLISTLIST_H__
  2 #define __SCYCLISTLIST_H__
  3 #include "List.h"
  4 template<class ElemType>
  5 class sCycleList :public List<ElemType>
  6 {
  7 private:
  8     struct Node
  9     {
 10         ElemType data;
 11         Node* next;
 12         Node(const ElemType& x, Node* n = nullptr) :data(x), next(n) {}
 13         Node() :next(nullptr) {}
 14     };    
 15     Node* Head;
 16     int CurrentLength;
 17     Node* Move(int i) const;
 18 public:
 19     sCycleList() :Head(nullptr), CurrentLength(0){};
 20     ~sCycleList() { Clear(); };
 21     virtual void Clear() override;//清空数据结构
 22     virtual void Insert(const int& i, const ElemType& X) override;//插入元素
 23     virtual void ReMove(const int& i) override;//移除指定位置的元素
 24     virtual void Erase(const ElemType& X) override;//删除表中所有X元素
 25     virtual int Search(const ElemType& X) const override;//搜索某个元素
 26     virtual void Traverse() const override;//遍历数据结构
 27     virtual ElemType Visit(const int& i)const override;//访问某个元素
 28     void Inverse();//逆置
 29 };
 30 
 31 
 32 template<class ElemType>
 33 typename sCycleList<ElemType>::Node* sCycleList<ElemType>::Move(int i) const
 34 {
 35     Node* tmp = Head;
 36     while (--i >= 0)
 37     {
 38         tmp = tmp->next;
 39     }
 40     return tmp;
 41 }
 42 
 43 template<class ElemType>
 44 void sCycleList<ElemType>::Clear()
 45 {
 46     if (Head != nullptr)
 47     {
 48         Node* beg = Head, * tmp = nullptr;
 49         do
 50         {
 51             tmp = Head->next;
 52             delete Head;
 53             Head = tmp;
 54             --CurrentLength;
 55         } while (beg == Head);
 56     }
 57 }
 58 
 59 template<class ElemType>
 60 void sCycleList<ElemType>::Insert(const int& i, const ElemType& X)
 61 {
 62     if (Head == nullptr)
 63     {
 64         Head = new Node(X, Head);
 65         Head->next = Head;
 66     }
 67     else
 68     {
 69         if (i == 0)
 70         {
 71             Node* newNode = new Node(X, Head);
 72             Head = newNode;
 73             Node* tail = Move(CurrentLength);
 74             tail->next = Head;
 75         }
 76         else
 77         {
 78             Node* pre = Move(i - 1);
 79             Node* newNode = new Node(X, pre->next);
 80             pre->next = newNode;
 81         }    
 82     }
 83     ++CurrentLength;
 84 }
 85 
 86 template<class ElemType>
 87 void sCycleList<ElemType>::ReMove(const int& i)
 88 {
 89     Node* pre = Move(i - 1);
 90     
 91     if (pre == Head)
 92     {
 93         Node* delp = pre;
 94         Head = Head->next;
 95         delete delp;
 96         Node* tail = Move(CurrentLength-1);
 97         tail->next = Head;
 98     }
 99     else
100     {
101         Node* delp = pre->next;
102         pre->next = pre->next->next;
103         delete delp;
104     }
105     --CurrentLength;
106 }
107 
108 template<class ElemType>
109 void sCycleList<ElemType>::Erase(const ElemType& X)
110 {
111     if (Head == nullptr)
112         return;
113     int num = CurrentLength;
114     Node* p = Head;
115     Node* tail = Move(CurrentLength-1);
116     Node* pre = Head;
117     while (num--)
118     {
119         if (num + 1 == CurrentLength)
120         {
121             if (p->data == X)
122             {
123                 Head = Head->next;
124                 tail->next = Head;
125                 delete p;
126                 p = Head;
127                 --CurrentLength;
128             }
129             else
130             {
131                 pre = p;
132                 p = p->next;
133             }
134         }
135         else
136         {
137             if (p->data == X)
138             {
139                 if (p == Head)
140                 {
141                     Head = Head->next;
142                     tail->next = Head;
143                     delete p;
144                     p = Head;
145                 }
146                 else
147                 {
148                     p = p->next;
149                     delete pre->next;
150                     pre->next = p;
151                 }    
152                 --CurrentLength;
153             }
154             else
155             {
156                 pre = p;
157                 p = p->next;
158             }
159         }
160     }
161 }
162 
163 template<class ElemType>
164 int sCycleList<ElemType>::Search(const ElemType& X) const
165 {
166     Node* p = Head;
167     int i = 0;
168     while (p->data != X)
169     {
170         ++i;
171         if (i == CurrentLength)
172             return -1;
173         p = p->next;
174     }
175     return i;
176 }
177 
178 template<class ElemType>
179 void sCycleList<ElemType>::Traverse() const
180 {
181     int num = CurrentLength;
182     Node* p = Head;
183     while (num--)
184     {
185         std::cout << p->data << std::endl;
186         p = p->next;
187     }
188 }
189 
190 template<class ElemType>
191 ElemType sCycleList<ElemType>::Visit(const int& i) const
192 {
193     return Move(i)->data;
194 }
195 
196 template<class ElemType>
197 void sCycleList<ElemType>::Inverse()
198 {
199     Node* pn = Head->next;
200     Node* tmp = Head;
201     while (pn!= tmp)
202     {
203         Node* pt = pn->next;
204         Node* pr = Head;
205         Head = pn;
206         Head->next = pr;
207         pn = pt;
208     }
209     pn->next = Head;
210 }
211 #endif
sCycleList.h
技术图片
  1 #include <iostream>
  2 #include <string.h>
  3 #include "sCycleList.h"
  4 void IntTest()
  5 {
  6     std::cout<< "\n----------------------IntTest----------------------" << std::endl;
  7     sCycleList<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     sCycleList<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     sCycleList<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

 

单循环链表

标签:--   private   ide   slist   arc   stream   for   one   tor   

原文地址:https://www.cnblogs.com/jiangnansytle/p/12326643.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!