創(chuàng)建一個結(jié)構(gòu)體用于存放順序表相關(guān)數(shù)據(jù)
1
2
3
4
5
6
7
|
#define SEQTYPE int typedef struct SeqList { SEQTYPE* data; int size; //有效數(shù)據(jù)個數(shù) int capacity; //容量 }SeqList; |
初始化順序表
1
2
3
4
5
6
7
8
|
void SeqListInit(SeqList* pq) { CheckNull(pq); pq->data = NULL; pq->capacity = 0; pq->size = 0; } |
插入元素
- 插入到表頭;
- 插入到指定位置;
- 插入到尾部;
先檢查容量是否夠用
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
|
void CheckCapacity(SeqList* pq) { CheckNull(pq); //如果空間滿了,擴(kuò)容 if (pq->size >= pq->capacity) { int newcapacity = pq->capacity == 0 ? 4 : pq->capacity * 2; SEQTYPE* new = (SEQTYPE*) realloc (pq->data, sizeof (SEQTYPE) * newcapacity); if ( new == NULL) { perror ( "realloc" ); exit (-1); } pq->data = new ; pq->capacity = newcapacity; } puts ( "增容成功" ); } //往順序表指定位置插入數(shù)據(jù) void SeqListInsert(SeqList* pq, int pos) { CheckNull(pq); assert (pos <= pq->size); SEQTYPE InsertVal; if (pos == -1) { printf ( "請分別輸入添加的數(shù)據(jù)和位置,空格隔開:>" ); scanf ( "%d %d" , &InsertVal, &pos); if (pos > pq->size) { printf ( "請正確輸入\n" ); return ; } } else { printf ( "請輸入添加的數(shù)據(jù):>" ); scanf ( "%d" , &InsertVal); } //檢查容量是否足夠 CheckCapacity(pq); //插入數(shù)據(jù) int end = pq->size; int begin = pos; while (begin < end) { pq->data[end] = pq->data[end - 1]; --end; } pq->data[pos] = InsertVal; ++pq->size; printf ( "添加成功\n" ); } //往順序表末位置插入數(shù)據(jù) void SeqListPushBack(SeqList* pq) { CheckNull(pq); SeqListInsert(pq, pq->size); } //往順序表首位置插入數(shù)據(jù) void SeqListPushFront(SeqList* pq) { CheckNull(pq); SeqListInsert(pq, 0); } |
刪除元素
- 刪除首元素;
- 刪除指定位置元素;
- 刪除尾部元素;
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
|
//從順序表指定位置刪除數(shù)據(jù) void SeqListErase(SeqList* pq, int pos) { CheckNull(pq); if (pos == -1) { printf ( "請輸入要刪除數(shù)據(jù)的位置:>" ); scanf ( "%d" , &pos); if (pos < 0 || pos >= pq->size) { printf ( "請正確輸入\n" ); return ; } } int begin = pos; int end = pq->size - 1; while (begin < end) { pq->data[begin] = pq->data[begin + 1]; ++begin; } --pq->size; puts ( "刪除成功" ); } //從順序表末位置刪除數(shù)據(jù) void SeqListPophBack(SeqList* pq) { CheckNull(pq); SeqListErase(pq, pq->size - 1); } //從順序表首位置刪除數(shù)據(jù) void SeqListPophFront(SeqList* pq) { CheckNull(pq); SeqListErase(pq, 0); } |
元素修改
- 找到目標(biāo)元素;
- 直接修改該元素的值;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
//修改順序表指定位置數(shù)據(jù) void SeqListModify(SeqList* pq) { CheckNull(pq); int pos; SEQTYPE x; printf ( "請輸入修改的位置和新的數(shù)據(jù),空格隔開:>" ); scanf ( "%d %d" , &pos, &x); if (pos < 0 && pos >= pq->size) { printf ( "請正確輸入\n" ); return ; } pq->data[pos] = x; puts ( "修改成功" ); } |
查找元素
查找目標(biāo)元素,算法多種,比如二分,插值等等,這里使用順序查找算法,具體代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
//查找所需數(shù)據(jù)是否存在順序表中 void SeqListFindData(SeqList* pq) { CheckNull(pq); SEQTYPE x; printf ( "請輸入要查找的數(shù)據(jù):>" ); scanf ( "%d" , &x); for ( int i = 0; i < pq->size; i++) { if (pq->data[i] == x) { printf ( "所需查詢數(shù)據(jù)存在,下標(biāo)為:>%d\n" , i); return ; } } printf ( "找不到\n" ); } |
排序元素
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
|
//排序順序表 void SeqListSort(SeqList* pq) { CheckNull(pq); int option = 0; printf ( "輸入0為升序,1為降序:>" ); scanf ( "%d" , &option); for ( int i = 0; i < pq->size - 1; i++) { for ( int j = 0; j < pq->size - i - 1; j++) { if (pq->data[j] > pq->data[j + 1]) { SEQTYPE tmp = pq->data[j]; pq->data[j] = pq->data[j + 1]; pq->data[j + 1] = tmp; } } } if (option) { SeqListReverse(pq); return ; } } |
元素反轉(zhuǎn)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
//順序表反轉(zhuǎn) void SeqListReverse(SeqList* pq) { CheckNull(pq); int left = 0; int right = pq->size - 1; while (left < right) { SEQTYPE tmp = pq->data[left]; pq->data[left] = pq->data[right]; pq->data[right] = tmp; ++left; --right; } } |
源碼
- 以上是順序表常用的功能操作,下面附上完整代碼,VS2019環(huán)境
SeqList.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
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
|
#include "SeqList.h" void CheckNull(SeqList* pq) { if (pq == NULL) { perror ( "pq::" ); exit (-1); } } //初始化順序表 void SeqListInit(SeqList* pq) { CheckNull(pq); pq->data = NULL; pq->capacity = 0; pq->size = 0; } void SeqListDestory(SeqList* pq) { CheckNull(pq); free (pq->data); pq->data = NULL; pq->size = 0; pq->capacity = 0; } void CheckCapacity(SeqList* pq) { CheckNull(pq); //如果空間滿了,擴(kuò)容 if (pq->size >= pq->capacity) { int newcapacity = pq->capacity == 0 ? 4 : pq->capacity * 2; SEQTYPE* new = (SEQTYPE*) realloc (pq->data, sizeof (SEQTYPE) * newcapacity); if ( new == NULL) { perror ( "realloc" ); exit (-1); } pq->data = new ; pq->capacity = newcapacity; } puts ( "增容成功" ); } void SeqListPrint(SeqList* pq) { CheckNull(pq); if (pq->size == 0) printf ( "\n" ); else { for ( int i = 0; i < pq->size; i++) { printf ( "%d " , pq->data[i]); } puts ( "\n--------------------------------------" ); } } //往順序表末位置插入數(shù)據(jù) void SeqListPushBack(SeqList* pq) { CheckNull(pq); SeqListInsert(pq, pq->size); } //往順序表首位置插入數(shù)據(jù) void SeqListPushFront(SeqList* pq) { CheckNull(pq); SeqListInsert(pq, 0); } //往順序表指定位置插入數(shù)據(jù) void SeqListInsert(SeqList* pq, int pos) { CheckNull(pq); assert (pos <= pq->size); SEQTYPE InsertVal; if (pos == -1) { printf ( "請分別輸入添加的數(shù)據(jù)和位置,空格隔開:>" ); scanf ( "%d %d" , &InsertVal, &pos); if (pos > pq->size) { printf ( "請正確輸入\n" ); return ; } } else { printf ( "請輸入添加的數(shù)據(jù):>" ); scanf ( "%d" , &InsertVal); } //檢查容量是否足夠 CheckCapacity(pq); //插入數(shù)據(jù) int end = pq->size; int begin = pos; while (begin < end) { pq->data[end] = pq->data[end - 1]; --end; } pq->data[pos] = InsertVal; ++pq->size; printf ( "添加成功\n" ); } //從順序表指定位置刪除數(shù)據(jù) void SeqListErase(SeqList* pq, int pos) { CheckNull(pq); if (pos == -1) { printf ( "請輸入要刪除數(shù)據(jù)的位置:>" ); scanf ( "%d" , &pos); if (pos < 0 || pos >= pq->size) { printf ( "請正確輸入\n" ); return ; } } int begin = pos; int end = pq->size - 1; while (begin < end) { pq->data[begin] = pq->data[begin + 1]; ++begin; } --pq->size; puts ( "刪除成功" ); } //從順序表末位置刪除數(shù)據(jù) void SeqListPophBack(SeqList* pq) { CheckNull(pq); SeqListErase(pq, pq->size - 1); } //從順序表首位置刪除數(shù)據(jù) void SeqListPophFront(SeqList* pq) { CheckNull(pq); SeqListErase(pq, 0); } //修改順序表指定位置數(shù)據(jù) void SeqListModify(SeqList* pq) { CheckNull(pq); int pos; SEQTYPE x; printf ( "請輸入修改的位置和新的數(shù)據(jù),空格隔開:>" ); scanf ( "%d %d" , &pos, &x); if (pos < 0 && pos >= pq->size) { printf ( "請正確輸入\n" ); return ; } pq->data[pos] = x; puts ( "修改成功" ); } //查找順序表指定位置數(shù)據(jù) void SeqListFindPos(SeqList* pq) { CheckNull(pq); int pos; printf ( "請輸入要查找數(shù)據(jù)的位置:>" ); scanf ( "%d" , &pos); if (pos < 0 && pos >= pq->size) { printf ( "請正確輸入\n" ); return ; } for ( int i = 0; i < pq->size; i++) { if (pq->data[i] == pq->data[pos]) { printf ( "查找位置的數(shù)據(jù)為:>%d\n" , pq->data[pos]); break ; } } } //查找所需數(shù)據(jù)是否存在順序表中 void SeqListFindData(SeqList* pq) { CheckNull(pq); SEQTYPE x; printf ( "請輸入要查找的數(shù)據(jù):>" ); scanf ( "%d" , &x); for ( int i = 0; i < pq->size; i++) { if (pq->data[i] == x) { printf ( "所需查詢數(shù)據(jù)存在,下標(biāo)為:>%d\n" , i); return ; } } printf ( "找不到\n" ); } //排序順序表 void SeqListSort(SeqList* pq) { CheckNull(pq); int option = 0; printf ( "輸入0為升序,1為降序:>" ); scanf ( "%d" , &option); for ( int i = 0; i < pq->size - 1; i++) { for ( int j = 0; j < pq->size - i - 1; j++) { if (pq->data[j] > pq->data[j + 1]) { SEQTYPE tmp = pq->data[j]; pq->data[j] = pq->data[j + 1]; pq->data[j + 1] = tmp; } } } if (option) { SeqListReverse(pq); return ; } } //順序表反轉(zhuǎn) void SeqListReverse(SeqList* pq) { CheckNull(pq); int left = 0; int right = pq->size - 1; while (left < right) { SEQTYPE tmp = pq->data[left]; pq->data[left] = pq->data[right]; pq->data[right] = tmp; ++left; --right; } } |
test.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
|
#include "SeqList.h" void menu() { printf ( "######################################################\n" ); printf ( "##### 1. Print 2. Insert #####\n" ); printf ( "##### 3. PushFront 4. PushBack #####\n" ); printf ( "##### 5. PopFront 6. PopBack #####\n" ); printf ( "##### 7. FindPos 8. FindData #####\n" ); printf ( "##### 9. Modify 10. Erase #####\n" ); printf ( "##### 11. EMPTY 12. Sort #####\n" ); printf ( "##### 13. Reverse 0. Exit #####\n" ); printf ( "######################################################\n" ); } enum Option { EXIT, PRINT, INSERT, PUSHFRONT, PUSHBACK, POPFRONT, POPBACK, FINDPOS, FINDDATA, MODIFY, ERASE, EMPTY, SORT, REVERSE, }; int main() { int option = 0; int posi = -1; SeqList s; SeqListInit(&s); do { menu(); printf ( "請選擇:>" ); scanf ( "%d" , &option); switch (option) { case PRINT: SeqListPrint(&s); break ; case INSERT: SeqListInsert(&s, posi); break ; case PUSHFRONT: SeqListPushFront(&s); break ; case PUSHBACK: SeqListPushBack(&s); break ; case POPFRONT: SeqListPophFront(&s); break ; case POPBACK: SeqListPophBack(&s); break ; case FINDPOS: SeqListFindPos(&s); break ; case FINDDATA: SeqListFindData(&s); break ; case MODIFY: SeqListModify(&s); break ; case ERASE: SeqListErase(&s, posi); break ; case EMPTY: SeqListDestory(&s); break ; case SORT: SeqListSort(&s); break ; case REVERSE: SeqListReverse(&s); break ; case EXIT: SeqListDestory(&s); printf ( "退出\n" ); break ; default : printf ( "請正確輸入\n" ); break ; } } while (option); return 0; } |
SeqList.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
|
#pragma once #define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> #include <stdlib.h> #include <assert.h> #include <errno.h> #include <string.h> // #define SEQTYPE int typedef struct SeqList { SEQTYPE* data; int size; //有效數(shù)據(jù)個數(shù) int capacity; //容量 }SeqList; //初始化順序表 void SeqListInit(SeqList* pq); //銷毀順序表 void SeqListDestory(SeqList* pq); //打印順序表 void SeqListPrint(SeqList* pq); //往順序表指定位置插入數(shù)據(jù) void SeqListInsert(SeqList* pq, int pos); //往順序表末位置插入數(shù)據(jù) void SeqListPushBack(SeqList* pq); //往順序表首位置插入數(shù)據(jù) void SeqListPushFront(SeqList* pq); //從順序表指定位置刪除數(shù)據(jù) void SeqListErase(SeqList* pq, int pos); //從順序表末位置刪除數(shù)據(jù) void SeqListPophBack(SeqList* pq); //從順序表首位置刪除數(shù)據(jù) void SeqListPophFront(SeqList* pq); //修改順序表指定位置數(shù)據(jù) void SeqListModify(SeqList* pq); //查找順序表指定位置數(shù)據(jù) void SeqListFindPos(SeqList* pq); //查找所需數(shù)據(jù)是否存在順序表中 void SeqListFindData(SeqList* pq); //排序順序表 void SeqListSort(SeqList* pq); //順序表反轉(zhuǎn) void SeqListReverse(SeqList* pq); |
總結(jié)
到此這篇關(guān)于C語言實(shí)現(xiàn)順序表的基本操作指南的文章就介紹到這了,更多相關(guān)C語言實(shí)現(xiàn)順序表基本操作內(nèi)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/weixin_45266788/article/details/120694962