欢迎来到天天文库
浏览记录
ID:15585634
大小:41.50 KB
页数:19页
时间:2018-08-04
《数据结构:顺序线性表》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、数据结构:顺序线性表//利用线性表的顺序存储结构实现线性表的创建、插入、删除、查找等算法/*顺序线性表*/#include#defineINITSIZE50#defineADDNODE10typedefstructelement{intdata;}ELEMENT;typedefstruct{ELEMENT*sq;intsize;intlength;}SLIST;SLISTsqlist,*list;ELEMENTelem,*p;voidlistcreate()/*建立线性表*/{inti;list=&sqlist;list->sq=(ELEMENT*)malloc(IN
2、ITSIZE*sizeof(ELEMENT))if(!list->sq)exit(0);list->size=INITSIZE;list->length=0;}if(list->sq!=NULL){printf("INPUTLISTLENGTH:");scanf("%d",&list->length);for(i=0;ilength;i++)scanf("%d",&list->sq[i]);}}voidfreelist()/*撤消线性表*/{if(list->sq!=NULL){free(list->sq);list->size=0;list->length=0;}}vo
3、idlistlen()/*求线性表长度*/{if(list->sq!=0){printf("listlengthis:");printf("%d",list->length);}getch();}intfindelement()/*查找数据元素*/{inti;printf("inputfindelemnt:");scanf("%d",&elem.data);for(i=0;ilength;i++)if(list->sq[i].data==elem.data){printf("elementposition:");printf("%d",i+1);getch()
4、;return(i+1);}printf("notfind!");getch();return(0);}voidlistinsert()/*数据元素插入*/{inti,position;ELEMENT*tmplist;printf("inputinsertelement:");scanf("%d",&elem.data);printf("inputinsertposition:");scanf("%d",&position);if(position<1
5、
6、position>list->length+1){printf("insertpositionerror");return;}if(
7、list->length==list->size){tmplist=(ELEMENT*)realloc(list->sq,(list->size+ADDNODE)*sizeof(ELEMENT));if(tmplist==NULL){printf("notspace");exit(0);}list->sq=tmplist;list->size=list->size+ADDNODE;}list->length++;for(i=list->length-1;i>=position-1;i--)list->sq[i+1]=list->sq[i];list->sq[position-1].da
8、ta=elem.data;}voidlistdelete()/*数据元素删除*/{inti,position;printf("inputdeleteelement:");scanf("%d",&elem.data);for(i=0;ilength;i++){if(list->sq[i].data==elem.data){list->length--;while(ilength){list->sq[i]=list->sq[i+1];i++;}}}}voidgetprior()/*求数据元素前驱*/{intpos;ELEMENTprior;pos=findele
9、ment();if(pos==0){printf("listnotfindelementreturn!!");return;}if(pos==1){printf("findelementlistfurst!!");return;}prior=list->sq[pos-2];printf("priorelementis:");printf("%d",prior.data);return;}voidgetnext()/*求后继*/{intpo
此文档下载收益归作者所有