单链表

 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 __SLINKLIST_H__
  2 #define __SLINKLIST_H__
  3 #include "List.h"
  4 #include <iostream>
  5 template<class ElemType>
  6 class sLinkList :public List<ElemType>
  7 {
  8 private:
  9     struct Node 
 10     {
 11         ElemType data;
 12         Node* next;
 13         Node(const ElemType &x,Node*n=nullptr):data(x),next(n){}
 14         Node():next(nullptr) {};
 15         ~Node(){}
 16     };
 17     Node* Head;
 18     int CurrentLength;
 19     Node* Move(int i) const;
 20 public:
 21     sLinkList();
 22     virtual ~sLinkList() { Clear(); delete Head; }
 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 sLinkList<ElemType>::sLinkList()
 35 {
 36     Head = new Node;
 37     CurrentLength = 0;
 38 }
 39 
 40 template<class ElemType>
 41 typename sLinkList<ElemType>::Node* sLinkList<ElemType>::Move(int i) const
 42 {
 43     Node* p = Head;
 44     while (i-- >= 0)
 45         p = p->next;
 46     return p;
 47 }
 48 
 49 template<class ElemType>
 50 void sLinkList<ElemType>::Clear()
 51 {
 52     Node* p = Head->next, * q;
 53     Head->next = nullptr;
 54     while (p != nullptr)
 55     {
 56         q = p->next;
 57         delete p;
 58         p = q;
 59     }
 60     CurrentLength = 0;
 61 }
 62 
 63 template<class ElemType>
 64 void sLinkList<ElemType>::Insert(const int& i, const ElemType& X)
 65 {
 66     Node* pos = Move(i - 1);
 67     pos->next = new Node(X, pos->next);
 68 }
 69 
 70 template<class ElemType>
 71 void sLinkList<ElemType>::ReMove(const int& i)
 72 {
 73     Node* pos, * delp;
 74     pos = Move(i - 1);
 75     delp = pos->next;
 76     pos->next = delp->next;
 77     delete delp;
 78     --CurrentLength;
 79 }
 80 
 81 template<class ElemType>
 82 void sLinkList<ElemType>::Erase(const ElemType& X)
 83 {
 84     int delNum = 0;
 85     Node* delp = nullptr;
 86     for (Node* pre = Head; pre->next != nullptr; pre = pre->next)
 87     {
 88         if (pre->next->data == X)
 89         {
 90             delp = pre->next;
 91             pre->next = delp->next;
 92             delete delp;
 93             ++delNum;
 94         }
 95     }
 96     CurrentLength -= delNum;
 97 }
 98 
 99 template<class ElemType>
100 int sLinkList<ElemType>::Search(const ElemType& X) const
101 {
102     Node* p = Head->next;
103     int i = 0;
104     while (p != nullptr && p->data != X)
105     {
106         p = p->next;
107         ++i;
108     }
109     if (p != nullptr) return i; else return -1;
110 }
111 
112 template<class ElemType>
113 void sLinkList<ElemType>::Traverse() const
114 {
115     Node* p = Head->next;
116     while (p != nullptr)
117     {
118         std::cout << p->data << std::endl;
119         p = p->next;
120     }
121 }
122 
123 template<class ElemType>
124 ElemType sLinkList<ElemType>::Visit(const int& i)const
125 {
126     return Move(i)->data;
127 }
128 
129 template<class ElemType>
130 void sLinkList<ElemType>::Inverse()
131 {
132     if (Head->next == nullptr)
133         return;
134     Node* cur = Head->next->next;
135     Node* cur_next;
136     Head->next->next = nullptr;
137     while (cur != nullptr)
138     {
139         cur_next = cur->next;
140         cur->next = Head->next;
141         Head->next = cur;
142         cur = cur_next;
143     }
144 }
145 #endif
sLinkList.h
  1 #include <iostream>
  2 #include <string.h>
  3 #include "sLinkList.h"
  4 void IntTest()
  5 {
  6     std::cout<< "\n----------------------IntTest----------------------" << std::endl;
  7     sLinkList<int> sList;
  8     for (int i = 0; i < 10; ++i)
  9         sList.Insert(i,i);
 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(0, 9999);
 16     sList.Insert(3, 9999);
 17     sList.Traverse();
 18     std::cout << "---------Erase---------" << std::endl;
 19     sList.Erase(9999);
 20     sList.Traverse();
 21     std::cout << "---------Search---------" << std::endl;
 22     std::cout << sList.Search(8)<<std::endl;
 23     std::cout << "---------Visit---------" << std::endl;
 24     std::cout << sList.Visit(0) << std::endl;
 25     std::cout << "---------Inverse---------" << std::endl;
 26     sList.Inverse();
 27     sList.Traverse();
 28 }
 29 
 30 void StringTest()
 31 {
 32     std::cout << "\n----------------------StringTest----------------------" << std::endl;
 33     std::string sarr[] = { "aaaa", "bbb", "ccc", "ddd","eee", "fff", "ggg", "hhh","iii","jjj" };
 34     sLinkList<std::string> sList;
 35     for (int i = 0; i < 10; ++i)
 36         sList.Insert(i, sarr[i]);
 37     sList.Traverse();
 38     std::cout << "---------ReMove---------" << std::endl;
 39     sList.ReMove(0);
 40     sList.Traverse();
 41     std::cout << "---------Insert---------" << std::endl;
 42     sList.Insert(0,"ggg");
 43     sList.Insert(3, "ggg");
 44     sList.Traverse();
 45     std::cout << "---------Erase---------" << std::endl;
 46     sList.Erase("ggg");
 47     sList.Traverse();
 48     std::cout << "---------Search---------" << std::endl;
 49     std::cout << sList.Search("ddd") << std::endl;
 50     std::cout << "---------Visit---------" << std::endl;
 51     std::cout << sList.Visit(5) << std::endl;
 52     std::cout << "---------Inverse---------" << std::endl;
 53     sList.Inverse();
 54     sList.Traverse();
 55 }
 56 
 57 struct MyStruct
 58 {
 59     int id;
 60     char name[20];
 61     void copy(const MyStruct& tmp)
 62     {
 63         id = tmp.id;
 64         for (int i = 0; i < 20; ++i)
 65         {
 66             name[i] = tmp.name[i];
 67         }
 68     }
 69     bool operator ==(const MyStruct&tmp) const
 70     {
 71         if (this->id == tmp.id)
 72         {
 73             for (int i = 0; i < 20; ++i)
 74             {
 75                 if (this->name[i] != tmp.name[i])
 76                     return false;
 77             }
 78         }
 79         else
 80         {
 81             return false;
 82         }
 83         return true;
 84     }
 85     MyStruct& operator =(const MyStruct& right)
 86     {
 87         copy(right);
 88         return *this;
 89     }
 90 
 91     bool operator !=(const MyStruct& right)
 92     {
 93         if (right == *this)
 94             return false;
 95         return true;
 96     }
 97 
 98     friend std::ostream& operator <<(std::ostream &os,const MyStruct&self)
 99     {
100         os << self.id << "  " << self.name;
101         return os;
102     }
103 };
104 static MyStruct Stdudent[10] =
105 {
106     {1,"ge"},
107     {2,"sheng"},
108     {3,"lu"},
109     {4,"ge1"},
110     {5,"sheng1"},
111     {6,"lu1"},
112     {7,"ge2"},
113     {8,"sheng2"},
114     {9,"lu2"},
115     {10,"lu3"},
116 };
117 
118 void ObjectTest()
119 {
120     std::cout << "\n----------------------ObjectTest----------------------" << std::endl;
121     sLinkList<MyStruct> sList;
122     for (int i = 0; i < 10; ++i)
123         sList.Insert(i, Stdudent[i]);
124     sList.Traverse();
125     std::cout << "---------ReMove---------" << std::endl;
126     sList.ReMove(0);
127     sList.Traverse();
128     std::cout << "---------Insert---------" << std::endl;
129     sList.Insert(0, Stdudent[7]);
130     sList.Insert(3, Stdudent[7]);
131     sList.Traverse();
132     std::cout << "---------Erase---------" << std::endl;
133     sList.Erase(Stdudent[7]);
134     sList.Traverse();
135     std::cout << "---------Search---------" << std::endl;
136     std::cout << sList.Search(Stdudent[5]) << std::endl;
137     std::cout << "---------Visit---------" << std::endl;
138     std::cout << sList.Visit(5) << std::endl;
139     std::cout << "---------Inverse---------" << std::endl;
140     sList.Inverse();
141     sList.Traverse();
142 }
143 
144 int main()
145 {
146     IntTest();
147     StringTest();
148     ObjectTest();
149     return 0;
150 }
main.cpp

 

posted @ 2020-02-14 16:31  江南灬  阅读(125)  评论(0)    收藏  举报