实验10.1 单向链表的建立

【问题描述】输人若干个学生信息(包括学号、姓名和成绩),输人学号为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;
    }
    
}

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值