链表查找输入不是链表的数据else无法显示
#include<stdio.h>#include<stdlib.h>
struct node
{
int data;
char name[20];
struct node *pnext;
};
struct node *search(struct node *g,int num)
{
while(g->data!=num&&g!=NULL)
g = g->pnext;
return g;
}
int main()
{
int n,i,num;
struct node *pre,*h,*p,*m;
p = (struct node *)malloc(sizeof(struct node));
h = p;
pre = p;
printf("请输入共有几个结点:");
scanf("%d",&n);
printf("请输入第1个结点的data:");
scanf("%d",&p->data);
printf("请输入第1个结点的name:");
scanf("%s",p->name);
p->pnext = NULL;
for(i = 2;i <= n;i++)
{
p = (struct node *)malloc(sizeof(struct node));
printf("请输入第%d个结点的data:",i);
scanf("%d",&p->data);
printf("请输入第%d个结点的name:",i);
scanf("%s",p->name);
p->pnext = NULL;
pre->pnext = p;
pre = p;
}
printf("请输入要查找的data:");
scanf("%d",&num);
m = search(h,num);
if(m->data != 0)
{
printf("查找的name为:%s",m->name);
}
else
{
printf("没有要查找的数据");
}
return 0;
}