欢迎来到天天文库
浏览记录
ID:40646440
大小:14.03 KB
页数:6页
时间:2019-08-05
《数据结构--顺序表的基本操作(C语言实现)》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、#include#include#defineTRUE 1#defineFALSE 0#defineOK 1#defineERROR 0#defineOVERFLOW-2#defineLIST_INIT_SIZE100#defineLISTINCREMENT 10typedefintstatus;typedefintElemType;typedefstruct{ ElemType*elem; intlength,listsize;}SqList;statusInitList(SqList&L)//初始化{ L.elem=
2、(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个元素"); scanf("%d",&n); if(n>LIST_INIT_SIZE)//如果n大于当前空间 { L.elem=(ElemType*)realloc(L.elem,(n+LISTINCR
3、EMENT)*sizeof(ElemType)); if(!L.elem)exit(OVERFLOW); L.listsize=n+LISTINCREMENT; } for(i=0;i4、 printf("请选择你的想要的操作:"); printf("<1>输出顺序表及顺序表的长度"); printf("<2>删除值为x的结点"); printf("<3>删除给定位置i的结点"); printf("<4>将顺序表逆置"); printf("<5>将顺序表按升序排序"); printf("<6>将x插入到顺序表的适当位置上"); printf("<7>将两个有序表合并"); printf("<0>退出");}statusListDelete1(SqList&L,intx)//删除值为X的元素{ inti; for5、(i=0;i6、7、x>=L.length) returnERROR; for(i=x+1;i8、m+i); L.length--; returnOK;}voidInverse(SqList&L)//逆置函数{ inti,t; for(i=0;i9、*(L.elem+j+1)) { t=*(L.elem+j); *(L.elem+j)=*(L.elem+j+1); *(L.elem+j+1)=t; } } printf("已按升序排列");}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.e
4、 printf("请选择你的想要的操作:"); printf("<1>输出顺序表及顺序表的长度"); printf("<2>删除值为x的结点"); printf("<3>删除给定位置i的结点"); printf("<4>将顺序表逆置"); printf("<5>将顺序表按升序排序"); printf("<6>将x插入到顺序表的适当位置上"); printf("<7>将两个有序表合并"); printf("<0>退出");}statusListDelete1(SqList&L,intx)//删除值为X的元素{ inti; for
5、(i=0;i6、7、x>=L.length) returnERROR; for(i=x+1;i8、m+i); L.length--; returnOK;}voidInverse(SqList&L)//逆置函数{ inti,t; for(i=0;i9、*(L.elem+j+1)) { t=*(L.elem+j); *(L.elem+j)=*(L.elem+j+1); *(L.elem+j+1)=t; } } printf("已按升序排列");}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.e
6、
7、x>=L.length) returnERROR; for(i=x+1;i8、m+i); L.length--; returnOK;}voidInverse(SqList&L)//逆置函数{ inti,t; for(i=0;i9、*(L.elem+j+1)) { t=*(L.elem+j); *(L.elem+j)=*(L.elem+j+1); *(L.elem+j+1)=t; } } printf("已按升序排列");}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.e
8、m+i); L.length--; returnOK;}voidInverse(SqList&L)//逆置函数{ inti,t; for(i=0;i
9、*(L.elem+j+1)) { t=*(L.elem+j); *(L.elem+j)=*(L.elem+j+1); *(L.elem+j+1)=t; } } printf("已按升序排列");}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.e
此文档下载收益归作者所有