sunflower781 2022-09-07 08:56 采纳率: 0%
浏览 37

进来看看吧,标题怎么这么难

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

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的学生,并返回结果链表的头指针。

struct stud_node *createlist()
{
    struct stud_node *pMove=NULL,*head=NULL,*tail=NULL;
    pMove=(struct stud_node*)malloc(sizeof(struct stud_node));
    scanf("%d",&pMove->num);
    while(pMove->num!=0)
    {
        scanf("%s %d",pMove->name,&pMove->score);
        if(head==NULL){
           head=pMove; 
        }
        else{
            tail->next=pMove;
        }
        tail=pMove;
        pMove=(struct stud_node*)malloc(sizeof(struct stud_node));
    }
    return head;
}
struct stud_node *deletelist( struct stud_node *head, int min_score )
{
    struct stud_node* p=head;
    if(!head)
        return NULL;
    p=(struct stud_node*)malloc(sizeof(struct stud_node));
    while(p->next)
    {
        if(p->score<min_score)
        {
            struct stud_node* temp=p;
            p=temp->next;
            free(temp);
        }
        else
        {
            p=p->next;
        }
    }
    return head;
}

为什么我这个代码是错的QAQ,有没有好心的给个意见!

  • 写回答

2条回答 默认 最新

  • qzjhjxj 2022-09-07 11:20
    关注

    修改如下,供参考:

    #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* pMove = NULL, * head = NULL, * tail = NULL;
        while (1)       //while (pMove->num != 0)//修改
        {
            pMove = (struct stud_node*)malloc(sizeof(struct stud_node));
            pMove->next = NULL;                   //修改
            scanf("%d", &pMove->num);
            if (pMove->num == 0) {               //修改 
                free(pMove);
                break;                           //修改
            }
            scanf("%s %d", pMove->name, &pMove->score);
            if (head == NULL) {
                head = pMove;
            }
            else {
                tail->next = pMove;
            }
            tail = pMove;
            //pMove = (struct stud_node*)malloc(sizeof(struct stud_node)); //修改
        }
        return head;
    }
    struct stud_node* deletelist(struct stud_node* head, int min_score)
    {
        struct stud_node* p = head, * pre = NULL;  //修改
        if (!head)
            return NULL;
        //p = (struct stud_node*)malloc(sizeof(struct stud_node)); //修改
        while (p)   //while (p->next)
        {
            if (p->score < min_score)
            {
                if (p == head) {     //修改
                    head = p->next;  //修改
                    free(p);        //修改
                    p = head;       //修改 
                }
                else {
                    pre->next = p->next; //修改
                    free(p);            //修改 
                    p = pre;            //修改
                }
            }
            else{
                pre = p;           //修改
                p = p->next;
            }
        }
        return head;
    }
    void print(stud_node* L)
    {
        stud_node* p = L;
        while (p) {
            printf("%d %s %d\n", p->num, p->name, p->score);
            p = p->next;
        }
    }
    int main()
    {
        struct stud_node* L;
        L = createlist();
        L = deletelist(L, 100);
        print(L);
        return 0;
    }
    
    
    评论

报告相同问题?

问题事件

  • 创建了问题 9月7日

悬赏问题

  • ¥15 如何使用python 实现对串口/dev/ttyUSB0进行上锁,使得该串口只能在一个python脚本中使用,其他脚本不能操作这个串口
  • ¥15 晶体塑性有限元——Damask求解
  • ¥15 写出这个有没有人能写一下今天中午就要
  • ¥30 设计一个图形用户界面来控制你机械臂的运动
  • ¥30 3d打印机无法识别到SD卡,如何解决?(相关搜索:格式化)
  • ¥15 RPG游戏架构设计和开发方法
  • ¥15 前端返回pdf时不显示内容
  • ¥50 如何在不能联网影子模式下的电脑解决usb锁
  • ¥20 服务器redhat5.8网络问题
  • ¥15 如何利用c++ MFC绘制复杂网络多层图