C++类实现顺序表和双向链表

C++类实现顺序表与双向链表

顺序表

1.#include <assert.h>
2.#include <iostream>
3.using namespace std;
4.
5.typedef int DataType;
6.
7.class Seqlist
8.{
9.public:
10. Seqlist()
11. :_pData(new DataType[3])
12. ,_capacity(3)
13. ,_size(0)
14. {}
15.
16. Seqlist(size_t n, DataType value)
17. { //构造出n个值为value的顺序表
18. _pData = new DataType[n];
19. _capacity = n;
20. _size = n;
21. for(size_t idx = 0; idx<n; idx++)
22. _pData[idx] = value;
23. }
24.
25. Seqlist(const Seqlist& s)
26. :_pData(NULL)
27. { //深拷贝
28. _pData = new DataType(s._capacity);
29. _size = s._size;
30. _capacity = s._capacity;
31. for(size_t i = 0; i<s._size; ++i)
32. {
33. _pData[i] = s._pData[i];
34. }
35. }
36.
37. Seqlist& operator=(const Seqlist& s)
38. {
39. if(this != &s)
40. {
41. delete[] _pData;
42. _pData = new DataType(_capacity);
43. for(size_t i = 0; i < s._size; i++)
44. _pData[i] = s._pData[i];
45. _size = s._size;
46. _capacity = s._capacity;
47. }
48. return *this;
49. }
50.
51. ~Seqlist()
52. {
53. if(_pData != NULL)
54. {
55. delete[] _pData;
56. _size = 0;
57. _capacity = 0;
58. }
59.
60. }
61.
62. void Display()
63. {
64. for(size_t i = 0; i < _size; ++i)
65. cout<<_pData[i]<<" ";
66. cout<<endl;
67. cout<<"_size = "<<_size<<endl;
68. cout<<"_capacity = "<<_capacity<<endl;
69. }
70.
71. void PushBack(const DataType& data)
72. {
73. _CheckCapacity();
74. _pData[_size] = data;
75. ++_size;
76. }
77.
78. void PopBack()
79. {
80. if(_size == NULL)
81. return;
82. --_size;
83. }
84.
85. void Insert(size_t pos, const DataType& data)
86. {
87. _CheckCapacity();
88. if(pos >= _size)
89. return;
90. for(size_t i = _size-1; i>=pos; --i)
91. _pData[i+1] = _pData[i];
92. _pData[pos] = data;
93. _size++;
94. }
95.
96. void Erase(size_t pos)
97. {
98. if(pos >= _size)
99. return;
100. for(size_t i = pos; i<_size-1; ++i)
101. _pData[i] = _pData[i+1];
102. --_size;
103. }
104.
105. int Find(const DataType& data)
106. { //查找第一个出现data的位置
107. for(size_t i = 0; i<_size; ++i)
108. {
109. if(data == _pData[i])
110. return i;
111. }
112. return -1;
113. }
114.
115. size_t Size()const
116. {
117. return _size;
118. }
119. size_t Capacity()const
120. {
121. return _capacity;
122. }
123.
124. bool Empty()const
125. {
126. if(_size == 0)
127. return true;
128. return false;
129. }
130.
131. void clear()
132. {
133. _size = 0;
134. _capacity = 3;
135. }
136.
137. void Resize(size_t size, const DataType& data)//把顺序表中的有用元素改变到size个,如果不够则在后面加到size个data
138. {
139. if(size <= _size)
140. {
141. _size = size;
142. }
143. else if(size <= _capacity)
144. {
145. for(size_t i = _size; i<size; ++i)
146. _pData[i] = data;
147. _size = size;
148. }
149. else
150. {
151. for(size_t i = _size; i<size; ++i)
152. {
153. _CheckCapacity();
154. _pData[i] = data;
155. ++_size;
156. }
157. }
158. }
159.
160. DataType& operator[](size_t index)//输入一个下标,返回下标对应的数据(既能读又能写)
161. { //1、index在范围内 2、index超范围
162. assert(index < _size);
163. return _pData[index];
164. }
165.
166. const DataType& operator[](size_t index)const//只读,在找到下表对应的数据后不能够改变
167. {
168. assert(index < _size);
169. return _pData[index];
170. }
171.
172. DataType& Front()
173. {
174. return *_pData;
175. }
176.
177. const DataType& Front()const
178. {
179. return *_pData;
180. }
181.
182. DataType& Back()
183. {
184. return _pData[_size-1];
185. }
186.
187. const DataType& Back()const
188. {
189. return _pData[_size-1];
190. }
191.private:
192. void _CheckCapacity()
193. {
194. if(_size == _capacity)
195. {
196. DataType *pTemp = new DataType[_capacity*2];
197. for(size_t i = 0; i<_size; ++i)
198. pTemp[i] = _pData[i];
199. delete[] _pData;
200. _pData = pTemp;
201. _capacity *= 2;
202. }
203. }
204.
205.private:
206. DataType* _pData;
207. size_t _capacity;
208. size_t _size;
209.};
210.
211.
212.
213.void test1()
214.{
215. /*Seqlist s1(4, 2);
216. s1.Display();*/

217. Seqlist s1;
218. s1.PushBack(1);
219. s1.PushBack(2);
220. s1.PushBack(3);
221. s1.PushBack(4);
222. s1.PushBack(5);
223. s1.PushBack(6);
224. s1.PushBack(7);
225. //s1.Resize(13,0);
226. //s1.PopBack();
227. //s1.Insert(3,6);
228. //s1.Insert(3,7);
229. //s1.Erase(3);
230. //int ret = s1.Find(4);
231. //s1.clear();
232. //Seqlist s2(s1);
233. cout<<s1.Back()<<endl;
234. s1.Display();
235. //s2.Display();
236. //cout<<ret<<endl;
237.}
238.
239.
240.int main()
241.{
242. test1();
243. system("pause");
244. return 0;
245.}

