zoukankan      html  css  js  c++  java
  • PTA 学生成绩链表处理(C语言)

    本题要求实现两个函数,一个将输入的学生成绩组织成单向链表;另一个将成绩低于某分数线的学生结点从链表中删除。

    函数接口定义:

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

    函数createlist利用scanf从输入中获取学生的信息,将其组织成单向链表,并返回链表头指针。链表节点结构定义如下:

    struct stud_node {
        int              num;      /*学号*/
        char             name[20]; /*姓名*/
        int              score;    /*成绩*/
        struct stud_node *next;    /*指向下个结点的指针*/
    };

    输入为若干个学生的信息(学号、姓名、成绩),当输入学号为0时结束。

    函数 deletelist 从以 head 为头指针的链表中删除成绩低于 min_score 的学生,并返回结果链表的头指针。

    裁判测试程序样例:

    #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
    ", p->num, p->name, p->score);
    
        return 0;
    }
    
    /* 你的代码将被嵌在这里 */

    输入样例:

    1 zhang 78
    2 wang 80
    3 li 75
    4 zhao 85
    0
    80

    输出样例:

    2 wang 80
    4 zhao 85

    思路:这道题目的思路很简单,每输入一组数据,将该组数据存到一个临时结点,再将这个结点与链表进行正确连接;删除时从头历遍链表,删除score<min_score 的结点。

    但是,这道题我却做了一节课,原因就是 对于每次输入的数据,我没有将它存到一个新创建的临时结点,自始至终都存到了同一片空间,导致链表链接有问题。

    代码:

    struct stud_node *createlist(){
        int tem=0;
        struct stud_node *firstnode=NULL,*endnode=NULL;
        struct stud_node *temnode=(struct stud_node *)malloc(sizeof(struct stud_node));
        while(scanf("%d", &temnode->num)&&temnode->num){
            scanf("%s %d", &temnode->name,&temnode->score);
            temnode->next=NULL;
            if(tem==0){
                tem=1;
                firstnode=endnode=temnode;
            }
            else{
                endnode->next=temnode;
                endnode=endnode->next;
            }
            temnode=(struct stud_node *)malloc(sizeof(struct stud_node));//问题关键,之前少写了这句话
        }
        return firstnode;
    }
    struct stud_node *deletelist( struct stud_node *head, int min_score ){
        while(head&&head->score<min_score)
            head=head->next;
        if(head==NULL)
            return NULL;
        struct stud_node *temnode=head->next;
        struct stud_node *prenode=head;
        while(temnode!=NULL){
            if(temnode->score<min_score)
                prenode->next=temnode->next;
            else
                prenode=prenode->next;
            temnode=temnode->next;
        }
        return head;
    }
  • 相关阅读:
    Algebra, Topology, Differential Calculus, and Optimization Theory For Computer Science and Machine Learning 第4章 读书笔记(待更新)
    Algebra, Topology, Differential Calculus, and Optimization Theory For Computer Science and Machine Learning 第3章 读书笔记(待更新)
    Algebra, Topology, Differential Calculus, and Optimization Theory For Computer Science and Machine Learning 第1,2章 读书笔记(待更新)
    Tkinter的Message组件
    Git 实操/配置/实践
    mysq5.7.32-win安装步骤
    行为型模式之模板方法
    结构型模式之组合模式
    结构型模式之享元模式
    结构型模式之外观模式
  • 原文地址:https://www.cnblogs.com/bjxqmy/p/11694165.html
Copyright © 2011-2022 走看看