c++实现顺序表中的基本操作:
1、顺序表的初始化
2、顺序表的创建
3、顺序表的插入
4、顺序表的删除
5、顺序表的查找
6、顺序表的取值
7、顺序表的清空
8、顺序表的长度
9、顺序表的判空
10、顺序表的打印
复制代码
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
301
302
303
304/* Project: sequence_list(数据结构-顺序表) Date: 2021/11/15 InitList(SqList &L) 参数:顺序表L 功能:初始化 时间复杂度:O(1) CreateList(SqList &L,int n) 参数:顺序表L,顺序表长度n 功能:创建长度为n的顺序表 时间复杂度:O(n) InsertList(SqList &L,int i,ElemType e) 参数:顺序表L,位置i,元素e 功能:位置i处插入元素e 时间复杂度:O(n) DeleteList(SqList &L,int i) 参数:顺序表L,位置i 功能:删除位置i处元素 时间复杂度:O(n) LocateDate(SqList L,ElemType e) 参数:顺序表L,元素e 功能:返回第一个等于e的元素的位置 时间复杂度:O(n) GetData(SqList L, int i, ElemType &e) 参数:顺序表L,位置i,元素e 功能:取得i处的元素e 时间复杂度:O(1) ClearList(SqList &L) 参数:顺序表L 功能:清空顺序表 GetLength(SqList L) 参数:顺序表L 功能:求顺序表的长度 IsEmpty(SqList L) 参数:顺序表L 功能:判断顺序表是否为空 PrintList(SqList L) 参数:顺序表L 功能:遍历并输出顺序表 */ #include<iostream> #define Maxsize 100 #define ElemType char//ElemType 可根据需求自行定义 using namespace std; //顺序表数据结构 typedef struct { /*ElemType是数据结构的书上为了说明问题而用的一个词。它是element type(“元素的类型”)的简化体。*/ ElemType *data;//顺序表元素 int length;//顺讯表当前长度 }SqList; //*******************************基本操作函数****************************** //顺序表的初始化,创建一个空的顺序表 int Initlist(SqList &L) { L.data = new ElemType[Maxsize];//为顺序表分配一个大小为Maxsize的数组空间 if (!L.data) exit(OVERFLOW);//存储空间分配失败 L.length = 0;//空表长度为0 return 0; } //顺序表的创建,创建一个长度为n的顺序表 bool CreateList(SqList& L, int n) { if (n<0 || n>Maxsize) { return false; } else for (int i = 0; i < n; i++) { cout << "请输入第" << i+1 << "个元素:" << endl; cin >> L.data[i]; L.length++; } return true; } //顺序表的插入,第i个位置插入新元素e bool InsertList(SqList& L, int i, ElemType e) { if (i<1 || i>L.length + 1) { return false;//插入位置不合法 } if (L.length == Maxsize) { return false;//当前存储空间已满 } for (int j = L.length; j >= i; j--) { L.data[j] = L.data[j - 1]; } L.data[i - 1] = e; L.length++; return true; } //顺序表的删除,删除第i个元素 bool DeleteList(SqList& L,int i) { if (i<1 || i>L.length) { return false;//删除位置不合法 } for (int j = i; j < L.length; j++) { L.data[j - 1] = L.data[j]; } L.length--; return true; } //顺序表的查找,按值查找 int LocateData(SqList L, ElemType e) { for (int i = 0; i < L.length; i++) { if (L.data[i] == e) { return i + 1;//查找成功,返回其序号 } } return 0; } //顺序表的取值,取得第i个元素的值 bool GetData(SqList L, int i, ElemType &e) { if (i<1 || i>L.length) { return false; } else { e = L.data[i - 1]; return true; } } //*********************************功能函数******************************** //1、 顺序表的创建函数 void Create(SqList& L) { int n; bool flag; cout << "请输入要创建的顺序表长度:" << endl; while (1) { cin >> n; flag = CreateList(L, n); if (flag) { cout << "顺序表创建成功!" << endl; break; } else { cout << "顺序表创建失败!" << endl; cout << "请重新输入要创建数据表的长度:" << endl; } } } //2、顺序表的插入函数 void Insert(SqList& L) { int place; ElemType e; bool flag; cout << "请输入元素要插入的位置:" << endl; cin >> place; cout << "请输入元素要插入的元素:" << endl; cin >> e; flag = InsertList(L, place, e); if (flag) { cout << "顺序表插入成功!" << endl; } else { cout << "顺序表插入失败!" << endl; } } //3、顺序表的删除函数 void Delete(SqList& L) { int place; bool flag; cout << "请输入要删除元素的位置:" << endl; cin >> place; flag = DeleteList(L, place); if (flag) { cout << "顺序表的元素删除成功!" << endl; } else { cout << "顺序表的元素删除失败!" << endl; } } //4、顺序表的查找函数 void Search(SqList L) { ElemType e; int place; cout << "请输入要查找的值:" << endl; cin >> e; place = LocateData(L, e); if (place) cout << "该元素的位置为:" << place << endl; else cout << "没有找到该元素" << endl; } //5、顺序表的取值函数 void Get(SqList L) { int place; ElemType e; bool flag; cout << "请输入要取元素的位置:" << endl; cin >> place; flag=GetData(L, place, e); if (flag) { cout << "取得的元素为:" << e << endl; } else cout << "取值失败!" << endl; } //6、顺序表的清空函数 void ClearList(SqList& L) { L.length = 0; } //7、顺序表的求长函数 void GetLength(SqList L) { cout << "顺序表的长度为:" << L.length << endl; } //8、顺序表的判空函数 void IsEmpty(SqList L) { if (L.length == 0) cout << "顺序表为空" << endl; else cout << "顺序表不为空" << endl; } //9、打印输出当前顺序表的所有元素 void PrintList(SqList L) { cout << "当前顺序表的所有元素分别为:" << endl; for (int i = 0; i < L.length; i++) { cout << "第" << i + 1 << "个元素为:" << L.data[i] << endl; } } //菜单 void menu() { cout << "************************************************" << endl; cout << "*********************1、创建********************" << endl; cout << "*********************2、插入********************" << endl; cout << "*********************3、删除********************" << endl; cout << "*********************4、查找********************" << endl; cout << "*********************5、取值********************" << endl; cout << "*********************6、清空********************" << endl; cout << "*********************7、求长********************" << endl; cout << "*********************8、判空********************" << endl; cout << "*********************9、打印********************" << endl; cout << "*********************0、退出********************" << endl; cout << "************************************************" << endl; } int main() { SqList L; Initlist(L); int select; while (1) { system("cls");//进行清屏操作 menu(); cout << "请输入菜单序号:" << endl; cin >> select; switch (select) { case 1: Create(L);//创建 system("pause");//按任意键继续 break; case 2: Insert(L);//插入 system("pause"); break; case 3: Delete(L);//删除 system("pause"); break; case 4: Search(L);//查找 system("pause"); break; case 5: Get(L);//取值 system("pause"); break; case 6: ClearList(L);//清空 system("pause"); break; case 7: GetLength(L);//求长 system("pause"); break; case 8: IsEmpty(L);//判空 system("pause"); break; case 9: PrintList(L);//打印 system("pause"); break; case 0: cout << "欢迎下次使用!" << endl;//退出 system("pause"); return 0; break; default: cout << "菜单序号输入有误!" << endl; system("pause"); break; } } system("pause"); return 0; }
注意:ElemType 可根据需求自行定义,将其定义成合适的结构体形式,对以上代码稍作修改,便可以实现通讯录管理系统或图书信息管理系统的一些基本操作。
例如:图书信息管理系统可对ElemType进行如下定义。
复制代码
1
2
3
4
5
6typedef struct { string number;//图书编号 string name;//图书名字 float price;//图书价格 }ElemType;
参考资料:
《数据结构》(C语言版)严蔚敏
数据结构-顺序表基本操作的实现(含全部代码)_lady_killer9的博客-CSDN博客_数据结构顺序表代码
最后
以上就是忧心黑米最近收集整理的关于C++实现顺序表c++实现顺序表中的基本操作:的全部内容,更多相关C++实现顺序表c++实现顺序表中内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复