双向链表

1.#include <iostream>
2.using namespace std;
3.
4.typedef int DataType;
5.struct Node
6.{
7. Node(const DataType& data)
8. : _pNext(NULL)
9. , _pPre(NULL)
10. , _data(data)
11. {}
12.
13. Node* _pNext;
14. Node* _pPre;
15. DataType _data;
16.};
17.
18.class List
19.{
20.public:
21. List()
22. : _pHead(NULL)
23. {}
24.
25. List(size_t n, DataType data)//构造n个值为data个链表
26. {
27. if(n <= 0)
28. _pHead = NULL;
29. else if(n == 1)
30. _pHead = BuyNode(data);
31. else
32. {
33. _pHead = BuyNode(data);
34. Node* head = _pHead;
35. while(n--)
36. {
37. head->_pNext = BuyNode(data);
38. head->_pNext->_pPre = head;
39. head = head->_pNext;
40. }
41. }
42. }
43.
44. List(const List& l)
45. { //深拷贝
46. //将l里面的数据拷贝过来,并将数据关系也拷贝过来
47. if(l._pHead == NULL)
48. _pHead = NULL;
49. else
50. {
51. Node* tmp = l._pHead;
52. _pHead = BuyNode(tmp->_data);
53. while(tmp->_pNext)
54. {
55. PushBack(tmp->_pNext->_data);
56. tmp = tmp->_pNext;
57. }
58. }
59. }
60. List& operator=(const List& l)
61. {
62. List tmp(l);
63. swap(tmp._pHead,_pHead);
64. return *this;
65. }
66.
67. ~List()
68. {
69. if(_pHead == NULL)
70. return;
71. Node *head = _pHead;
72. Node *tail = NULL;
73. while(head->_pNext)
74. {
75. head = head->_pNext;
76. }
77. while(head != _pHead)
78. {
79. tail = head->_pPre;
80. delete head;
81. head = tail;
82. }
83. _pHead = NULL;
84. }
85.
86.
87.void Display()
88.{
89. if(_pHead == NULL)
90. return;
91. Node *head = _pHead;
92. while(head)
93. {
94. cout<<head->_data<<" ";
95. head = head->_pNext;
96. }
97. cout<<endl;
98.}
99.
100.void PushBack(const DataType& data)
101.{ //尾插一个data的数据并且NEXT指针指向NULL
102. //1、一个结点也没有
103. //2、有一个或多个节点
104. Node *head = _pHead;
105. if(_pHead == NULL)
106. _pHead = BuyNode(data);
107. else
108. {
109. while(head->_pNext)
110. {
111. head = head->_pNext;
112. }
113. head->_pNext = BuyNode(data);
114. head->_pNext->_pPre = head;
115. }
116.}
117.
118.void PopBack()
119.{ //1、链表为空 2、只有一个节点时 3、多个节点
120. if(_pHead == NULL)
121. _pHead = NULL;
122. else if(_pHead->_pNext == NULL)
123. {
124. delete _pHead;
125. _pHead = NULL;
126. }
127. else
128. {
129. Node *head = _pHead;
130. while(head->_pNext)
131. {
132. head = head->_pNext;
133. }
134. Node *tail = head->_pPre;
135. delete head;
136. tail->_pNext = NULL;
137. }
138.}
139.
140.void PushFront(const DataType& data)
141.{ //1、一个节点也没有 2、有多个节点
142. if(_pHead == NULL)
143. _pHead = BuyNode(data);
144. else
145. {
146. Node *head = _pHead;
147. head->_pPre = BuyNode(data);
148. head->_pPre->_pNext = head;
149. _pHead = head->_pPre;
150. }
151.
152.}
153.
154.void PopFront()
155.{ //1、没有节点 2、只有一个节点 3、有多个节点
156. if(_pHead == NULL)
157. _pHead = NULL;
158. else if(_pHead->_pNext == NULL)
159. {
160. delete _pHead;
161. _pHead = NULL;
162. }
163. else
164. {
165. Node* head = _pHead;
166. _pHead = _pHead->_pNext;
167. _pHead->_pPre = NULL;
168. delete head;
169. head = NULL;
170. }
171.
172.}
173.
174.Node* Find(const DataType& data)
175.{
176. Node *head = _pHead;
177. if(head->_data == data)
178. return head;
179. while(head->_pNext)
180. {
181. head = head->_pNext;
182. if(head->_data == data)
183. return head;
184. }
185. return NULL;
186.}
187.
188.void Insert(Node* pos, const DataType& data)//在pos的前面插入数据
189.{ //1、没有节点(前插)或者在第一个位置插入(前插) 2、在其他位置插入
190. if(_pHead == NULL || pos == Find(1))
191. PushFront(data);
192. Node *node = BuyNode(data);
193. node->_pNext = pos;
194. pos->_pPre->_pNext = node;
195. node->_pPre = pos->_pPre;
196. pos->_pPre = node;
197.}
198.
199.void Erase(Node* pos)
200.{ //1、没有节点 2、删除头结点 3、删除尾节点 4、非头非尾
201. if(_pHead == NULL)
202. return;
203. else
204. {
205. Node *head = _pHead;
206. while(head->_pNext)
207. head = head->_pNext;
208. if(pos == Find(1))
209. PopFront();
210. else if(pos == head)
211. {
212. PopBack();
213. }
214. else
215. {
216. pos->_pPre->_pNext = pos->_pNext;
217. pos->_pNext->_pPre = pos->_pPre;
218. delete pos;
219. pos = NULL;
220. }
221. }
222.}
223.
224.size_t Size()
225.{
226. Node* head = _pHead;
227. size_t count = 0;
228. while(head)
229. {
230. count++;
231. head = head->_pNext;
232. }
233. return count;
234.}
235.
236.bool Empty()const
237.{ //判断Size是否为0
238. if(_pHead == NULL)
239. return true;
240. return false;
241.}
242.
243.
244.private:
245.Node* BuyNode(const DataType& data)
246.{
247. return new Node(data);
248.}
249.private:
250. Node* _pHead;
251.};
252.
253.void FunTest()
254.{
255. List s1;
256. //s1.PushBack(1);
257. //s1.PushBack(2);
258. //s1.PushBack(3);
259. //s1.PushBack(4);
260.
261. //s1.PopBack();
262. //s1.PopFront();
263. //List s2(s1);
264. //List s2;
265. //s2 = s1;
266. //s1.PushFront(1);
267. //s1.PushFront(2);
268. //s1.PushFront(3);
269. //s1.PushFront(4);
270.
271. //s1.Insert(s1.Find(3), 5);
272. //s1.Erase(s1.Find(5));
273. //cout<<s1.Size()<<endl;
274. cout<<s1.Empty()<<endl;
275. s1.Display();
276. //s2.Display();
277.}
278.
279.int main()
280.{
281. FunTest();
282. system("pause");
283. return 0;
284.}
285.
所有接口均已通过测试案例


猜你喜欢

转载自blog.csdn.net/prokgtfy9n18/article/details/75668901