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

一个完整链表的实现

时间:2015-09-13 15:52:59      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:

【linearlist.h】:

 1 #include<iostream>
 2 using namespace std;
 3 template <class T>
 4 class LinearList
 5 {
 6 public:
 7     virtual bool IsEmpty() const = 0;
 8     virtual int Length() const = 0;
 9     virtual bool Find(int i, T& x)const = 0;
10     virtual int Search(T x) const = 0;
11     virtual bool Insert(int i, T x) = 0;
12     virtual bool Delete(int i) = 0;
13     virtual bool Update(int i, T x) = 0;
14     virtual void Output(ostream & out) const = 0;
15 protected:
16     int n;
17 };

【singlelist.h】:

  1 #include"linearlist.h"
  2 template <class T> 
  3 class SingleList;
  4 template <class T>
  5 class Node
  6 {
  7 private:
  8     T element;
  9     Node<T> *link;
 10     friend class SingleList<T>;
 11 };
 12 template <class T>
 13 class SingleList:public LinearList<T>
 14 {
 15 public:
 16     SingleList()
 17     {
 18         first = NULL;
 19         n = 0;
 20     }
 21     ~SingleList();
 22     bool IsEmpty() const;
 23     int Length() const;
 24     bool Find(int i, T& x) const;
 25     int Search(T x) const;
 26     bool Insert(int i, T x);
 27     bool Delete(int i);
 28     bool Update(int i, T x);
 29     void Clear();
 30     void Output(ostream& out) const;
 31 private:
 32     Node<T>* first;
 33 };
 34 
 35 template<class T>
 36 SingleList<T>::~SingleList()
 37 {
 38     Node<T> *p;
 39     while (first)
 40     {
 41         p = first->link;
 42         delete first;
 43         first = p;
 44     }
 45 }
 46 template <class T>
 47 int SingleList<T>::Length() const
 48 {
 49     return n;
 50 }
 51 template <class T>
 52 bool SingleList<T>::IsEmpty() const
 53 {
 54     return n == 0;
 55 }
 56 template <class T>
 57 bool SingleList<T>::Find(int i, T& x) const
 58 {
 59     if (i<-1 || i>n - 1)
 60     {
 61         cout << "out of bounds";
 62         return false;
 63     }
 64     Node<T> *p = first;
 65     for (int j = 0; j < i; j++)
 66         p = p->link;
 67     x = p->element;
 68     return true;
 69 }
 70 template <class T>
 71 int SingleList<T>::Search(T x) const
 72 {
 73     int j ;
 74     Node<T> *p = first;
 75     for ( j = 0;  p&&p->element!=x; j++)
 76         p = p->link;
 77     if (p)
 78         return j;
 79     return -1;
 80 }
 81 template <class T>
 82 bool SingleList<T>::Insert(int i, T x)
 83 {
 84     if (i<-1 || i>n - 1)
 85     {
 86         cout << "out of bounds";
 87         return false;
 88     }
 89     Node<T> *q = new Node<T>;
 90     q->element = x;
 91     Node<T> *p = first;
 92     for (int j = 0; j < i; j++)
 93         p = p->link;
 94     if (i>-1)
 95     {
 96         q->link = p->link;
 97         p->link = q;
 98     }
 99     else
100     {
101         q->link = first;
102         first = q;
103     }
104     n++;
105     return true;
106 }
107 template<class T>
108 bool SingleList<T>::Delete(int i)
109 {
110     if (!n)
111     {
112         cout << "underflow" << endl;
113         return false;
114     }
115     if (i<0 || i>n - 1)
116     {
117         cout << "out of bounds"<<endl;
118         return false;
119     }
120     Node<T> *p = first, *q = first;
121     for (int j = 0; j < i - 1; j++)
122         q = q->link;
123     if (i == 0)
124         first = first->link;
125     else
126     {
127         p = q->link;
128         q->link = p->link;
129     }
130     delete p;
131     n--;
132     return true;
133 }
134 template<class T>
135 bool SingleList<T>::Update(int i, T x)
136 {
137     if (i<0 || i>n - 1)
138     {
139         cout << "out of bounds" << endl;
140         return false;
141     }
142     Node<T> *p = first;
143     for (int j = 0; j < i; j++)
144         p = p->link;
145     p->element = x;
146     return true;
147 }
148 template <class T>
149 void SingleList<T>::Output(ostream& out) const
150 {
151     Node<T> *p = first;
152     while (p)
153     {
154         out << p->element << " ";
155         p = p->link;
156     }
157     out << endl;
158 }

