资源预览内容
第1页 / 共5页
第2页 / 共5页
第3页 / 共5页
第4页 / 共5页
第5页 / 共5页
亲,该文档总共5页全部预览完了,如果喜欢就下载吧!
资源描述
#include#include#include#include#defineLIST_INIT_SIZE100#defineLISTINCREMENT10#defineOK1#defineERROR-1#defineOVERFLOW-1#defineENDFLAG0typedefintStatus;typedefintElemType;#defineOUTFORMAT%d#defineINFORMAT%dtypedefstructElemType*elem;intlength;intlistsize;SqList;StatusInitList(SqList*L)L-elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType);if(!L-elem)/如果没有分配成功exit(OVERFLOW);/退出,显示()内内容L-length=0;L-listsize=LIST_INIT_SIZE;returnOK;StatusInputdata(SqList*L)/SqList*L定义首节点的地址ElemTypetemp,*newbase;printf(nInputthedataofthesequenciallist:nNote:0istheendingflag:n);scanf(INFORMAT,&temp);while(temp!=ENDFLAG)if(L-length=L-listsize)newbase=(ElemType*)realloc(L-elem,(L-listsize+LISTINCREMENT)*sizeof(ElemType);/扩大空间,把旧的地址拷贝到新空间if(!newbase)exit(OVERFLOW);L-elem=newbase;L-listsize+=LISTINCREMENT;L-elemL-length+=temp;scanf(INFORMAT,&temp);returnOK;StatusListInsert(SqList*L,inti,ElemTypee)ElemType*p,*q,*newbase;if(iL-length)returnERROR;if(L-length=L-listsize)Newbase=(elemType*)realloc(L-elem,(L-listsize+LISTINCREMENT)*sizeof(ElemType);if(!newbase)exit(OVERFLOW);L-elem=newbase;L-listsize+=LISTINCREMENT;q=&(L-elemi-1);for(p=&(L-elemL-length-1);p=q;-p)*(p+1)=*p;*q=e;+L-length;returnOK;voidMyDisplay(SqListL)inti;for(i=0;iL.length;i+)printf(OUTFORMAT,L.elemi);printf(n);voidmain(void)SqListL;ElemTypetemp;inti;if(!InitList(&L)/如果初始化失败printf(Toinitializethesequenciallistfailn);getch();/如果失败,按任意键退出exit(0);if(!Inputdata(&L)printf(Toinputthedataofthesequenciallistfailn);getch();exit(0);MyDisplay(L);printf(Inputthedatathatyouwanttoinsert:);scanf(INFORMAT,&temp);printf(Inputtheinsert_location:);scanf(%d,&i);if(!ListInsert(&L,i,temp)printf(Toinsertfailn);getch();exit(0);MyDisplay(L);getch();
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号