学生信息链表一个小问题求解!
程序代码:#include<stdio.h>
#include<stdlib.h>
struct student
{
int num;
char name[30];
int score;
struct student * pnext;
};
struct student * creat_list(struct student * h); //建立单链表
void print_list(struct student * h); //输出单链表
void search_list(struct student * h); //搜寻函数
int main()
{
struct student * head;
char ren;
head = NULL;
printf("请输入学生信息,直至序号为0为止\n");
head = creat_list(head);
printf("----------------------------------------\n");
print_list(head);
printf("----------------------------------------\n");
search_list(head);
}
struct student * creat_list(struct student * h) //建立单链表 ,这一段没有问题
{
struct student * p1,*p2;
p1 = p2 = (struct student *)malloc(sizeof(struct student));
printf("请输入学生学号:");
scanf("%d",&p2 -> num);
printf("请输入学生姓名:");
scanf("%s",&p2 -> name);
printf("请输入学生成绩:");
scanf("%d",&p2 -> score);
p2 -> pnext = NULL;
while(p2 -> num != 0)
{
if(h == NULL)
h = p2;
else
p1 -> pnext = p2;
p1 = p2;
p2 = (struct student *)malloc(sizeof(struct student));
if(p2 != NULL)
{
printf("请输入学生学号:");
scanf("%d",&p2 -> num);
if(p2 -> num == 0) break;
printf("请输入学生姓名:");
scanf("%s",&p2 -> name);
printf("请输入学生成绩:");
scanf("%d",&p2 -> score);
p2 -> pnext = NULL;
}
}
return h;
}
void print_list(struct student * h) //输出单链表函数,没有问题
{
struct student * temp;
temp = h;
printf("学号\t姓名\t成绩\n");
while(temp != NULL)
{
printf("%d\t%s\t%d",temp -> num,temp -> name,temp -> score);
printf("\n");
temp = temp -> pnext;
}
}
void search_list(struct student * h) //搜寻函数,用来寻找学生学号!
{
struct student * temp;
int date;
char x;
temp = h;
printf("是否进行查询工作?(y/n): ");
scanf("%d",&x);
if(x == 'y')
{
printf("请输入学生学号:\n"); //就是这一段!为什么程序就没有执行呢?
scanf("%d",&date); // 直接就结束了呢?
if(date == 0)
printf("查无此人,请重输!");
while(temp != NULL)
{
if(date == temp -> num)
{
printf("学号\t姓名\t成绩\n");
printf("%d\t%s\t%d",temp -> num,temp -> name,temp -> score);
}
else
temp = temp -> pnext;
}
}
}




