数据结构 C语言链表操作.doc

数据结构 C语言链表操作.doc

ID:61510429

大小:20.00 KB

页数:5页

时间:2021-02-08

数据结构 C语言链表操作.doc_第1页
数据结构 C语言链表操作.doc_第2页
数据结构 C语言链表操作.doc_第3页
数据结构 C语言链表操作.doc_第4页
数据结构 C语言链表操作.doc_第5页
资源描述:

《数据结构 C语言链表操作.doc》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库

1、#include#include#defineMAXSIZE10000typedefintElemType;typedefstructlist{ElemType*elem;intlistsize;intlength;}Sqlist;voidinitlist_sq(Sqlist*L)//创建一个链表{L->elem=(ElemType*)malloc(MAXSIZE*sizeof(ElemType));if(!L->elem)exit(0);L->length=0;L->listsize=MAXSIZE;}voidinsert_sq(Sqli

2、st*L,intn,ElemTypex)//插入元素(n为插入的位置,x为插入的值){inti;if(n<0

3、

4、n>L->length+1){printf("niserror");return;}for(i=L->length;i>=n;i--){L->elem[i+1]=L->elem[i];}L->elem[i]=x;//L->elem[i-1]=x;L->length++;//printf("**********Sqlistinserted**********");}voidcreat_sq(Sqlist*L)//输入链表的值{inttablen,i=0;Elem

5、Typetemp;printf("pleaseinputthetablelength");scanf("%d",&tablen);printf("pleaseinputaserialnumber");/*do{scanf("%d",&temp);insert_sq(L,i,temp);i++;}while((ilistsize));*/while(ielem[i]=temp;L->length++;i++;}}voiddisplay_sq(Sqlist*L)//输出当前链表的值{

6、inti;i=0;do{printf("%dt",L->elem[i]);}while(++ilength);printf("**********Sqlistdiplay**********");}intSearch_sq(Sqlist*L,ElemTypex)//在当前链表中查找x{inti;i=0;do{if(L->elem[i]==x){returni+1;}}while(++ilength);return0;}ElemTypeSearch_sq_byV(Sqlist*L,intn)//(输入位置n,查找该位置的值){returnL->elem[

7、n-1];}voiddelete_sq(Sqlist*L,intpos)//删除pos为删除的位置{do{L->elem[pos-1]=L->elem[pos];pos++;}while(poslength);L->length--;}intmain(void){SqlistL1;ElemTypetemp;charcmd;inti=0;initlist_sq(&L1);printf("X=EXIT,C=CREATE,P=DISPLAY,I=INSERT,S=SEARCH,D=DELETE");while(1){cmd=getchar();switch(cmd){ca

8、se'C':case'c':creat_sq(&L1);printf("X=EXIT,C=CREAT,P=DISPLAY,I=INSERT,S=SEARCH,D=DELETE");break;case'I':case'i':printf("pleaseinputthenumbertobeinserted");scanf("%d",&temp);printf("pleaseinputthepositiontobeinserted(1~100)");scanf("%d",&i);insert_sq(&L1,i,temp);//insert_sq(&L1,i-1,temp

9、);printf("**********Sqlistinserted**********");printf("X=EXIT,C=CREAT,P=DISPLAY,I=INSERT,S=SEARCH,D=DELETE");break;case'S':case's':printf("pleaseinputthenumberwanted");scanf("%d",&temp);if(Search_sq(&L1,temp)){printf("该数在链表中的位置是第%d个.",Searc

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。