本文实例为大家分享了C++实现线性表链式存储的具体代码,供大家参考,具体内容如下
实现的功能:
1、定义了三中传入不同参数的构造函数,用于初始化创建不同的链表;
2、能实现增、删、查等基本功能;
存在的问题:
当创建一个已知大小的空链表后,链表中的数据并不为空,见下图:
下面是代码及测试结果:
singlelinklist.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
#pragma once #include "iostream" #include "exception" #include "stdlib.h" #include "malloc.h" using namespace std; //结点类 template < class T> class Node { public : T data; Node<T> *next; Node() { this ->next = NULL; } Node(T data, Node<T>* next = NULL) { this ->data = data; } ~Node() {}; }; //定义链表类 template < class T> class SLinkList { public : Node<T> node; //结点 Node<T>* head; //头指针 SLinkList(); //创建链表 SLinkList( int num, T elem[]); SLinkList( int num); ~SLinkList(); int LengthSLinkList(); //表长 void InsertNode( int i, T elem); //插入结点 void InsertNode(T elem); void DeleteNode( int i); //删除结点 void DeleteAllNode(); //删除表 T GetElem( int i); //按序号查找 int * GetNum(T elem); //按元素查找 void OutputList(); //输出 }; template < class T> SLinkList<T>::SLinkList() { Node<T>* p = new Node<T>(); this ->head = p; cout << "finish<SLinkList()>!" << endl; } template < class T> SLinkList<T>::SLinkList( int num, T elem[]) { try { if (num < 1) throw length_error( "输入初始化num参数必须大于1!" ); else { Node<T>* p = new Node<T>(); this ->head = p; Node<T>* temp = this ->head; for ( int i = 0; i < num; i++) { temp->next = new Node<T>(*(elem + i)); //这里犯了一个错,就是把elem[i]直接丢进去,()里面放的是T类型初始值,实参传进来的是T* elem[],无法用下标进行访问 temp = temp->next; } temp->next = NULL; cout << "finish<SL]inkList(int num, T elem[])>!" << endl; } } catch (length_error e) { cout << "info:" << e.what() << endl; exit (1); } } template < class T> SLinkList<T>::SLinkList( int num) { try { if (num < 1) throw length_error( "输入初始化参数num必须大于1!" ); else { Node<T>* p = new Node<T>(); this ->head = p; Node<T>* temp = this ->head; for ( int i = 1; i <= num; i++) { temp->next = new Node<T>(); temp = temp->next; } temp->next = NULL; cout << "finish<SLinkList(int num)>!" << endl; } } catch (length_error e) { cout << "info:" << e.what() << endl; exit (0); } } //调用函数析构 /* template<class T> SLinkList<T>::~SLinkList() { this->DeleteAllNode(); } */ template < class T> SLinkList<T>::~SLinkList() { Node<T>* p = this ->head; Node<T>* temp = p->next; Node<T>* q = NULL; while (temp) { q = temp; temp = temp->next; delete q; } delete p; delete temp; cout << "finish ~SLinkList()" << endl; } template < class T> int SLinkList<T>::LengthSLinkList() { int count = 0; Node<T>* p = this ->head; Node<T>* temp = p->next; while (temp) { count++; temp = temp->next; } return count; } template < class T> void SLinkList<T>::InsertNode( int i, T elem) { try { if (i<1 || i>LengthSLinkList() + 1) throw out_of_range( "输入的参数i值超出了链表的范围!" ); else { if (i != LengthSLinkList()+1) //1-len之间插入 { Node<T>* Elem = new Node<T>(elem); Node<T>* temp = this ->head; for ( int j = 1; j < i; j++) temp = temp->next; Node<T>* p = temp->next; temp->next = Elem; Elem->next = p; } else InsertNode(elem); //在末尾插入 cout << "finish<Insert(int i, T elem)>!" << endl; } } catch (out_of_range e) { cout << "info:" << e.what() << endl; exit (0); } } template < class T> void SLinkList<T>::InsertNode(T elem) //末尾插入 { Node<T>* Elem = new Node<T>(elem); Node<T>* temp = this ->head; Node<T>* p = NULL; while (temp) { p = temp; temp = temp->next; } p->next = Elem; Elem->next = NULL; cout << "finnish<InsertNode(T elem)>!" << endl; } template < class T> void SLinkList<T>::DeleteNode( int i) //返回的是被删除的值 { try { if (i<1 || i > LengthSLinkList()) throw out_of_range( "输入的参数i值超出了链表的范围!" ); else { Node<T>* temp = this ->head; for ( int j = 1; j < i; j++) temp = temp->next; Node<T> *p = temp->next; temp->next = p->next; cout << "finish<DeleteNode(int i)>!" << endl; } } catch (out_of_range e) { cout << "info:" << e.what() << endl; exit (1); } } template < class T> void SLinkList<T>::DeleteAllNode() { Node<T>* temp = this ->head; int count = this ->LengthSLinkList(); while (count) { DeleteNode(count); count--; } delete temp; cout << "finish<DeleteAllNode()>!" << endl; } template < class T> T SLinkList<T>::GetElem( int i) { try { if (i<1 || i > LengthSLinkList()) throw out_of_range( "输入参数i值超出了链表的范围!" ); else { Node<T>* temp = this ->head; for ( int j = 1; j <=i; j++) temp = temp->next; cout << "finish!" << endl; cout << "finish<GetElem(int i)>! " << endl; return temp->data; } } catch (out_of_range e) { cout << "info:" << e.what() << endl; exit (1); } } template < class T> int * SLinkList<T>::GetNum(T elem) { Node<T>* p = this ->head; Node<T>* temp = p->next; int * count = new int [LengthSLinkList()]; int j = 0; for ( int i = 1; i <= LengthSLinkList(); i++) { if (temp->data == elem) { count[j] = i; j++; } else temp = temp->next; } cout << "finish<GetNum(T elem)>!" << endl; return count; } template < class T> void SLinkList<T>::OutputList() { Node<T>* p = this ->head; Node<T>* temp = p->next; cout << "output list:" ; for ( int i = 1; i <= LengthSLinkList(); i++) { cout << temp->data << " " ; temp = temp->next; } cout << endl; } |
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
#include "iostream" #include "singlelinklist.h" using namespace std; int main() { //Init:测试初始化构造函数 //operator:测试增、删、查 //标注行代码为异常测试,主要包括length_error和out_of_range //最后输出的是析构函数,下面代码行中未给出,见.h文件中的析构函数定义 cout << "**************************************************" << endl; cout << "test Innit<SLinkList()>" << endl; SLinkList< int > sqlinklist1; cout << "**************************************************" << endl; cout << "test Init<SLinkList(int num, T elem[])>" << endl; int num = 5; int a[] = {1, 2, 3, 4, 5}; cout << "input list:" ; for ( int i = 0; i < num; i++) cout << a[i] << " " ; cout << endl; SLinkList< int > sqlinklist2(num, a); cout << "length:" << sqlinklist2.LengthSLinkList() << endl; sqlinklist2.OutputList(); cout << endl; //cout << "test error" << endl; //SLinkList<int> error1(0, a); cout << "**************************************************" << endl; cout << "test Init<SLinkList(int num)>" << endl; int num1 = 5; SLinkList< int > slistlink3(num1); cout << "length:" << slistlink3.LengthSLinkList() << endl; slistlink3.OutputList(); cout << endl; //cout << "test error" << endl; //SLinkList<int> errror(0); cout << "**************************************************" << endl; cout << "test operation<InsertNode(int i, T elem)>" << endl; sqlinklist2.InsertNode(2, 1996); sqlinklist2.OutputList(); cout << endl; //cout << "test error" << endl; //error.InsertNode(0, 0); //error.InsertNode(7, 0); cout << "test operation<InsertNode(T elem)>" << endl; sqlinklist2.InsertNode(256); sqlinklist2.OutputList(); cout << endl; //cout << "test error" << endl; //error.InsertNode(0); //error.InsertNode(7); cout << "**************************************************" << endl; cout << "test operation<DeleteNode(int i)>" << endl; sqlinklist2.DeleteNode(1); sqlinklist2.OutputList(); cout << endl; //cout << "test error" << endl; //error.DeleteNode(18); //cout << "test operation<DeleteAllNode()>" << endl; //sqlinklist2.DeleteAllNode(); cout << "**************************************************" << endl; cout << "test operation<GetElem(int i)>" << endl; cout << "num '2' is:" << sqlinklist2.GetElem(2) << endl; //cout << "test error" << endl; //error.DeleteNode(18); cout << "test operation<GetNum(T elem)>" << endl; cout << "elem '2' is :" << *(sqlinklist2.GetNum(2)) << endl; cout << endl; return 0; } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_33482723/article/details/98602861