【问题描述】输人若干个学生信息(包括学号、姓名和成绩),输人学号为0时输人结束,建立一个单向链表,再输人一个成绩值,将成绩大于等于该值的学生信息输出。试编写相应程序。
【样例输入】
1 zhang 78
2 wang 80
3 li 75
4 zhao 85
0
80
【样例输出】
2 wang 80
4 zhao 85
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct stud_node{
int num;
char name[20];
int score;
struct stud_node *next;
};
//新建链表
struct stud_node *Creat_Stu_Doc(void);
//遍历链表
void Print_Stu_Doc(struct stud_node *head,int min_score);
int main()
{
struct stud_node *head;
int min_score;
head=Creat_Stu_Doc();
scanf("%d",&min_score);
Print_Stu_Doc(head,min_score);
}
//新建链表
struct stud_node *Creat_Stu_Doc()
{
struct stud_node *head,*tail, *p;
int num, score;
char name[20];
int size = sizeof(struct stud_node);
head=tail=NULL;
scanf("%d",&num);
while(num != 0){
scanf("%s%d",name,&score);
p=(struct stud_node*)malloc(size);
p->num=num;
strcpy(p->name,name);
p->score=score;
p->next=NULL;
if(head==NULL)
head=p;
else
tail->next=p;
tail=p;
scanf("%d",&num);
}
return head;
}
//遍历操作
void Print_Stu_Doc(struct stud_node *head,int min_score)
{
struct stud_node *ptr;
ptr=head;
while (ptr!=NULL) {
if (ptr->score>=min_score)
printf("%d %s %d\n",ptr->num,ptr->name,ptr->score);
ptr=ptr->next;
}
}