数据结构c语言版单链表源代码.docx

数据结构c语言版单链表源代码.docx

ID:62802316

大小:22.81 KB

页数:12页

时间:2021-05-31

数据结构c语言版单链表源代码.docx_第1页
数据结构c语言版单链表源代码.docx_第2页
数据结构c语言版单链表源代码.docx_第3页
数据结构c语言版单链表源代码.docx_第4页
数据结构c语言版单链表源代码.docx_第5页
资源描述:

《数据结构c语言版单链表源代码.docx》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库

1、/*单链表的各种操作*/ftinclude<>^include<>#definenull0typedefintElemType;/*字符型数据*/struetLNodeElemTypedata;struetLNode*next;};voidsetnull(struetLNode**p);intlength(struetLNode**p);ElemTypeget(struetLNode**p,inti);voidinsert(struetLNode**p,ElemTypex,inti);voiddele(str

2、uetLNode**p,inti);voiddisplay(struetLNode**p);intlocate(struetLNode**p,ElemTypex);voidmain()structLNode*head,*q;/*定义静态变量*/intselect,xl,x2,x3,x4;inti,n;intm,g;chare,y;setnull(&head);/*建设链表并设置为空表*/printf("请输入数据长度:”);scanf&n);for(i=l;i<=n;i++)printfr将数据插入到单链表中

3、:〃);scanf(〃%d〃,&y);insert(&head,y,i);}/*插入数据到链表*/display(&head);/*显示链表所有数据*/printf(z/select1求长度length()z/);printf(z/select2取结点get(),z):printf(z/select3求值查找locate()?,);printf(z/select4删除结点delete()?,);printf(/zselect0退出〃);printf(〃inputyourselect:“);

4、scanf&select)•1while(select!=0){switch(select)case1:xl=length(&head);printfC输出单链表的长度%d〃,xl);display(&head);}break;case2:printf("请输入要取得结点:”);scanf(〃%d〃,&m);x2=get仗head,m);printfx2);display(&head);}break;case3:printf(〃请输入要查找的数据:x3=locate(&head,e);CL>CL>㊀]—兰Se

5、o•:丨eaiq{Fc一田密p<>建葷二muAu•1owl迥钱AuroAu04。①m3匸£」d寸4。①m3匸£」do七<3m3voidsetnull(struetLNode**p)*p=null;)intlength(struetLNode**p)intn二0;struetLNode*q二*p;while(q!=null)n++;q=q->next;)return(n);)ElemTypeget(struetLNode**p,inti)intj=l;struetLNode*q二*p;while(j〈i&

6、&q!二null)q=q->next;j++;)if(q!=null)return(q->data);else{printfC位置参数不正确!");return0;}intlocate(struetLNode**p,ElemTypex)1intn二0;struetLNode*q二*p;while(q!=null&&q—>data!=x)n++;if(q二二null)return(-1);elsereturn(n+1);voidinsert(struetLNode**p,ElemTypex,inti)int

7、j二1;struetLNode*s,*q;s=(struetLNode*)malloc(sizeof(struetLNode));s->da.ta二x;q二*p;if(i==l){s~>next二q;*p=s;elseq=q->next;s~>next二q-〉next;q->next二s;elseprintf(〃位置参数不正确!〃);voiddele(struetLNode**p,inti)1intj二1;struetLNode*q=*p,*t;if(i==l)t二q;elsewhile(j

8、>next!二null)(q=q->next;j卄;if(q->next!二null&&j=iT)(t二q—>next;q->next二t~>next;elseprintfC位置参数不正确!");}if(t!二null)free(t);)voiddisplay(struetLNode**p)1structLNode*q;q二*p;printf(,z单链表显7K:“);if(q=null)prin

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

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

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