帮下忙!~~
为什么返回结构指针会有错??#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student
{
int num,p_num;
char name[10];
struct student *next;
};
typedef struct student s_data;
s_data *top=NULL;
int chunru(int num,char *name,int p_num)
{
s_data *new_data;
new_data=(s_data *)malloc(sizeof(s_data));
if(new_data==NULL)
return 0;
else
{
strcpy(new_data->name,name);
new_data->num=num;
new_data->p_num=p_num;
new_data->next=top;
top=new_data;
return 1;
}
}
s_data* chaxun(char *name)
{
s_data *pitop=top;
do
{
if((strcmp(name,pitop->name)==0)
return pitop;
else
pitop=pitop->next;
}while(pitop!=NULL);
return NULL;
}
void show()
{
s_data *pitop=top;
while (pitop != NULL)
{
puts("----------------------------------------");
printf("学号\t姓名\t电话号码\n");
printf("%d\t%s\t%d\n",pitop->num,pitop->name,pitop->p_num);
pitop=pitop->next;
}
puts("----------------------------------------");
}
int main()
{
int select,s_num,s_p_num;
char s_name[10],c_name[10];
s_data *ptr;
do
{
printf("(1)存入 (2)查询 (3)遍历 (4)离开");
scanf("%d",&select);
switch (select)
{
case 1:
printf("请输入学生的学号 姓名 电话号码: ");
scanf("%d%s%d",&s_num,s_name,&s_p_num);
if(!(chunru(s_num,s_name,s_p_num)))
printf("存入失败!\n");
break;
case 2:
if (top=NULL)
printf("无数据!\n");
else
{
printf("请输入要查询的姓名:");
scanf("%s",c_name);
ptr=chaxun(c_name);
if(ptr==NULL)
printf("无此数据!\n");
else
printf("%d\t%s\t%d\n",ptr->num,ptr->name,ptr->p_num);
break;
case 3:
if (top=NULL)
printf("无数据!\n");
else
show();
break;
}
}while (select != 4);
if (top=NULL)
{
while(top!=NULL)
{
s_data *pitop=top;
free(pitop);
top=top->next;
}
}
return 0;
}