输入若干个学生信息(包括学号、姓名和成绩),输入学号为0时输入结束,建立一个单向链表,再输入一个成绩值,将成绩大于等于该值的学生信息输出。
提示:
定义函数struct stud_node *Creat_Stu_Doc()完成创建链表
定义函数struct stud_node *DeleteDoc(struct stud_node *head,int min_score)将分数低于min_score的结点删除
定义函数void Ptrint_Stu_Doc(struct stud_node *head)打印链表
输入输出示例:括号内为说明,无需输入输出
输入样例:
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 *Creat_Stu_Doc();
struct stud_node *DeleteDoc(struct stud_node *head,int min_score);
void Ptrint_Stu_Doc(struct stud_node *head);
struct stud_node {
int num;
char name[10];
int score;
struct stud_node *next;
};
int main()
{
int min_score;
struct stud_node *head;
head=NULL;
head=Creat_Stu_Doc();
scanf("%d",&min_score);
head=DeleteDoc(head,min_score);
Ptrint_Stu_Doc(head);
return 0;
}
struct stud_node *Creat_Stu_Doc()
{
struct stud_node *head,*p,*t;
head=NULL;
int num;
scanf("%d",&num);
while(num)
{
p=(struct stud_node *)malloc(sizeof(struct stud_node ));
p->num=num;
scanf("%s %d",p->name,&p->score);
if(head==NULL)
{
head=p;
t=head;
}
else
{
t->next=p;
t=t->next;
}
t->next=NULL;
scanf("%d",&num);
}
return head;
}
struct stud_node *DeleteDoc(struct stud_node *head,int min_score)
{
struct stud_node *p,*p1;
while(head->score<min_score)
{
p1=head;
head=head->next;
free(p1);
}
p=head,p1=head->next;
while(p1!=NULL)
{
if(p1->score<min_score)
{
p->next = p1->next;
free(p1);
p1=p->next ;
}
else
{
p=p->next;
p1=p1->next ;
}
}
return head;
}
void Ptrint_Stu_Doc(struct stud_node *head)
{
struct stud_node *p;
for(p=head;p!=NULL;p=p->next)
{printf("%d %s %d\n",p->num,p->name ,p->score);
}
}