| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 651 人关注过本帖
标题:[求助]关于创建链表和查寻的问题
只看楼主 加入收藏
无赖
Rank: 1
等 级:新手上路
帖 子:46
专家分:0
注 册:2006-8-17
收藏
 问题点数:0 回复次数:1 
[求助]关于创建链表和查寻的问题

设线性表(a1,a2,a3.....an)存储在带头结点的单链表中,求出该线性表中值为x的元素的序号.如果x不存在,则序号为0
请各位帮我看 看出什么问题了!

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
typedef struct node
{ int data;
struct node *next;
}LNode;
LNode *creat(int n)
{ LNode *p,*head,*s; int i;
head=(LNode*)malloc(sizeof(LNode));
head->next=NULL;
p=head;
printf("Please input the number:");
for(i=0;i<n;++i)
{ s=(LNode*)malloc(sizeof(LNode));
scanf("%d",&s->data);
s->next=NULL;
p->next=s;p=s;
} printf("%d",&p->data);
return (head);
}
void rearch(LNode *head,int x)
{ LNode *q;
int num;
num=0;
q=head->next;
printf("The station is:");
while(q->next!=NULL)
{ if(q->data==x)
{ num++;
printf("\n%2d",&num);
}
q=q->next;
}
}
print(LNode *head,int n)
{ LNode *l;
int i;
l=head;
for(i=0;i<n;i++)
{
printf("\n%2d\n",&l->data);
}
}
void main()
{ LNode *head;
int n,x;
clrscr();
printf("Please input n and x:");
scanf("%d%d",&n,&x);
print(creat(n), n);

getch();
}

搜索更多相关主题的帖子: 链表 查寻 
2006-10-12 15:01
cinsin
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2007-6-25
收藏
得分:0 

[QUOTE]设线性表(a1,a2,a3.....an)存储在带头结点的单链表中,求出该线性表中值为x的元素的序号.如果x不存在,则序号为0
请各位帮我看 看出什么问题了!

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
typedef struct node
{ int data;
struct node *next;
}LNode;
LNode *creat(int n)
{ LNode *p,*head,*s; int i;
head=(LNode*)malloc(sizeof(LNode));
head->next=NULL;
p=head;
printf("Please input the number:");
for(i=0;i<n;++i)
{ s=(LNode*)malloc(sizeof(LNode));
scanf("%d",&s->data);
s->next=NULL;
p->next=s;p=s;
} printf("%d",&p->data);/*这里用不到输出,用也应该用循环来实现*/
return (head);
}
void rearch(LNode *head,int x)
{ LNode *q;
int num;
num=0;
q=head->next;
printf("The station is:");
while(q->next!=NULL)
{ if(q->data==x)
{ num++;
printf("\n%2d",&num);
}
q=q->next;
}
}
print(LNode *head,int n)
{ LNode *l;
int i;
l=head;
for(i=0;i<n;i++)
{
printf("\n%2d\n",&l->data);
}
}
void main()
{ LNode *head;
int n,x;
clrscr();
printf("Please input n and x:");
scanf("%d%d",&n,&x);
print(creat(n), n);

getch();
}

[/QUOTE]
你还是看看主函数把。主函数都没调用void rearch(LNode *head,int x)怎么可能实现。。。希望你写程序最好能带上注释。。养成良好的习惯。。。


曾几何时,我也是菜鸟...
2007-06-25 08:23
快速回复:[求助]关于创建链表和查寻的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018194 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved