欢迎来到天天文库
浏览记录
ID:50368393
大小:87.00 KB
页数:29页
时间:2020-03-08
《C语言数据结构基本操作.doc》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、/*顺序表的基本操作*/#include#include#include#include#defineLength10#defineLISTINCREMENT3#defineTRUE1#defineFALSE0#defineOK1#defineERROR-1#defineOVERFLOW-2typedefintStatus;typedefintElemType;typedefstructlineorder{int*elem;intlength;intlistsize;}SqLi
2、st;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=0;}voidclearlist_Sq(SqList&c)/*清空顺序表*/{c.length=0;}Statu
3、slistempty_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.length>=L.listsize){newbase=(ElemType*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(
6、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;}intLocateElem_Sq(SqListL,ElemTypee)/*返回元素e在顺序表中的位置*/{inti=1;ElemType*p=L.elem;while(i<=L.length&&!(*p==e)){i++;p++;}if(i<=
7、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=i;j10、'slengthis%d.It'sempty!",L.length);elseprintf("SequentialList'slengthis%d.Theseelementare:",L.length);for(i=0;i11、tadataintheSequentialList.");printf("3-------DeleteadataintheSequentialList.");printf("4-------Getaelement'slocationtintheSqList.");printf("5-------CleartheSequentialList.");printf("6-------Exit.");printf("**************************");printf("");}voidmenuselect(SqLis12、tL){intk,i,done=1;ElemTypee;charc
10、'slengthis%d.It'sempty!",L.length);elseprintf("SequentialList'slengthis%d.Theseelementare:",L.length);for(i=0;i11、tadataintheSequentialList.");printf("3-------DeleteadataintheSequentialList.");printf("4-------Getaelement'slocationtintheSqList.");printf("5-------CleartheSequentialList.");printf("6-------Exit.");printf("**************************");printf("");}voidmenuselect(SqLis12、tL){intk,i,done=1;ElemTypee;charc
11、tadataintheSequentialList.");printf("3-------DeleteadataintheSequentialList.");printf("4-------Getaelement'slocationtintheSqList.");printf("5-------CleartheSequentialList.");printf("6-------Exit.");printf("**************************");printf("");}voidmenuselect(SqLis
12、tL){intk,i,done=1;ElemTypee;charc
此文档下载收益归作者所有