我是靠谱客的博主 精明犀牛,这篇文章主要介绍C++实现顺序表,进阶版,现在分享给大家,希望可以做个参考。

复制代码
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
//编写程序,建立并显示出一个有10个数据元素的顺序线性表 //实现顺序线性表的插入,查找,删除等算法 #include<iostream> #include<stdio.h> #define LIST_INIT_SIZE 100 //表长度初始定义 #define LIST_INCREMENT 10 //顺序表的最大长度 using namespace std; struct Sqlist { long *elem,*newlist; int Length; int listsize; }; void Error(const char *s) { cout << s << endl; exit(1); } void InitList_Sq(Sqlist &L) //创建有若干个数据元素的顺序表,引用型指针, 初始化表 { L.elem = new long[LIST_INIT_SIZE]; //C++ 开辟内存空间 if (!L.elem) //如果无法开辟 Error("asd"); //输出错误信息 L.Length = 0; //初始长度为0 L.listsize = LIST_INIT_SIZE; //表长为10,存放十个元素 } void Create_Sq(Sqlist *L) { int i, num; cout << "输入顺序表元素个数"; cin >> num; cout << "请输入元素数据"; for (i = 0; i < num; i++)//循环向数组输入元素 { cin >> L->elem[i]; L->Length++; } cout << "创建顺序表成功" << endl; } void Increment(Sqlist &L) //为顺序表扩展LIST_INCREMEN个数据元素空间 { L.newlist = new long[L.listsize + LIST_INCREMENT]; //增加LIST_INCREMENT更存储空间 if (!L.newlist) { Error("error"); } for (int i = 0; i < L.Length; i++)//将原空间中的数据元素挪到新的数据空间中 { L.newlist[i] = L.elem[i]; }//释放元素所占用空间 L.elem = L.newlist;//移交空间首地址; delete[] L.elem; L.listsize += LIST_INCREMENT;//修改当前顺序表的最大空间; } void ListInsert_Sq(Sqlist &L, int i, int e) //在顺序表中第i个位置插入元素e;若插入位置.不合理则给出相关信息并退出运行 //i的合理范围是1<=i<=L.Length+1 { if ((i < 1) || (i > L.Length + 1)) Error("Position 错误"); if (L.Length >= LIST_INIT_SIZE)//若当前存储空间已满,则增加空间 Increment(L); //增加空间 long *q = &(L.elem[i - 1]); //指针q指向插入位置 long *p = &(L.elem[L.Length - 1]); for (p; p >= q; p--) //向后移动元素 { *(p + 1) = *p; } *q = e; //在L的第i个位置插入元素e L.Length++; //修改当前顺序表长度 } void ListDelete_Sq(Sqlist &L, int i, int &e) //删除表中的i元素并用e返回其值 { if (i<1 || i>L.Length) //删除元素的参数不合理 Error("Position cuowu"); e = L.elem[i - 1];//将待删除的元素的值赋给e long *p = (&L.elem[i - 1]); //指向待删除元素的位置 for (++p; p <= (L.elem + L.Length - 1); p++) //向前移动元素 { *(p - 1) = *p; } L.Length--; //修改L长度 cout << "删除元素是"; cout << e << endl; } int LocatElem_Sq(Sqlist L, int e)//查找e元素,若找到,则返回元素位序,否则返回0 { int j = 1; long *q = L.elem; while ((j<=L.Length)&&(*q!=e)) { j++; q++; } if (j > L.Length) return j; else return 0; } void getElem_Sq(Sqlist L, int i, int &e)//输出顺序表中要操作的那个元素 { if ((i < 1) || (i > L.Length)) Error("Position cuowu"); e = L.elem[i - 1]; //用e返回顺序表中第i个元素的值 cout << e << endl; } void printElem_Sq(Sqlist L)//输出顺序表中所有元素 { cout << "如下" << endl; int i; for (i = 0; i < L.Length; i++) { cout << " " << L.elem[i]; } cout << endl; } int mail() { Sqlist L; int e, n, number; while (1) { cout << " 1、创建信息表" << endl; cout << " 2、插入元素" << endl; cout << " 3、查询元素" << endl; cout << " 4、删除元素" << endl; cout << " 5、退出程序" << endl; cout << " 请选择所要执行的操作:"; cin >> n; switch (n) { case 1: InitList_Sq(L); Create_Sq(&L); printElem_Sq(L); break; case 2: cout << "请输入插入位置元素"; cin >> number >> e; while (e != 0) { ListInsert_Sq(L, number, e); printElem_Sq(L); cout << "请输入插入的位置和元素:"; cin >> number >> e; } case 3: cout << "请输入查找的元素:"; cin >> e; while (e != 0) { LocatElem_Sq(L, e); cout << "该元素所在顺序表的位置位置是:" << LocatElem_Sq(L, e) << endl; cout << "该元素是:"; getElem_Sq(L, LocatElem_Sq(L, e), e); cout << "请输入查找的元素:"; cin >> e; } break; case 4: cout << "请输入要删除的位置 :"; cin >> number; ListDelete_Sq(L, number, e); printElem_Sq(L); break;//程序结束 case 5: exit(1); break;//程序结束 default: cout << "输入错误,请重新输入!!!!!" << endl; continue; } break; } return 0; }

 

最后

以上就是精明犀牛最近收集整理的关于C++实现顺序表,进阶版的全部内容,更多相关C++实现顺序表内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(76)

评论列表共有 0 条评论

立即
投稿
返回
顶部