上海网站开发建,seo费用价格,企业网站内容如何搭建,百度知道答题赚钱搭建个场景: 将学生的信息#xff0c;以顺序表的方式存储#xff08;堆区)#xff0c;并且实现封装函数︰1】顺序表的创建#xff0c; 2】判满、 3】判空、 4】往顺序表里增加学生、5】遍历、 6】任意位置插入学生、7】任意位置删除学生、8】修改、 9】查找(按学生的学号查…搭建个场景: 将学生的信息以顺序表的方式存储堆区)并且实现封装函数︰1】顺序表的创建 2】判满、 3】判空、 4】往顺序表里增加学生、5】遍历、 6】任意位置插入学生、7】任意位置删除学生、8】修改、 9】查找(按学生的学号查找)、10】去重、 11】销毁顺序表
main.c
#include head.h
int main(int argc,const char *argv[])
{//创建学生的顺序表seqlist_ptr pcreate_list();//判断顺序表是否为满int p1full_doubt(p);//判断顺序表是否为空int p2empty_doubt(p);//顺序表中添加学生数据add(p,6666);add(p,1001);add(p,1002);add(p,1003);add(p,1004);//顺序表中输出学生数据 output(p);//在任意位置插入学生insert(p,3,1111); output(p);//删除任意位置的学生del(p,3);output(p);//更改学生IDchange_index(p,3,6666);output(p);//查找学生IDfind(p,6666);//去重del_same(p);output(p);//释放my_free(p);return 0;
}fun.c 1 #include head.h 2 3 4 //1.创建学生的顺序表 5 seqlist_ptr create_list() 6 { 7 //申请堆区的空间 8 seqlist_ptr p(seqlist_ptr)malloc(sizeof(seqlist)); 9 if(NULLp) 10 { 11 printf(顺序表创建失败\n); 12 return NULL; 13 } 14 15 p-len0;//将顺序表中的长度清零 16 //将数组的长度清零 17 memset(p-ID,0,sizeof(p-ID)); 18 printf(创建顺序表成功\n); 19 return p; 20 } 21 22 23 //2.判断顺序表是否为满 24 int full_doubt(seqlist_ptr p) 25 { 26 if(NULLp) 27 { 28 printf(顺序表不合法,无法判断); 29 return -1; 30 } 31 else if(p-lenMAX) 32 { 33 printf(顺序表满\n); 34 return 1; 35 } 36 37 return 0; 38 } 39 40 41 //3.判断顺序表是否为空 42 int empty_doubt(seqlist_ptr p) 43 { 44 if(NULLp) 45 { 46 printf(顺序表不合法,无法判断); 47 } 48 else if(p-len0) 49 { 50 printf(顺序表为空\n); 51 return 1; 52 } 53 return 0; 54 } 55 56 57 //4.顺序表数据的增加(添加学生的id号) 58 int add(seqlist_ptr p,datatype a) 59 { 60 if(NULLp || full_doubt(p)) 61 { 62 printf(无法增加\n); 63 return 0; 64 } 65 p-ID[p-len]a; 66 p-len; 67 return 1; 68 } 69 70 71 //5.顺序表中输出学生数据 72 int output(seqlist_ptr p) 73 { 74 if(NULLp || empty_doubt(p)) 75 { 76 printf(无法输出i\n); 77 return 0; 78 } 79 for(int i0;ip-len;i) 80 { 81 printf(%d ,p-ID[i]); 82 } 83 printf(\n); 84 return 1; 85 } 86 87 88 //6.在任意位置插入学生数据 89 int insert(seqlist_ptr p,int index,datatype e) 90 { 91 if(NULLp || indexMAX || index0 || empty_doubt(p)) 92 { 93 printf(插入失败\n); 94 return -1; 95 } 96 //此时的index表示数组的下标 97 index-1; 98 for(int i0;ip-len-index;i) 99 { //p-len表示未赋值的那个元素
100 p-ID[p-len-i]p-ID[p-len-1-i];
101 }
102 //赋值
103 p-ID[index]e;
104 //长度1
105 p-len;
106 return 1;
107 }
108
109
110 //7.删除任意位置的学生
111 int del(seqlist_ptr p,int index)
112 {
113 if(NULLp || indexMAX || index0 || empty_doubt(p))
114 {
115 printf(删除失败\n);
116 return -1;
117 }
118 //此时index表示数组的下标
119 index-1;
120 for(int iindex;ip-len;i)
121 {
122 p-ID[index]p-ID[index1];
123 }
124 p-len--;
125 return 1;
126 }
127
128
129 //8.任意位置更改学生ID
130 int change_index(seqlist_ptr p,int index,datatype e)
131 {
132 if(NULLp || indexMAX || index 0 || empty_doubt(p))
133 {
134 printf(更改失败\n);
135 return -1;
136 }
137 index-1;
138 p-ID[index]e;
139 return 1;
140 }
141
142
143 //9.查找学生ID
144 int find(seqlist_ptr p,datatype e)
145 {
146 if(NULLp || empty_doubt(p))
147 {
148 printf(查找失败\n);
149 return -1;
150 }
151 int flag0;
152 for(int i0;ip-len;i)
153 {
154 if(p-ID[i]e)
155 {
156 flag1;
157 printf(查找的学生是第%d位学生\n,i1);
158 return i;
159 }
160
161 if(flag0)
162 {
163 printf(未查找到学生ID\n);
164 return 0;
165 }
166 }
167 }
168
169
170 //10.去重
171 int del_same(seqlist_ptr p)
172 {
173 if(NULLp || empty_doubt(p))
174 {
175 printf(去重失败\n);
176 return -1;
177 }
178 for(int i0;ip-len;i)
179 {
180 for(int ji1;jp-len;j)
181 {
182 if(p-ID[i]p-ID[j])
183 {
184 del(p,j1);
185 j--;
186 }
187 }
188 }
189 return 1;
190 }
191
192
193 //11 释放
194 int my_free(seqlist_ptr p)
195 {
196 if(NULLp)
197 {
198 printf(释放失败\n);
199 return -1;
200 }
201 free(p);
202 printf(释放成功\n);
203 return 1;
204
205 }
~ head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#include stdio.h
#include stdlib.h
#include string.h
//顺序标容器存储学生个数的最大值
#define MAX 30
//宏替换ID的数据类型
typedef int datatype;
//创建顺序表用于存储学生的信息
typedef struct sequence
{ datatype ID[MAX]; //存储学生的个数 int len;
}seqlist,*seqlist_ptr; //1.创建学生的顺序表
seqlist_ptr create_list();
//2.判断顺序表是否为满
int full_doubt(seqlist_ptr p);
//3.判断顺序表是否为空
int empty_doubt(seqlist_ptr p);
//4.顺序表数据的增加(添加学生的id号)
int add(seqlist_ptr p,datatype a);
//5.顺序表中输出学生数据
int output(seqlist_ptr p);
//6.在任意位置插入学生数据
int insert(seqlist_ptr p,int index,datatype e);
//7.删除任意位置的学生
int del(seqlist_ptr p,int index);
//8.任意位置更改学生ID
int change_index(seqlist_ptr p,int index,datatype e);
//9.查找学生ID
int find(seqlist_ptr p,datatype e);
//10.去重
int del_same(seqlist_ptr p);
//11 释放
int my_free(seqlist_ptr p);
#endif