教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 范文大全 > 资料大全 >

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

来源:网络收集 时间:2026-09-06
导读: /*单链表的各种操作*/ #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(s

/*单链表的各种操作*/

#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)

{

}

int length (struct LNode **p)

{

int n=0; *p=null;

struct LNode *q=*p;

while (q!=null)

{

}

return(n);

}

ElemType get(struct LNode **p,int i)

{

int j=1; struct LNode *q=*p; n++; q=q->next;

while (j<i&&q!=null)

{

}

if(q!=null)

return(q->data); q=q->next; j++;

else

}

int locate(struct LNode **p,ElemType x) {

int n=0; {printf("位置参数不正确!\n"); return 0;}

struct LNode *q=*p;

while (q!=null&&q->data!=x)

{

} q=q->next; n++;

if(q==null)

return(-1);

else

}

void insert(struct LNode **p,ElemType x,int i) {

int j=1; return(n+1);

struct LNode *s,*q;

s=(struct LNode *)malloc(sizeof(struct LNode)); s->data=x;

q=*p;

if(i==1)

{

}

else

{

while(j<i-1&&q->next!=null) { s->next=q; *p=s;

} q=q->next; j++;

if(j==i-1)

{

}

else

printf("位置参数不正确!\n"); s->next=q->next; q->next=s;

}

}

void dele(struct LNode **p,int i) {

int j=1;

struct LNode *q=*p,*t; if(i==1)

{

} t=q; *p=q->next;

{

while(j<i-1&&q->next!=null) {

}

if(q->next!=null&&j==i-1) {

t=q->next;

q->next=t->next; q=q->next; j++;

}

}

} else printf("位置参数不正确!\n"); if(t!=null) free(t);

void display(struct LNode **p) {

struct LNode *q;

printf("单链表显示: "); if(q==null)

printf("链表为空!"); else if (q->next==null)

printf("%d\n",q->data); else

{

{ } } printf("%d",q->data); printf("%d->",q->data); q=q->next; while(q->next!=null)

printf("\n");

}

…… 此处隐藏:771字,全部文档内容请下载后查看。喜欢就下载吧 ……
数据结构C语言版 单链表源代码.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/fanwen/2192487.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)