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

上传人:b****5 文档编号:7860739 上传时间:2023-01-26 格式:DOCX 页数:7 大小:15.20KB
下载 相关 举报
数据结构C语言版 单链表源代码.docx_第1页
第1页 / 共7页
数据结构C语言版 单链表源代码.docx_第2页
第2页 / 共7页
数据结构C语言版 单链表源代码.docx_第3页
第3页 / 共7页
数据结构C语言版 单链表源代码.docx_第4页
第4页 / 共7页
数据结构C语言版 单链表源代码.docx_第5页
第5页 / 共7页
点击查看更多>>
下载资源
资源描述

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

《数据结构C语言版 单链表源代码.docx》由会员分享,可在线阅读,更多相关《数据结构C语言版 单链表源代码.docx(7页珍藏版)》请在冰豆网上搜索。

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

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

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

#include

#include

#definenull0

typedefintElemType;/*字符型数据*/

structLNode

{

ElemTypedata;

structLNode*next;

};

voidsetnull(structLNode**p);

intlength(structLNode**p);

ElemTypeget(structLNode**p,inti);

voidinsert(structLNode**p,ElemTypex,inti);

voiddele(structLNode**p,inti);

voiddisplay(structLNode**p);

intlocate(structLNode**p,ElemTypex);

voidmain()

{

structLNode*head,*q;/*定义静态变量*/

intselect,x1,x2,x3,x4;

inti,n;

intm,g;

chare,y;

setnull(&head);/*建设链表并设置为空表*/

printf("请输入数据长度:

");

scanf("%d",&n);

for(i=1;i<=n;i++)

{

printf("将数据插入到单链表中:

");

scanf("%d",&y);

insert(&head,y,i);

}/*插入数据到链表*/

display(&head);/*显示链表所有数据*/

printf("select1求长度length()\n");

printf("select2取结点get()\n");

printf("select3求值查找locate()\n");

printf("select4删除结点delete()\n");

printf("select0退出\n");

printf("inputyourselect:

");

scanf("%d",&select);

while(select!

=0)

{switch(select)

{

case1:

{

x1=length(&head);

printf("输出单链表的长度%d",x1);

display(&head);

}break;

case2:

{

printf("请输入要取得结点:

");

scanf("%d",&m);

x2=get(&head,m);

printf("%d",x2);

display(&head);

}break;

case3:

{

printf("请输入要查找的数据:

");

scanf("%d",&e);

x3=locate(&head,e);

printf("%d",x3);

display(&head);

}break;

case4:

{

printf("请输入要删除的结点:

");

scanf("%d",&g);

dele(&head,g);

display(&head);

}break;

}

printf("select1求长度length()\n");

printf("select2取结点get()\n");

printf("select3求值查找locate()\n");

printf("select4删除结点delete()\n");

printf("select0退出\n");

printf("inputyourselect:

");

scanf("%d",&select);

}

}

voidsetnull(structLNode**p)

{

*p=null;

}

intlength(structLNode**p)

{

intn=0;

structLNode*q=*p;

while(q!

=null)

{

n++;

q=q->next;

}

return(n);

}

ElemTypeget(structLNode**p,inti)

{

intj=1;

structLNode*q=*p;

while(j

=null)

{

q=q->next;

j++;

}

if(q!

=null)

return(q->data);

else

{printf("位置参数不正确!

\n");

return0;}

}

intlocate(structLNode**p,ElemTypex)

{

intn=0;

structLNode*q=*p;

while(q!

=null&&q->data!

=x)

{

q=q->next;

n++;

}

if(q==null)

return(-1);

else

return(n+1);

}

voidinsert(structLNode**p,ElemTypex,inti)

{

intj=1;

structLNode*s,*q;

s=(structLNode*)malloc(sizeof(structLNode));

s->data=x;

q=*p;

if(i==1)

{

s->next=q;

*p=s;

}

else

{

while(jnext!

=null)

{

q=q->next;

j++;

}

if(j==i-1)

{

s->next=q->next;

q->next=s;

}

else

printf("位置参数不正确!

\n");

}

}

voiddele(structLNode**p,inti)

{

intj=1;

structLNode*q=*p,*t;

if(i==1)

{

t=q;

*p=q->next;

}

else

{

while(jnext!

=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);

}

voiddisplay(structLNode**p)

{

structLNode*q;

q=*p;

printf("单链表显示:

");

if(q==null)

printf("链表为空!

");

elseif(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");

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > PPT模板 > 节日庆典

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1