资源描述:
《顺序表的相关算法(可运行)》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、#include#includetypedefintDataType;#defineMaxSize100#defineTrue1#defineFalse0typedefstruct{DataTypedata[MaxSize];intlength;}SeqList;SeqList*IniList(){SeqList*L;L=(SeqList*)malloc(sizeof(SeqList));inti;L->length=0;for(i=1;i<=5;i++){pri
2、ntf("请输入第%d个元素:",i);scanf("%d",&L->data[i-1]);L->length++;}returnL;}DataTypeGetNode(SeqList*L,inti){if((i<1)
3、
4、(i>L->length))return-1;//i的值非法,返回空类型elsereturnL->data[i-1];//返回第i个结点(下标是i-1)的值}intLengthList(SeqList*L){returnL->length;}intLocateList(SeqLis
5、t*L,DataTypex){inti;for(i=0;ilength;i++)if(L->data[i]==x)returni+1;if(i>=L->length){printf("线性表中不存在与%d值相等的结点!",x);}return-1;}intInsertList(SeqList*L,DataTypex,inti){intj;if(i<1
6、
7、i>L->length+1){printf("插入位置非法!");returnFalse;}for(j=L->length;j>=i-1;j-
8、-)L->data[j+1]=L->data[j];L->data[i-1]=x;L->length++;returnTrue;}intDeleteList(SeqList*L,inti){intj;if(i<1
9、
10、i>L->length){printf("您要删除的结点不存在");}returnFalse;for(j=i;jlength;j++)L->data[j-1]=L->data[j];L->length--;returnTrue;}intIsEmpty(SeqList*L){retu
11、rnL->length==0;}voidOutputList(SeqList*L){inti;for(i=0;ilength;i++)printf("%d",L->data[i]);}voidmain(){inti,x;SeqList*p;p=IniList();printf("********************************");printf("1.求顺序表长度。");printf("2.取表中第i个结点。");printf("3.按值查找。");printf
12、("4.插入结点。");printf("5.删除结点。");printf("6.判空表。");printf("7.输出顺序表元素。");printf("********************************");printf("请输入相应的数字完成相应的操作:");scanf("%d",&i);while(1){switch(i){case1:OutputList(p);printf("%d",LengthList(p));break;case2:printf("pl
13、easeinputwhichnodeyouwantget:");scanf("%d",&i);printf("%d",GetNode(p,i));break;case3:printf("pleaseinputx:");scanf("%d",&x);printf("%d",LocateList(p,x));break;case4:printf("pleaseinputthelocationandxwhichyouwanttoinsert:");scanf("%d,%d",&x,&i);Ins
14、ertList(p,x,i);OutputList(p);break;case5:printf("pleaseinputthelocationyouwanttodelete:");scanf("%d",&i);DeleteList(p,i);break;case6:IsEmpty(p)==1?printf("顺序表不为空!"):printf("顺序表为空!");break;case7:OutputList(p);}printf("请输入相应的