1 #include<stdio.h>
2 #include<malloc.h>
3 #include<stdbool.h>
4 #define SEQLIST_INIT_SIZE 8
5 typedef int ElemType;
6 //结构体定义
7 typedef struct SeqList{
8 ElemType* base;
9 int size;
10 int capacity;
11 }SeqList;
12
13 //初始化结构体
14 void initSeqList(SeqList* seqList){
15 seqList->base = (ElemType*)malloc(sizeof(SeqList) * SEQLIST_INIT_SIZE); //申请内存空间
16 seqList->size = 0;//初始顺序表当前大小为0
17 seqList->capacity = SEQLIST_INIT_SIZE;//顺序表容量为 SEQLIST_INIT_SIZE
18 }
19 ////尾插法
20 //void push_back(SeqList* seqList,ElemType e){
21 // //判断容量是否已满
22 // if(seqList->size >= seqList->capacity){
23 // printf("当前顺序表容量已满,不能向尾部插入数据>%d. \n",e);
24 // return;
25 // }
26 // //插入数据到尾部
27 // seqList->base[seqList->size] = e;
28 // //数组当前容量大小 + 1
29 // seqList->size++;
30 //}
31
32 //头插法
33 void push_front(SeqList* seqList,ElemType e){
34 //判断容量是否已满
35 if(seqList->size >= seqList->capacity){
36 printf("当前顺序表容量已满,不能向头部插入数据>%d. \n",e);
37 return;
38 }
39 int i = 0;
40 //将数组当前所有元素后移
41 for(i = seqList->size; i > 0; i--){
42 seqList->base[i] = seqList->base[i-1];
43 }
44 //插入数据到头部
45 seqList->base[0] = e;
46 //数组当前容量大小 + 1
47 seqList->size++;
48 }
49
50 //显示数据中的所有数据
51 void show_list(SeqList seqList){
52 int i = 0;
53 for(i = 0;i < seqList.size;i++){
54 printf("%d ",seqList.base[i]);
55 }
56 printf("\n");
57 }
58
59 //删除尾部
60 void pop_back(SeqList* seqList){
61 //判断当前数组是否为空
62 if(seqList->size == 0){
63 printf("当前数组为空,不能进行删除尾部.\n");
64 return;
65 }
66 //只需更改size大小即可
67 seqList->size--;
68 }
69
70 //删除头部
71 void pop_front(SeqList* seqList){
72 //判断当前数组是否为空
73 if(seqList->size == 0){
74 printf("当前数组为空,不能进行删除头部.\n");
75 return;
76 }
77 int i = 1;
78 //数组数据前移
79 for(i = 1;i < seqList->size;i++){
80 seqList->base[i-1] = seqList->base[i];
81 }
82 //数组当前大小-1
83 seqList->size--;
84 }
85
86 //向顺序表的 position 位置插入 e 元素
87 //注:这里位置为数据下标
88 void insert_pos(SeqList* seqList,int position,ElemType e){
89 //判断 插入的位置[position] 是否合法
90 if(position < 0 || position > seqList->size){
91 printf("插入的位置[position]不合法,不能插入数据.\n");
92 return;
93 }
94 //判断当前数组是否已满
95 if(seqList->size >= seqList->capacity){
96 printf("当前数组已满,不能插入数据.\n");
97 return;
98 }
99
100 // if(position == 0){
101 // //如果插入的位置为第一个元素,则调用头插法函数
102 // push_front(seqList,e);
103 // }else if(position == seqList->size){
104 // //如果插入的位置为最后一个元素,则调用头插法函数
105 // push_back(seqList,e);
106 // }else{
107 // //如果既不是头部也不是尾部,
108 // //在中间插入元素
109 // int i = seqList->size;
110 // for(i;i > position;i--){
111 // seqList->base[i] = seqList->base[i - 1];
112 // }
113 // seqList->base[position] = e;
114 // seqList->size++;
115 // }
116 int i = seqList->size;
117 for(i;i > position;i--){
118 seqList->base[i] = seqList->base[i - 1];
119 }
120 seqList->base[position] = e;
121 seqList->size++;
122 }
123
124 //查找顺序表元素
125 int find(SeqList seqList,ElemType e){
126 int i = 0;
127 for(i;i < seqList.size;i++){
128 if(seqList.base[i] == e){
129 return i;
130 }
131 }
132 return -1;
133 }
134
135 //顺序表长度
136 int length(SeqList seqList){
137 return seqList.size;
138 }
139
140 //删除顺序表的 pos 位置的元素
141 //注: pos代表元素下标位置
142 void delete_pos(SeqList* seqList,int pos){
143 if(pos < 0 || pos >= seqList->size){
144 printf("删除的数据位置不合法,不能删除数据.\n");
145 return;
146 }
147 int i = pos;
148 for(i;i< seqList->size - 1;i++){
149 seqList->base[i] = seqList->base[i+1];
150 }
151 seqList->size--;
152 }
153
154 //按值删除元素
155 void delete_val(SeqList* seqList,ElemType e){
156 int pos = find(*seqList,e);
157 if(pos == -1){
158 printf("按值删除失败,当前val: %d 不存在顺序表中.\n",e);
159 return;
160 }
161 delete_pos(seqList,pos);
162 }
163
164
165
166 //排序(从小到大)
167 void sort(SeqList* seqList){
168 int i = 0;
169 int size = seqList->size;
170 for(i;i < size - 1;i++){
171 int flag = 0;//判断这次循环是否有把元素前移
172 int j = 0;
173 for(j;j < size - i;j++){
174 if(seqList->base[j] > seqList->base[j + 1]){
175 ElemType temp = seqList->base[j];
176 seqList->base[j] = seqList->base[j + 1];
177 seqList->base[j + 1] = temp;
178 flag = 1;//当前有变换
179 }
180 }
181 if(flag == 0){
182 //说明当前已经排序好了
183 return;
184 }
185
186 }
187 }
188
189 //逆置顺序表
190 void reverse(SeqList* seqList){
191 int low = 0;
192 int high = seqList->size - 1;
193 while(low < high){
194 ElemType temp = seqList->base[low];
195 seqList->base[low] = seqList->base[high];
196 seqList->base[high] = temp;
197 low++;
198 high--;
199 }
200 }
201
202 //清空顺序表
203 void clear(SeqList* seqList){
204 seqList->size = 0;
205 }
206
207 //销毁顺序表
208 void destory(SeqList* seqList){
209 free(seqList->base);//释放所指的空间
210 seqList->base = NULL;
211 seqList->size = 0;
212 seqList->capacity = 0;
213 }
214
215 #define INC_SIZE 8
216 //扩容
217 bool capacity_inc(SeqList* seqList){
218 ElemType* newBase = (ElemType*)realloc(seqList->base,sizeof(ElemType) * (seqList->capacity + INC_SIZE));
219 if(newBase == NULL){
220 printf("增配空间失败,内存不足.\n");
221 return false;
222 }
223 seqList->base = newBase;
224 seqList->capacity += INC_SIZE;
225 return true;
226 }
227
228 //尾插法[扩容改进]
229 void push_back(SeqList* seqList,ElemType e){
230 //判断容量是否已满 且 是否可以进行扩容
231 if(seqList->size >= seqList->capacity && !capacity_inc(seqList)){
232 printf("当前顺序表容量已满,不能向尾部插入数据>%d. \n",e);
233 return;
234 }
235 //插入数据到尾部
236 seqList->base[seqList->size] = e;
237 //数组当前容量大小 + 1
238 seqList->size++;
239 }
240
241 //数组合并[从小到大]
242 void merge(SeqList* list,SeqList* list_a,SeqList* list_b){
243 list->capacity = list_a->size + list_b->size;
244
245 list->base = (SeqList*) malloc(sizeof(ElemType) * list->capacity);
246 if(list->base == NULL){
247 printf("内存空间不足.\n");
248 return;
249 }
250 int index_a = 0;
251 int index_b = 0;
252 int index = 0;
253 while(index_a < list_a->size && index_b < list_b->size){
254 if(list_a->base[index_a] < list_b->base[index_b]){
255 list->base[index] = list_a->base[index_a];
256 index_a++;
257 index++;
258 //list->base[index++] = list_a->base[index_a++];
259 }else{
260 list->base[index] = list_b->base[index_b];
261 index_b++;
262 index++;
263 //list->base[index++] = list_b->base[index_b++];
264 }
265 }
266 while(index_a < list_a->size){
267 list->base[index++] = list_a->base[index_a++];
268 }
269 while(index_b < list_b->size){
270 list->base[index++] = list_b->base[index_b++];
271 }
272 list->size = list_a->size + list_b->size;
273 }
274 int main(){
275 SeqList myList;
276 initSeqList(&myList);
277 int select = 1;
278 while(select){
279 printf("***********************************\n");
280 printf("* [1] push_back [2] push_front *\n");
281 printf("* [3] show_list [4] pop_back *\n");
282 printf("* [5] pop_front [6] insert_pos *\n");
283 printf("* [7] find [8] length *\n");
284 printf("* [9] delete_pos [10] delete_val *\n");
285 printf("* [11] sort [12] reverse *\n");
286 printf("* [13] clear [14] destory *\n");
287 printf("* [0] quit_system *\n");
288 printf("***********************************\n");
289 printf("请选择:>");
290 scanf("%d",&select);
291 if(select == 0){
292 break;
293 }
294 int item = 0;
295 int pos = 0;
296 switch(select){
297 case 1:
298 printf("请输入要输入的数据(-1结束):>");
299 while(scanf("%d",&item),item != -1){
300 push_back(&myList,item);
301 }
302 break;
303 case 2:
304 printf("请输入要输入的数据(-1结束):>");
305 while(scanf("%d",&item),item != -1){
306 push_front(&myList,item);
307 }
308 break;
309 case 3:
310 show_list(myList);
311 break;
312 case 4:
313 pop_back(&myList);
314 break;
315 case 5:
316 pop_front(&myList);
317 break;
318 case 6:
319 printf("请输入要插入的位置:>",pos);
320 scanf("%d",&pos);
321 printf("请输入要输入的数据:>",item);
322 scanf("%d",&item);
323 insert_pos(&myList,pos,item);
324 break;
325 case 7:
326 printf("请输入要查找的数据:>");
327 scanf("%d",&item);
328 pos = find(myList,item);
329 if(pos == -1){
330 printf("查找到数据 %d 在顺序表中不存在.\n",item);
331 }else{
332 printf("找到数据 %d 在顺序表中的 %d 的位置.n",item,pos);
333 }
334 break;
335 case 8:
336 printf("该顺序表长度为 %d.\n",length(myList));
337 break;
338 case 9:
339 printf("请输入要删除的位置:>");
340 scanf("%d",&pos);
341 delete_pos(&myList,pos);
342 break;
343 case 10:
344 printf("请输入要删除的元素:>");
345 scanf("%d",&item);
346 delete_val(&myList,item);
347 break;
348 case 11:
349 sort(&myList);
350 return;
351 case 12:
352 reverse(&myList);
353 return;
354 case 13:
355 clear(&myList);
356 return;
357 case 14:
358 destory(&myList);
359 return;
360 }
361 }
362 return 0;
363 }