为什么不能查找呢?
#include "stdio.h"#include "malloc.h"
typedef struct node{
int data;
struct node *next;
}NODE;
NODE *create();
print_data(NODE *);
NODE *find_data(NODE *,int);
NODE *lookup_data(NODE *,int);
main()
{
int m,n;
NODE *a,*b,*c;
a=create();
print_data(a);
printf("请输入你要查找的数:");
scanf("%d",&m);
b=find_data(a,m);
if(b!=NULL)
printf("找到%d!\n",b->data);
else
printf("找不到该数!\n");
printf("请输入你要查找的节点:");
scanf("%d",&n);
c=lookup_data(a,n);
if(c !=NULL)
printf("该节点的值是:%d\n",c->data);
else
printf("输入的节点错误或找不到该数!\n");
}
NODE *create()
{
int a;
char str='*';
NODE *head,*p,*q;
head=(NODE *)malloc(sizeof(NODE));
q=head;
printf("Please input a list:");
while(str!='\n')
{
scanf("%d",&a);
p=(NODE *)malloc(sizeof(NODE));
p->data=a;
q->next=p;
q=p;
str=getchar();
}
p->next=NULL;
return head;
}
print_data(NODE *head)
{
NODE *p;
p=head->next;
while (p !=NULL)
{
printf("%5d",p->data);
p=p->next;
}
printf("\n");
}
NODE *find_data(NODE *head,int a)
{
NODE *p;
p=head->next;
while((p!=NULL)&&(p->data!=a));
p=p->next ;
return p;
}
NODE *lookup_data(NODE *head,int i)
{
NODE *p;
int j=1;
p=head->next;
while((p!= NULL)&&(j<i));
{
p=p->next;
j++;
}
return p;
}
编译通过,输入数后也能显示,但是查找时出问题!