【singlelist_temp.h】:

  1 #include"linearlist.h"
  2 template <class T>
  3 class SingleList;
  4 template <class T>
  5 class Node
  6 {
  7 private:
  8     T element;
  9     Node<T> *link;
 10     friend class SingleList<T>;
 11 };
 12 template <class T>
 13 class SingleList :public LinearList<T>
 14 {
 15 public:
 16     SingleList()
 17     {
 18         first = NULL;
 19         n = 0;
 20     }
 21     ~SingleList();
 22     bool IsEmpty() const;
 23     int Length() const;
 24     bool Find(int i, T& x) const;
 25     int Search(T x) const;
 26     bool Insert(int i, T x);
 27     bool Delete(int i);
 28     bool Update(int i, T x);
 29     void Clear();
 30     void Output(ostream& out) const;
 31     void Upstream();
 32     bool SM_Delete(T x);
 33 private:
 34     Node<T>* first;
 35 };
 36 
 37 template<class T>
 38 SingleList<T>::~SingleList()
 39 {
 40     Node<T> *p;
 41     while (first)
 42     {
 43         p = first->link;
 44         delete first;
 45         first = p;
 46     }
 47 }
 48 template <class T>
 49 int SingleList<T>::Length() const
 50 {
 51     return n;
 52 }
 53 template <class T>
 54 bool SingleList<T>::IsEmpty() const
 55 {
 56     return n == 0;
 57 }
 58 template <class T>
 59 bool SingleList<T>::Find(int i, T& x) const
 60 {
 61     if (i<-1 || i>n - 1)
 62     {
 63         cout << "out of bounds";
 64         return false;
 65     }
 66     Node<T> *p = first;
 67     for (int j = 0; j < i; j++)
 68         p = p->link;
 69     x = p->element;
 70     return true;
 71 }
 72 template <class T>
 73 int SingleList<T>::Search(T x) const
 74 {
 75     int j;
 76     Node<T> *p = first;
 77     for (j = 0; p&&p->element != x; j++)
 78         p = p->link;
 79     if (p)
 80         return j;
 81     return -1;
 82 }
 83 template <class T>
 84 bool SingleList<T>::Insert(int i, T x)
 85 {
 86     if (i<-1 || i>n - 1)
 87     {
 88         cout << "out of bounds";
 89         return false;
 90     }
 91     Node<T> *q = new Node<T>;
 92     q->element = x;
 93     Node<T> *p = first;
 94     for (int j = 0; j < i; j++)
 95         p = p->link;
 96     if (i>-1)
 97     {
 98         q->link = p->link;
 99         p->link = q;
100     }
101     else
102     {
103         q->link = first;
104         first = q;
105     }
106     n++;
107     return true;
108 }
109 template<class T>
110 bool SingleList<T>::Delete(int i)
111 {
112     if (!n)
113     {
114         cout << "underflow" << endl;
115         return false;
116     }
117     if (i<0 || i>n - 1)
118     {
119         cout << "out of bounds" << endl;
120         return false;
121     }
122     Node<T> *p = first, *q = first;
123     for (int j = 0; j < i - 1; j++)
124         q = q->link;
125     if (i == 0)
126         first = first->link;
127     else
128     {
129         p = q->link;
130         q->link = p->link;
131     }
132     delete p;
133     n--;
134     return true;
135 }
136 template<class T>
137 bool SingleList<T>::Update(int i, T x)
138 {
139     if (i<0 || i>n - 1)
140     {
141         cout << "out of bounds" << endl;
142         return false;
143     }
144     Node<T> *p = first;
145     for (int j = 0; j < i; j++)
146         p = p->link;
147     p->element = x;
148     return true;
149 }
150 template <class T>
151 void SingleList<T>::Output(ostream& out) const
152 {
153     Node<T> *p = first;
154     while (p)
155     {
156         out << p->element << " ";
157         p = p->link;
158     }
159     out << endl;
160 }
161 template<class T>
162 void SingleList<T>::Upstream()
163 {
164     Node<T> *p = first;
165     Node<T> *curr = p->link;
166     Node<T> *tmp = NULL;
167 
168     p->link = NULL;
169     while (curr)
170     {
171         tmp = curr->link;
172         curr->link = p;
173         p = curr;
174         curr = tmp;
175 
176     }
177 
178     first->link = NULL;
179     first = p;
180 }
181 
182 template<class T>
183 bool  SingleList<T>::SM_Delete(T x)
184 {
185     /*
186     for (Node<T>** cur = &first; *cur;)
187     {
188         Node<T>* entry = *cur;
189         if (entryement == x)
190         {
191             *cur = entry->link;
192             free(entry);
193         }
194         else
195             cur = &entry->link;
196     }
197     */
198     Node<T> *p;
199     int j = 0;
200     p = first;
201     while (p)
202     {
203         for (j = 0; p&&p->element != x; j++)
204             p = p->link;
205         if (p)
206         {
207             Delete(j);
208             p = first;
209         }
210         else
211             return false;
212     }
213     return true;
214 }

【SingleList.cpp(测试用)】:

 1 #include"singlelist_temp.h"
 2 template<class T>
 3 void Intersection(SingleList<T> &LA, SingleList<T> &LB)
 4 {
 5     T x;
 6     int i = 0;
 7     while (i < LA.Length())
 8     {
 9         LA.Find(i, x);
10         if (LB.Search(x) == -1)
11             LA.Delete(i);
12         else i++;
13     }
14 }
15 int main()
16 {
17     SingleList<int> LA;
18     //SingleList<int> LB;
19     /*
20     for (int i = 0; i < 10; i++)
21          LA.Insert(i-1, i);
22         */
23          LA.Insert(-1, 1);
24          LA.Insert(0, 2);
25          LA.Insert(1, 3);
26          LA.Insert(2, 2);
27          LA.Insert(3, 4);
28          LA.Insert(4, 2);
29 
30          LA.Insert(5, 5);
31          
32     LA.Output(cout);
33     /*
34     for (int i = 5; i < 10; i++)
35          LB.Insert(i - 6, i);
36     LB.Output(cout);
37     LB.Insert(-1, 0);
38     LB.Output(cout);
39     LB.Insert(3, 2);
40     LB.Output(cout);
41     Intersection(LA, LB);
42     */
43     LA.Upstream();
44     LA.SM_Delete(2);
45     LA.Output(cout);
46     system("pause");
47 }

 

一个完整链表的实现

标签:

原文地址:http://www.cnblogs.com/zlgxzswjy/p/4804945.html

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