来自  资质荣誉 2019-12-25 06:22 的文章
当前位置: 澳门太阳娱乐手机登录 > 资质荣誉 > 正文

二维动态数组如何完美删除数组中的某个元素并

图片 1

#includestdio.h#includemalloc.h#includestring.h#define MAXSIZE 100typedef struct{int score;char sno[5];char sname[5];}Student;typedef struct{int last;Student data[MAXSIZE];}Seqlist;void displayAll(Seqlist *L){int i,n;i=0;n=L-last;printf("nttnotnamettscoren");while(i=n){printf("ntt%stt%stt%d",L-data[i].sno,L-data[i].sname,L-data[i].score);i=i+1;}}void display(Seqlist *L,int i){printf("nttnotnamettscoren");printf("nt%stt%stt%d",L-data[i].sno,L-data[i].sname,L-data[i].score);}Student *inputdata(){Student s1;Student *s=s1;char no[5];printf("n请输入房间号:"卡塔尔(英语:State of Qatar);scanf("%s",no卡塔尔;if(no[0]=='#')return NULL;strcpy(s-sno,no);printf("n请输入房间类型:"卡塔尔(英语:State of Qatar);scanf("%s",s-sname卡塔尔(قطر‎;printf("n请输入房间价格:"卡塔尔(قطر‎;scanf("%d",s-score卡塔尔(قطر‎;return s;}Seqlist *initseq(){Seqlist *L;L=(Seqlist *)malloc(sizeof(Seqlist));L-last=-1;Student *s;printf("n when the no is #,it finishes");s=inputdata();while(s){L-last++;L-data[L-last]=*s;s=inputdata();}return L;}int insertElem(Seqlist *L,int i){int j,k;Student *s;k=L-last;if(L-last==MAXSIZE-1){printf("n-----------overflow----------n");return 0;}if(i1||ik+1)for(j=k;j=i-1;j--){L-data[j+1]=L-data[j];}s=inputdata();L-last++;L-data[i-1]=*s;return 1;}int deleteElem(Seqlist *L,int i卡塔尔(英语:State of Qatar){int j,k;k=L-last;if(i1||ik+1卡塔尔国{printf("删除地方错误."卡塔尔国;return 0;}for(j=i;j=k;j++卡塔尔(英语:State of Qatar){L-data[j-1]=L-data[j];}L-last--;return 1;}void locateElem(Seqlist *L,char ch[5]){int i;for(i=0;i=L-last;i++)if(strcmp(L-data[i].sno,ch)==0){display(L,i);break;}}void locateElemByplace(Seqlist *L,int i){display(L,i-1);}int lengthList(Seqlist *L){return L-last+1;}void insertsort(Seqlist *L){Seqlist *L1=(Seqlist *)malloc(sizeof(Seqlist));Student temp;int len,i,j;len=L-last;for(i=0;i=len;i++){L1-data[i]=L-data[i];}L1-last=L-last;for(i=1;i=len;i++){if(L1-data[i].scoreL1-data[i-1].score){temp.score=L1-data[i].score;strcpy(temp.sno,L1-data[i].sno);strcpy(temp.sname,L1-data[i].sname);L1-data[i]=L1-data[i-1];for(j=i-2;(temp.scoreL1-data[j].score)(j=0);j--)L1-data[j+1]=L1-data[j];L1-data[j+1].score=temp.score;strcpy(L1-data[j+1].sno,temp.sno);strcpy(L1-data[j+1].sname,temp.sname);}}printf("n排序后为;"卡塔尔国;displayAll(L1卡塔尔(英语:State of Qatar);}int main(卡塔尔(قطر‎{printf("nn=====================================================================n"卡塔尔国;printf(" 饭店管理连串 n");printf("nn=====================================================================n");Seqlist *L;char ch[5];int i,res,a,b=1;while(b){printf("nn"卡塔尔;printf("1.成立 2.点名地方插入 3.按职务删除n"卡塔尔(قطر‎;printf("4.求开房总的数量 5.按房间号查找 6.按职责查找n"卡塔尔(英语:State of Qatar);printf("7.体现全体客户 8.价格排序 9.退出n");scanf("%d",a);switch(a){case 1: L=initseq();displayAll(L);break;case 2: printf("n输入插入的职分:n");scanf("%d",i);res=insertElem(L,i); if(res==1)displayAll(L);break;case 3:printf("n输入删除数据的职责:"卡塔尔(英语:State of Qatar);scanf("%d",i卡塔尔国;res=deleteElem(L,i卡塔尔(英语:State of Qatar);if(res==1卡塔尔displayAll(L卡塔尔国;break;case 4: printf("n已开的房间数为%d:n",lengthList(L));break;case 5:printf("n输入查找的房间号:"卡塔尔(英语:State of Qatar);scanf("%s",ch卡塔尔;locateElem(L,ch卡塔尔(英语:State of Qatar);break;case 6:printf("n输入要研究房间的地点:"卡塔尔(英语:State of Qatar);scanf("%d",i卡塔尔(قطر‎;locateElemByplace(L,i卡塔尔(قطر‎;break;case 7:displayAll(L卡塔尔;break;case 8:insertsort(L卡塔尔(英语:State of Qatar);break;case 9:printf ("已脱离"卡塔尔(英语:State of Qatar);b=0;break; }}}

