资源描述:
《c语言程序——线性顺序表》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、程序清单:线性顺序表1:#defineMaxSize100typedefcharET;#include#include#includestructlist{ETalist[MaxSize];intsize;};voidSetnull(structlist*p){if(p->alist[0]==NULL)exit(1);else{p->alist[0]=NULL;p->size=0;}}intLength(structlist*p){returnstrlen(p->a
2、list);}ETGet(structlist*p,inti){if(i<0
3、
4、i>p->size){printf("thenumberiswrong!");exit(1);}elsereturn(p->alist[--i]);}intLocate(structlist*p,ETx){intj;for(j=0;p->alist[j]!=NULL;j++)if(p->alist[j]==x)break;return++j;}voidInsert(structlist*p,inti,ETx){if(p->size==Max
5、Size){printf("thelistisfull");exit(1);}intj;j=p->size;for(;j>=(i-1);j--)p->alist[j+1]=p->alist[j];p->alist[i-1]=x;p->size=p->size+1;}voidDelete(structlist*p,inti){if(i<0
6、
7、i>p->size){printf("thenumberiswrong!");exit(1);}for(;p->alist[i]!=NULL;i++)p->alist[i-1]=
8、p->alist[i];p->alist[i-1]=NULL;p->size=p->size-1;}voidDisplay(structlist*p){inti;for(i=0;p->alist[i+1]!=NULL;i++)printf("%c->",p->alist[i]);printf("%c",p->alist[i]);printf("");}voidmain(){inti,j,k,l;ETa,b,c;structlistp;Setnull(&p);printf("pleaseentertheelements:
9、");scanf("%s",&p.alist);p.size=Length(&p);printf("mylistis:");Display(&p);printf("thelengthofthelistis%d",p.size);printf("pleaseentertheelementsyouwanttosearch:");scanf("%s",&b);j=Locate(&p,b);printf("%c'spositionis:%d",b,j);printf("pleaseenterthepositio
10、nyouwanttosearch:");scanf("%d",&i);a=Get(&p,i);printf("theelementinposition%dis:%c",i,a);printf("pleaseenterthepositionandtheelement:");scanf("%d,%c",&k,&c);Insert(&p,k,c);printf("afterinsert%c,thelistis:",c);Display(&p);printf("pleaseenterthepositionyouwantto
11、delete:");scanf("%d",&l);Delete(&p,l);printf("afterdeletetheelementinposition%d,thelistis:",l);Display(&p);printf("");}线性顺序表2:#include#include#include#include#include#defineMAXSIZE1000#defineN400staticintRandA
12、rray[MAXSIZE];voidRandomNum(){inti;srand((int)time(NULL));for(i=0;i