Kallen·Kaslana 2021-05-03 19:25 采纳率: 0%
浏览 45

pintia 学生成绩链表处理

程序在Dev里有时可以正常运行 有时读不进去东西 在pta里面交的话是编译错误

#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()
{
    stud_node *p,*head=(struct stud_node *)malloc(sizeof(struct stud_node*));
    p=head;
    int a;
    scanf("%d",&a);
    while(a!=0)
    {
        p->num=a;
        scanf("%s%d",p->name,&p->score);
        p->next=NULL;
        scanf("%d",&a);
        if(a)
        {
        p->next=(struct stud_node *)malloc(sizeof(struct stud_node *));
        p=p->next;
        }
        else
        break;
    }
    return head;
}

struct stud_node *deletelist( struct stud_node *head, int min_score )
{
    struct stud_node *b,*p=head,*head1=head;
    int first=1;
    while(p!=NULL)
    {
        if(p->score<min_score)
        {
            if(first)
            {
                head1=p->next;    
            }
            else
            {
                b->next=p->next;
                free(p);
                p=b->next;
                continue;
            }
        }
        else
        {
            first=0;    
        }
        b=p;
        p=p->next;
    }
    return head1;
}

  • 写回答

6条回答 默认 最新

  • CSDN专家-黄老师 2021-05-03 20:46
    关注

    将代码做个增减法,先从最小的代码量开始执行,然后再增加,直到出现异常就知道那里出错了

    评论

报告相同问题?

悬赏问题

  • ¥15 nasm x86 变量归零
  • ¥65 Tree 树形控件实现单选功能,可以使用element也可以手写一个,实现全选为全选状态
  • ¥60 寻抓云闪付tn组成网页付款链接
  • ¥20 如何通过sentry收集上传Android ndk的崩溃?
  • ¥30 有关数值生成的稳健性探讨
  • ¥20 关于C/C++图形界面设计问题
  • ¥15 QT+Gstreamer框架开发视频采集,无法将waylandsink视频绑定qt窗口
  • ¥15 vs2010开发 WFP(windows filtering platform)异常
  • ¥30 8*8*25的矩阵和1*8*25的矩阵相乘
  • ¥15 Ubuntu20.04主机有两个网口,如何配置将其中一个网口用来接入外网,另一个网口用来给其他设备上网