思维导图
![](https://i-blog.csdnimg.cn/blog_migrate/6cbb952cdda043908cf5101cf121dc85.png)
练习
头文件
1 #ifndef __HEAD_H__
2 #define __HEAD_H__
3
4
5 #include <stdio.h>
6 #include <string.h>
7 #include <stdlib.h>
8
9
10 #define MAXSIZE 7
11 typedef int datatype;
12 enum
13 {
14 FLASE=-1,
15 SUCCESS
16 };
17 //定义顺序表:数据元素和顺序表长度
18 typedef struct SQLIST
19 {
20 //数据元素
21 datatype data[MAXSIZE];
22 //顺序表长度
23 int len;
24 }sqlist;
25
26 sqlist* create();
27 int full(sqlist *list);
28 int insert_rear(datatype element,sqlist *list);
29 int output(sqlist *list);
30 int delete_rear(sqlist *list);
31
32 void search_index(sqlist*list,int index);
33 void change_search_index(sqlist*list,int index,int element);
34 void insert_index(sqlist*list,int index,int element);
35 void delete_index(sqlist*list,int index);
36 int search_key(sqlist*list,int element);
37 void delete_same(sqlist*list);
38 void change_key(sqlist*list,int key,int element);
39 void delete_key(sqlist*list,int key);
40 void bubble(sqlist*list);
41 sqlist* free_space(sqlist*list);
42 #endif
自定义函数
1 #include "head.h"
2
3
4
5 /*
6 * function: 创建顺序表在堆区
7 * @param [ in]
8 * @param [out]
9 * @return 成功返回地址,失败返回NULL
10 */
11
12
13 sqlist* create()
14 {
15 sqlist *list=(sqlist*)malloc(sizeof(sqlist));
16 if(NULL==list)
17 return NULL;
18 //成功返回堆区
19 //对数据元素清零
20 memset(list->data,0,sizeof(list->data));
21 //顺序长度清零
22 list->len=0;
23 return list;
24 }
25
26
27
28 /*
29 * function: 判断是否为满
30 * @param [ in]
31 * @param [out]
32 * @return
33 */
34 int full(sqlist *list)
35 {
36 return list->len==MAXSIZE?FLASE:SUCCESS;
37 }
38
39
40 /*
41 * function: 尾插入
42 * @param [ in]
43 * @param [out]
44 * @return
45 */
46
47 int insert_rear(datatype element,sqlist *list)
48 {
49 if(NULL==list ||full(list))
50 return FLASE;
51 list->data[list->len++]=element;
52 return SUCCESS;
53 }
54 /*
55 * function: 判空
56 * @param [ in]
57 * @param [out]
58 * @return
59 */
60 int empty(sqlist *list)
61 {
62 return list->len==0?FLASE:SUCCESS;
63 }
64
65
66
67
68 /*
69 * function: 遍历
70 * @param [ in]
71 * @param [out]
72 * @return 成功返回0,失败返回-1
73 */
74
75
76 int output(sqlist *list)
77 {
78 //判断顺序表是否创建成功
79 //判断顺序表是否为空
80 if(NULL==list || empty(list))
81 return FLASE;
82 //循环遍历
83 for(int i=0;i<list->len;i++)
84 {
85 printf("%-5d",list->data[i]);
86
87 }
88 puts("");
89
90 }
91
92 /*
93 * function: 尾删
94 * @param [ in]
95 * @param [out]
96 * @return
97 */
98
99
100 int delete_rear(sqlist *list)
101 {
102 //1.判断顺序表是否创建成功
103 //2.判断顺序表是否为空
104 if(NULL==list || empty(list))
105 return FLASE;
106 //3.删除
107 list->len--;
108 return SUCCESS;
109 }
110
111
112 /*
113 * function: 按下表查找
114 * @param [ in]
115 * @param [out]
116 * @return
117 */
118 void search_index(sqlist*list,int index)
119 {
120 if(NULL==list||empty(list)||index<0||index>=list->len)
121 return;
122 printf("element searched by search index is %d\n",list->data[index]);
123
124 }
125
126 /*
127 * function: 按下表修改
128 * @param [ in]
129 * @param [out]
130 * @return
131 */
132 void change_search_index(sqlist*list,int index,int element)
133 {
134 if(NULL==list||empty(list)||index<0||index>=list->len)
135 return;
136 list->data[index]=element;
137 }
138 /*
139 * function: 按下表插入
140 * @param [ in]
141 * @param [out]
142 * @return
143 */
144 void insert_index(sqlist*list,int index,int element)
145 {
146 if(NULL==list||full(list)||index<0||index>list->len)
147 return;
148 for(int i=list->len-1;i>=index;i--)
149 {
150 list->data[i+1]=list->data[i];
151 }
152 list->data[index]=element;
153 list->len++;
154 }
155
156 /*
157 * function: 按下表删除
158 * @param [ in]
159 * @param [out]
160 * @return
161 */
162 void delete_index(sqlist*list,int index)
163 {
164 if(NULL==list||empty(list)||index<0||index>=list->len)
165 return;
166 for(int i=index+1;i<list->len;i++)
167 {
168 list->data[i-1]=list->data[i];
169 }
170 list->len--;
171 }
172
173 /*
174 * function: 按元素查找
175 * @param [ in]
176 * @param [out]
177 * @return
178 */
179 int search_key(sqlist*list,int element)
180 {
181 if(NULL==list||empty(list))
182 return FLASE;
183 for(int i=0;i<list->len;i++)
184 {
185 if(element==list->data[i])
186 return i;
187 }
188 return FLASE;
189 }
190
191
192 /*
193 * function: 按元素去重
194 * @param [ in]
195 * @param [out]
196 * @return
197 */
198 void delete_same(sqlist*list)
199 {
200 for(int i=0;i<list->len-1;i++)
201 {
202 for(int j=i+1;j<list->len;j++)
203 {
204 if(list->data[i]==list->data[j])
205 {
206 delete_index(list,j);
207 j--;
208 }
209 }
210 }
211 }
212
213 /*
214 * function: 按任意元素修改
215 * @param [ in]
216 * @param [out]
217 * @return
218 */
219 void change_key(sqlist*list,int key,int element)
220 {
221
222 int index=search_key(list,key);
223 change_search_index(list,index,element);
224 }
225
226
227
228 /*
229 * function: 按任意元素删除
230 * @param [ in]
231 * @param [out]
232 * @return
233 */
234 void delete_key(sqlist*list,int key)
235 {
236 int index=search_key(list,key);
237 delete_index(list,index);
238 }
239
240 /*
241 * function: 顺序表排序
242 * @param [ in]
243 * @param [out]
244 * @return
245 */
246 void bubble(sqlist*list)
247 {
248 for(int i=0;i<list->len-1;i++)
249 {
250 for(int j=0;j<list->len-i;j++)
251 {
252 if(list->data[j]<list->data[j+1])
253 {
254 int temp=list->data[j];
255 list->data[j]=list->data[j+1];
256 list->data[j+1]=temp;
257 }
258 }
259 }
260 }
261
262
263
264 /*
265 * function: 顺序表释放空间
266 * @param [ in]
267 * @param [out]
268 * @return
269 */
270 sqlist* free_space(sqlist*list)
271 {
272 if(NULL==list)
273 return NULL;
274 free(list);
275 list=NULL;
276 return NULL;
277 }
主函数
1 #include "head1.h"
2 int main(int argc, const char *argv[])
3 {
4 //创建顺序表在堆区
5 sqlist *list=create();
6
7
8 //循环尾插
9 int n;
10 datatype element;
11 printf("请输入长度");
12 scanf("%d",&n);
13 for(int i=0;i<n;i++)
14 {
15 printf("请输入尾插入的值");
16 scanf("%d",&element);
17 int flag=insert_rear(element,list);
18 if(flag==FLASE)
19 {
20 puts("NULL or FULL");
21 break;
22 }
23 }
24 //循环遍历
25 output(list);
26
27
28 //尾删
29 delete_rear(list);
30 printf("尾删后\n");
31 output(list);
32
33
34
35
36 //按下表查找
37 int index;
38
39
40 //输入要查找的下表
41 printf("please enter search index:");
42 scanf("%d",&index);
43 //输出查找下表的值
44 printf("下表查找的值\n");
45 search_index(list,index);
46
47
48
49
50 //任意下表修改
51 //输入修改下表
52 printf("please enter change index:");
53 scanf("%d",&index);
54 //输入要修改的值
55 printf("please enter change element:");
56 scanf("%d",&element);
57 change_search_index(list,index,element);
58 //输出修改后的结果
59 printf("按任意下表修改后:\n");
60 output(list);
61 puts("");
62
63 //按下表插入
64 //输入要插入的下表
65 printf("please enter insert index:");
66 scanf("%d",&index);
67 //输入要插入的值
68 printf("please enter insert element:");
69 scanf("%d",&element);
70 insert_index(list,index,element);
71 //插入后的结果
72 printf("按下表插入后\n");
73 output(list);
74 puts("");
75
76
77
78 //按下表删除
79 //输入要删除的下表
80 printf("please enter delete index:");
81 scanf("%d",&index);
82 delete_index(list,index);
83 //删除后的结果
84 printf("按下表删除后\n");
85 output(list);
86 puts("");
87
88
89 //按元素查找
90 printf("please enter search element:");
91 scanf("%d",&element);
92 int flag=search_key(list,element);
93 printf("查找的元素的下表为:");
94 if(flag==-1)
95 puts("error");
96 else
97 printf("%d\n",flag);
98 puts("");
99
100 //按元素去重
101 delete_same(list);
102 printf("按元素去重后\n");
103 output(list);
104 puts("");
105
106
107 int key;
108
109 //按任意元素修改
110 printf("please enter change key:");
111 scanf("%d",&key);
112 printf("please enter change element:");
113 scanf("%d",&element);
114 change_key(list,key,element);
115 printf("按任意下表修改后\n");
116 output(list);
117 puts("");
118
119
120 //按任意元素删除
121 printf("please enter delete key:");
122 scanf("%d",&key);
123 delete_key(list,key);
124 printf("按任意元素删除后\n");
125 output(list);
126 puts("");
127
128
129 //冒泡排序顺序表
130 bubble(list);
131 printf("排序后\n");
132 output(list);
133 puts("");
134
135 //顺序表释放空间
136 list=free_space(list);
137 return 0;
138 }
效果图
![](https://i-blog.csdnimg.cn/blog_migrate/8e311262bd0801a5db3e060a291ab996.png)
![](https://i-blog.csdnimg.cn/blog_migrate/e7416525749415850d6a8667726b1b38.png)
![](https://i-blog.csdnimg.cn/blog_migrate/84fe81c5c775b1df4ec5b3fb952510ae.png)