【c语言篇】每日一题-pta-实验11-2-2 学生成绩链表处理

题目如下

裁判测试程序样例: 

#include <stdio.h>
#include <stdlib.h>

struct stud_node {
     int    num;
     char   name[20];
     int    score;
     struct stud_node *next;
};

struct stud_node *createlist();
struct stud_node *deletelist( struct stud_node *head, int min_score );

int main()
{
    int min_score;
    struct stud_node *p, *head = NULL;

    head = createlist();
    scanf("%d", &min_score);
    head = deletelist(head, min_score);
    for ( p = head; p != NULL; p = p->next )
        printf("%d %s %d\n", p->num, p->name, p->score);

    return 0;
}

/* 你的代码将被嵌在这里 */

 

 代码如下

struct stud_node *createlist()
{
    struct stud_node *head = (struct stud_node *)malloc(sizeof(struct stud_node));
    head->next = NULL;
    struct stud_node *tail = head;
    int number;
    scanf("%d",&number);
    while(number!=0)
    {
        struct stud_node *tmp = (struct stud_node *)malloc(sizeof(struct stud_node));
        tmp->next = NULL;
        tmp->num = number;
        scanf("%s",tmp->name);
        scanf("%d",&tmp->score);
        tail->next = tmp;//将临时结点连接到尾部
        tail = tmp;
        scanf("%d",&number);
    }
    return head;
}

struct stud_node *deletelist( struct stud_node *head, int min_score )
{
    //在进行操作前,进行安全性检验
    if(!head)
    {
        return head;
    }
    //创建一个结点进行遍历
    struct stud_node *cur = head;
    while(cur->next)
    {
        if(cur->next->score < min_score)
        {
            //创建临时结点,保存当前结点
            struct stud_node *temp = cur->next;
            //修改cur->next指向待删除结点的下一个结点 
            cur->next = temp->next;
            //释放temp
            free(temp);
        }
        else{
            cur = cur->next;
        }
    }
   return head->next;
}
  • 11
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值