欢迎来到天天文库
浏览记录
ID:17907322
大小:102.50 KB
页数:29页
时间:2018-09-09
《c语言数据结构基本操作》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、/*顺序表的基本操作*/#include#include#include#include#defineLength10#defineLISTINCREMENT3#defineTRUE1#defineFALSE0#defineOK1#defineERROR-1#defineOVERFLOW-2typedefintStatus;typedefintElemType;typedefstructlineorder{int*elem
2、;intlength;intlistsize;}SqList;Statusinitlist_Sq(SqList&L)/*初始化顺序表*/{L.elem=(ElemType*)malloc(sizeof(ElemType)*Length);if(!L.elem)exit(OVERFLOW);L.listsize=Length;L.length=0;returnOK;}voiddestroylist(SqList&c)/*销毁顺序表*/{free(c.elem);c.elem=NULL;c.length=
3、0;}voidclearlist_Sq(SqList&c)/*清空顺序表*/{c.length=0;}Statuslistempty_Sq(SqListc)/*测试顺序表是否为空*/{if(c.length!=0)return(FALSE);return(TRUE);}StatusListInsert_Sq(SqList&L,inti,ElemTypee)/*在第i个位置上插入一个元素*/{intj,*newbase;if(i<1
4、
5、i>L.length+1)returnERROR;if(L.leng
6、th>=L.listsize){newbase=(ElemType*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));if(!newbase)exit(OVERFLOW);L.elem=newbase;L.listsize+=LISTINCREMENT;}for(j=L.length;j>=i;--j)L.elem[j]=L.elem[j-1];L.elem[j]=e;++L.length;returnOK;}intLocateE
7、lem_Sq(SqListL,ElemTypee)/*返回元素e在顺序表中的位置*/{inti=1;ElemType*p=L.elem;while(i<=L.length&&!(*p==e)){i++;p++;}if(i<=L.length)returni;returnFALSE;}StatusListDelete_Sq(SqList&L,inti,int&e)/*删除第i个位置上的元素*/{intj;if(i<1
8、
9、i>L.length)returnERROR;e=L.elem[i-1];for(j
10、=i;j11、12、intf("4-------Getaelement'slocationtintheSqList.");printf("5-------CleartheSequentialList.");printf("6-------Exit.");printf("**************************");printf("");}voidmenuselect(SqListL){intk,i,done=1;ElemTypee;charc
11、12、intf("4-------Getaelement'slocationtintheSqList.");printf("5-------CleartheSequentialList.");printf("6-------Exit.");printf("**************************");printf("");}voidmenuselect(SqListL){intk,i,done=1;ElemTypee;charc
12、intf("4-------Getaelement'slocationtintheSqList.");printf("5-------CleartheSequentialList.");printf("6-------Exit.");printf("**************************");printf("");}voidmenuselect(SqListL){intk,i,done=1;ElemTypee;charc
此文档下载收益归作者所有