链表 locatelist问题,程序有错,大侠们帮忙分析一下吧

#include<stdio.h>#include<stdlib.h>typedefstructlnode{intdata;structlnode*next;}lnode... #include<stdio.h>
#include<stdlib.h>

typedef struct lnode
{
int data;
struct lnode *next;
}lnode,*linklist;

void creatlist(linklist l,int n)
{
int i;
linklist p;
l=(linklist)malloc(sizeof(lnode));
l->next=NULL;
for(i=n;i>0;i--)
{
p=(linklist)malloc(sizeof(lnode));
scanf("%d",&p->data);
p->next=l->next;
l->next=p;
}
}

int locate(linklist l,int x)
{
linklist p;
int n=1;
p=l;
while(p->next!=NULL)
{
if(p->data==x)
break;
p=p->next;
n++;
}
return n;
}

void display(linklist l)
{
linklist p;
p=l;
while(p->next!=NULL)
{
printf("%3d",p->data);
p=p->next;
}
}

void main()
{
int n,x,s;
lnode L;
printf("请输入链表长度:\n");
scanf("%d",&n);
printf("链表赋值:\n");
creatlist(&L,n);
display(&L);
printf("请输入要查找的数:\n");
scanf("%d",&x);
s=locate(&L,x);
printf("该数在链表中的位置:%d\n",s);
}
展开
 我来答
xiaosuo623
2011-03-24 · 超过19用户采纳过TA的回答
知道答主
回答量:29
采纳率:0%
帮助的人:47.1万
展开全部
#include<stdio.h>
#include<stdlib.h>

typedef struct lnode
{
int data;
struct lnode *next;
}lnode,*linklist;

void creatlist(linklist *l,int n)
{
int i;
linklist p;
(*l)=(lnode*)malloc(sizeof(lnode));
(*l)->next=NULL;
for(i=n;i>0;i--)
{
p=(linklist)malloc(sizeof(lnode));
scanf("%d",&p->data);
p->next=(*l)->next;
(*l)->next=p;
}
}

int locate(linklist l,int x)
{
linklist p;
int n=1;
p=l->next; //l是有头指针的链表
while(p->next!=NULL)
{
if(p->data==x)
break;
p=p->next;
n++;
}
return n;
}

void display(linklist l)
{
linklist p;
p=l->next; //l是有头指针的链表
while(p!=NULL)
{
printf("%3d",p->data);
p=p->next;
}
}

void main()
{
int n,x,s;
linklist L=NULL; //链表应该是链表指针,这样申请只能是一个节点
printf("请输入链表长度:\n");
scanf("%d",&n);
printf("链表赋值:\n");
creatlist(&L,n); //要想在函数中给变量申请空间,必须是变量的指针
display(L);
printf("请输入要查找的数:\n");
scanf("%d",&x);
s=locate(L,x);
printf("该数在链表中的位置:%d\n",s);
}
1398768015
2011-03-23
知道答主
回答量:16
采纳率:0%
帮助的人:0
展开全部
printf("该数在链表中的位置:%d\n",s);
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式