收藏 分销(赏)

数据结构C语言版-单链表源代码.doc

上传人:a199****6536 文档编号:3996037 上传时间:2024-07-24 格式:DOC 页数:10 大小:20.54KB 下载积分:8 金币
下载 相关 举报
数据结构C语言版-单链表源代码.doc_第1页
第1页 / 共10页
数据结构C语言版-单链表源代码.doc_第2页
第2页 / 共10页


点击查看更多>>
资源描述
/*单链表的各种操作*/ #include <stdio。h〉 #include <malloc.h〉 #define null 0 typedef int ElemType; /* 字符型数据*/ struct LNode { ElemType data; struct LNode *next; }; void setnull(struct LNode **p); int length (struct LNode **p); ElemType get(struct LNode **p,int i); void insert(struct LNode **p,ElemType x,int i); void dele(struct LNode **p,int i); void display(struct LNode **p); int locate(struct LNode **p,ElemType x); void main() { struct LNode *head,*q; /*定义静态变量*/ int select,x1,x2,x3,x4; int i,n; int m,g; char e,y; setnull(&head); /*建设链表并设置为空表*/ printf(”请输入数据长度: ”); scanf(”%d”,&n); for(i=1;i<=n;i++) { printf("将数据插入到单链表中: "); scanf("%d",&y); insert(&head,y,i); } /*插入数据到链表*/ display(&head); /*显示链表所有数据*/ printf(”select 1 求长度 length()\n”); printf(”select 2 取结点 get()\n”); printf(”select 3 求值查找 locate()\n”); printf(”select 4 删除结点 delete()\n”); printf(”select 0 退出\n”); printf(”input your select: ”); scanf("%d”,&select); while(select!=0) {switch(select) { case 1: { x1=length(&head); printf(”输出单链表的长度%d ”,x1); display(&head); }break; case 2: { printf("请输入要取得结点: ”); scanf(”%d”,&m); x2=get(&head,m); printf(”%d",x2); display(&head); }break; case 3: { printf(”请输入要查找的数据: ”); scanf(”%d”,&e); x3=locate(&head,e); printf(”%d”,x3); display(&head); }break; case 4: { printf(”请输入要删除的结点: "); scanf("%d”,&g); dele(&head,g); display(&head); }break; } printf("select 1 求长度 length()\n”); printf(”select 2 取结点 get()\n”); printf(”select 3 求值查找 locate()\n"); printf("select 4 删除结点 delete()\n”); printf(”select 0 退出\n"); printf(”input your select: ”); scanf("%d”,&select); } } void setnull(struct LNode **p) { *p=null; } int length (struct LNode **p) { int n=0; struct LNode *q=*p; while (q!=null) { n++; q=q—>next; } return(n); } ElemType get(struct LNode **p,int i) { int j=1; struct LNode *q=*p; while (j〈i&&q!=null) { q=q—〉next; j++; } if(q!=null) return(q—〉data); else {printf("位置参数不正确!\n"); return 0;} } int locate(struct LNode **p,ElemType x) { int n=0; struct LNode *q=*p; while (q!=null&&q—>data!=x) { q=q—>next; n++; } if(q==null) return(—1); else return(n+1); } void insert(struct LNode **p,ElemType x,int i) { int j=1; struct LNode *s,*q; s=(struct LNode *)malloc(sizeof(struct LNode)); s-〉data=x; q=*p; if(i==1) { s—>next=q; *p=s; } else { while(j〈i—1&&q-〉next!=null) { q=q-〉next; j++; } if(j==i-1) { s—〉next=q—〉next; q-〉next=s; } else printf(”位置参数不正确!\n"); } } void dele(struct LNode **p,int i) { int j=1; struct LNode *q=*p,*t; if(i==1) { t=q; *p=q->next; } else { while(j<i-1&&q-〉next!=null) { q=q—〉next; j++; } if(q—〉next!=null&&j==i—1) { t=q->next; q-〉next=t—〉next; } else printf(”位置参数不正确!\n"); } if(t!=null) free(t); } void display(struct LNode **p) { struct LNode *q; q=*p; printf(”单链表显示: "); if(q==null) printf("链表为空!”); else if (q—>next==null) printf(”%d\n",q-〉data); else { while(q—>next!=null) { printf("%d->”,q—〉data); q=q->next; } printf(”%d",q—>data); } printf(”\n"); }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服