1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "./03_shunxu.h"
4
5
6 /*
7 * function: 创建顺序表
8 * @param [ in]
9 * @param [out]
10 * @return
11 */
12 Shunxu * create_shunxu()
13 {
14 Shunxu *sq = (Shunxu *)malloc(sizeof(Shunxu));
15 if( sq == NULL)
16 {
17 printf("顺序表创建成功\n");
18 return NULL;
19 }
20 sq->pos = 0;
21 return sq;
22 }
23
24 /*
25 * function: 插入数据
26 * @param [ in]
27 * @param [out]
28 * @return 0:插入失败
29 */
30 int insert_shunxu(Shunxu * sq, datatype num)
31 {
32 if(sq->pos == N)
33 {
34 printf("顺序表已满,无法插入数据\n");
35 return 0;
36 }
37 sq->data[sq->pos] = num;
38 sq->pos++;
39 return num;
40 }
41
42 /*
43 * function: 删除数据
44 * @param [ in]
45 * @param [out]
46 * @return
47 */
48 datatype delete_shunxu(Shunxu * sq)
49 {
50 //判空
51 if (sq->pos == 0)
52 {
53 printf("顺序表为空,无法进行数据删除\n");
54 return (datatype)-1;
55 }
56 sq->pos--;
57 datatype num = sq->data[sq->pos];
58 return num;
59 }
60
61 /*
62 * function: 遍历
63 * @param [ in]
64 * @param [out]
65 * @return
66 */
67 void show_shunxu(Shunxu * sq)
68 {
69 int i;
70 for(i =0;i<sq->pos;i++)
71 {
72 printf("%d ",sq->data[i]);
73 }
74 putchar(10);
75 return ;
76 }
77
78
79
80 /*
81 * function: 从指定位置插入数据
82 * @param [ in]
83 * @param [out]
84 * @return
85 */
86 void insert_index(Shunxu * sq,datatype num,int index)
87 {
88 //判满
89 if (sq->pos >= N)
90 {
91 printf("顺序表已满\n");
92 return;
93 }
94
95 //判断插入的位置是否合法
96 if(index > sq->pos || index < 0)
97 {
98 printf("插入位置非法\n");
99 return;
100 }
101
102 for (int i = sq->pos;i >= index +1;i--)
103 {
104 sq->data[i] = sq->data[i-1];
105 }
106 sq->data[index] = num;
107 sq->pos++;
108 return;
109 }
110
111 /*
112 * function: 按下标位置删除数据
113 * @param [ in]
114 * @param [out]
115 * @return
116 */
117
118 datatype delete_index(Shunxu * sq, int index)
119 {
120 //判空
121 if(sq->pos == 0)
122 {
123 printf("顺序表为空\n");
124 return (datatype)-1;
125 }
126
127 //判断删除的位置是否合法
128 if(index > sq->pos || index < 0)
129 {
130 printf("插入位置非法\n");
131 return (datatype)-1;
132 }
133
134 datatype num = sq->data[index];//将要删除的位置的数据作为返回值返回出去,
135 //所以在删除之前先备份
136
137 //将要删除的位置后面的数据依次往前覆盖
138 for(int i = index; i <= sq->pos - 2; i++)
139 {
140 sq->data[i] = sq->data[i+1];
141 }
142 sq->pos--;
143 return num;
144 }
145 void free_shunxu(Shunxu ** sq1)//Shunxu ** sq1 = &sq
146 {
147 free(*sq1);
148 * sq1= NULL;
149 }
1 #include <stdio.h>
2 #include "./03_shunxu.h"
3 int main(int argc, const char *argv[])
4 {
5 Shunxu * sq = create_shunxu();
6
7 putchar(10);
8 printf("-----插入数据-----\n");
9 insert_shunxu(sq,1);
10 insert_shunxu(sq, 12);
11 insert_shunxu(sq, 21);
12 show_shunxu(sq);
13
14 putchar(10);
15 printf("-----按下标位置插入数据-----\n");
16 insert_index(sq,20,0);
17 show_shunxu(sq);
18 insert_index(sq,9,4);
19 show_shunxu(sq);
20
21 putchar(10);
22 printf("-----按指定位置删除数据-----\n");
23 datatype y,y1;
24 y = delete_index(sq,2);
25 printf("y=%d\n",y);
26 show_shunxu(sq);
27 y1 = delete_index(sq,0);
28 printf("y1=%d\n",y1);
29 show_shunxu(sq);
30
31
32 putchar(10);
33 printf("-----删除数据-----\n");
34 datatype x,x1;
35 x = delete_shunxu(sq);
36 printf("x=%d\n",x);
37 x1 = delete_shunxu(sq);
38 printf("x1=%d\n",x1);
39 show_shunxu(sq);
40
41 putchar(10);
42 printf("-----删除顺序表-----\n");
43 free_shunxu(&sq);
44 printf("sq=%p\n",sq);
45
46
47 return 0;
48 }
~
~
~
1 #ifndef __SHUNXU__H__
2 #define __SHUNXU__H__
3 #define N 6
4
5 typedef int datatype;//将int类型重命名为datatype类型
6 typedef struct
7 {
8 datatype data[N];//存储顺序表
9 int pos;//存储顺序表的长度
10 }Shunxu;
11
12 Shunxu * create_shunxu();
13 int insert_shunxu(Shunxu * sq, datatype num);
14 datatype delete_shunxu(Shunxu * sq);
15 void show_shunxu(Shunxu * sq);
16 void insert_index(Shunxu * sq,datatype num,int index);
17 datatype delete_index(Shunxu * sq, int index);
18 void free_shunxu(Shunxu ** sq1);
19
20
21
22 #endif
23
24
25
~
~
~
~
运行结果