数据结构--顺序表的基本操作(C语言实现)

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

#includestdio.h#includestdlib.h#defineTRUE1#defineFALSE0#defineOK1#defineERROR0#defineOVERFLOW-2#defineLIST_INIT_SIZE100#defineLISTINCREMENT10typedefintstatus;typedefintElemType;typedefstruct{ElemType*elem;intlength,listsize;}SqList;statusInitList(SqList&L)//初始化{L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));if(!L.elem)exit(OVERFLOW);L.listsize=LIST_INIT_SIZE;L.length=0;returnOK;}statusBuild(SqList&L)//建立表{inti,n;printf(请输入元素个数n和n个元素\n);scanf(%d,&n);if(nLIST_INIT_SIZE)//如果n大于当前空间{L.elem=(ElemType*)realloc(L.elem,(n+LISTINCREMENT)*sizeof(ElemType));if(!L.elem)exit(OVERFLOW);L.listsize=n+LISTINCREMENT;}for(i=0;in;i++)scanf(%d,L.elem+i);L.length=n;returnOK;}voidPrint(SqList&L)//输出表中元素和长度{inti;for(i=0;iL.length;i++)printf(%d,*(L.elem+i));printf(\n长度为:%d\n\n,L.length);}voidTips()//提示函数{printf(请选择你的想要的操作:\n);printf(1输出顺序表及顺序表的长度\n);printf(2删除值为x的结点\n);printf(3删除给定位置i的结点\n);printf(4将顺序表逆置\n);printf(5将顺序表按升序排序\n);printf(6将x插入到顺序表的适当位置上\n);printf(7将两个有序表合并\n);printf(0退出\n\n);}statusListDelete1(SqList&L,intx)//删除值为X的元素{inti;for(i=0;iL.length;i++)if(*(L.elem+i)==x)break;if(i==L.length)returnERROR;for(i++;iL.length;i++)*(L.elem+i-1)=*(L.elem+i);L.length--;returnOK;}statusListDelete2(SqList&L,intx)//删除第X个元素{inti;if(x0||x=L.length)returnERROR;for(i=x+1;iL.length;i++)*(L.elem+i-1)=*(L.elem+i);L.length--;returnOK;}voidInverse(SqList&L)//逆置函数{inti,t;for(i=0;iL.length/2;i++){t=*(L.elem+i);*(L.elem+i)=*(L.elem+L.length-i-1);*(L.elem+L.length-i-1)=t;}}voidSort(SqList&L)//冒泡排序(升序){inti,j,t;for(i=1;iL.length;i++)for(j=0;jL.length-i;j++){if(*(L.elem+j)*(L.elem+j+1)){t=*(L.elem+j);*(L.elem+j)=*(L.elem+j+1);*(L.elem+j+1)=t;}}printf(已按升序排列\n\n);}statusListInsert(SqList&L,intx)//将X插入,使仍然有序{inti,k;if(L.length=L.listsize){L.elem=(ElemType*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));if(!L.elem)exit(OVERFLOW);L.listsize+=LISTINCREMENT;}for(i=0;iL.length;i++)if(x*(L.elem+i))break;k=i;for(i=L.length;ik;i--)*(L.elem+i)=*(L.elem+i-1);*(L.elem+k)=x;L.length++;returnOK;}statusMerger(SqList&L,SqList&Lb)//合并两个线性表{inti,j,k;SqListLc;InitList(Lc);if(Lc.listsizeL.length+Lb.length){Lc.elem=(ElemType*)realloc(Lc.elem,(L.length+Lb.length+LISTINCREMENT)*sizeof(ElemType));if(!L.elem)exit(OVERFLOW);Lc.listsize=L.length+Lb.length+LISTINCREMENT;}i=j=k=0;while(iL.length&&jLb.length){if(*(L.elem+i)*(Lb.elem+j)){*(Lc.elem+k)=*(L.elem+i);k++;i++;}else{*(Lc.elem+k)=*(Lb.elem+j);k++;j++;}}while(iL.length){*(Lc.elem+k)=*(L.elem+i);k++;i++;}while(jLb.length){*(Lc.elem+k)=*(Lb.elem+j);k++;j++;}Lc.length=L.length+Lb.length;L=Lc;returnOK;}intmain(){intop,x,flag;SqListL,Lb;InitList(L);Build(L);Tips();scanf(%d,&op);while(op){switch(op){case1:Print(L);break;case2:printf(请输入要删除的数据X:\n);scanf(%d,&x);flag=ListDelete1(L,x);if(flag)printf(删除成功!!\n\n);elseprintf(元素不存在,删除失败!!\n\n);break;case3:printf(请输入要删除的位置i:\n);scanf(%d,&x);flag=ListDelete2(L,x-1);//第i个元素对应的下标为i-1if(flag)printf(删除成功!!\n\n);elseprintf(元素不存在,删除失败!!\n\n);break;case4:Inverse(L);break;case5:Sort(L);break;case6:printf(请输入要插入的数据X:\n);scanf(%d,&x);flag=ListInsert(L,x);if(flag)printf(插入成功!!\n\n);elseprintf(插入失败!!\n\n);break;case7:printf(请输入Lb的内容:\n);InitList(Lb);Build(Lb);flag=Merger(L,Lb);if(flag)printf(合并成功!!\n\n);break;}Tips();scanf(%d,&op);}return0;}

1 / 6
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功