如图,设置二维数组ch,想要删除数组中的有些成分;想删除有些成分后,删除地点后的要素全体前移,并释放最终黄金时代行成分的内部存款和储蓄器,笔者的代码运维会合世这种气象,最终的岗位显示为,想去掉它,并释放内部存款和储蓄器,首先希望能有大神教小编怎么把“”删掉,释放内存其次,自个儿学子小白,在那谢谢各位大神了。代码如下:#includestdio.h#includestdlib.hvoidadd(char**ch,ints);voiddel(char**ch,ints);main(){char**ch;ints,l;//设定多少个变量,为数老总度printf("请输入数组大小:"卡塔尔国;scanf("%d",s卡塔尔(英语:State of Qatar);//输入数组大小ch=(char**)malloc(s*sizeof(char卡塔尔卡塔尔国;//申请三个尺寸为s的数组,用p指向首地址printf("请输入数组中的成分的值:n"卡塔尔国;//赋值for(l=0;ls;l++卡塔尔国//为数组的因素赋值{ch[l]=(char*)malloc(s*sizeof(char*));scanf("%s",ch[l]卡塔尔国;getchar(卡塔尔(英语:State of Qatar);}for(l=0;ls;l++卡塔尔(قطر‎//输出数组中的全数因素{printf("%st",ch[l]);}printf("n"卡塔尔(قطر‎;add(ch,s卡塔尔(قطر‎;//调用add函数printf("新的数组:"卡塔尔;for(l=0;l=s;l++卡塔尔(قطر‎//输出新的全体数组成分{printf("%st",ch[l]卡塔尔国;}del(ch,s卡塔尔;//调用del函数printf("删除成分后的数组为:n"卡塔尔国;for(l=0;l=s;l++卡塔尔(英语:State of Qatar)//输出删除后的全部因素{printf("%st",ch[l]);}return0;}voidadd(char**ch,ints卡塔尔//add函数,目标是扩大有些地点的有些元素{intz;inti;printf("请输入要投入的地点:"卡塔尔(英语:State of Qatar);scanf("%d",i卡塔尔(قطر‎;if(i=0卡塔尔国{printf("非法输入,请重新输入"卡塔尔(英语:State of Qatar);add(ch,s卡塔尔;}for(z=s;z=i;z--卡塔尔国{ch[z]=ch[z-1];}ch[i-1]=(char*)malloc(s*sizeof(char*卡塔尔(قطر‎卡塔尔(قطر‎;printf("请输入要参预的数目:"卡塔尔(قطر‎;scanf("%s",ch[i-1]);}voiddel(char**ch,ints卡塔尔//del函数,指标是剔除某些地方的有些成分{intn;//设置变量,为地点序号intz;printf("请输入要去除的职位:"卡塔尔国;scanf("%d",n卡塔尔;//输入地方for(z=n;z=s;z++卡塔尔//将n后的全体因素前移一个岗位{ch[z-1]=ch[z];}ch[s-1]=NULL;//最后二个为空//ch[s-1]=(char*)realloc(*ch,s*sizeof(char*));}

本文由澳门太阳娱乐手机登录发布于 资质荣誉,转载请注明出处:二维动态数组如何完美删除数组中的某个元素并

关键词: