C语言单向链表操作:创建学生成绩链表并删除低于分数线学生

本题要求实现两个函数:

  1. createlist(): 将输入的学生成绩组织成单向链表。
  2. deletelist(struct stud_node *head, int min_score): 从链表中删除成绩低于 min_score 的学生结点。

函数接口定义:

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\n', 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

代码实现:

struct stud_node *createlist()
{
    struct stud_node *head, *tail, *p;
    head = tail = NULL;
    int num, score;
    char name[20];
    scanf('%d', &num);
    while (num) {
        scanf('%s %d', name, &score);
        p = (struct stud_node *)malloc(sizeof(struct stud_node));
        p->num = num;
        strcpy(p->name, name);
        p->score = score;
        p->next = NULL;
        if (!head) {
            head = tail = p;
        } else {
            tail->next = p;
            tail = p;
        }
        scanf('%d', &num);
    }
    return head;
}

struct stud_node *deletelist(struct stud_node *head, int min_score)
{
    struct stud_node *p, *pre, *q;
    p = head;
    while (p && p->score < min_score) {  // 头结点需要删除
        head = p->next;
        free(p);
        p = head;
    }
    while (p) {
        pre = p;
        p = p->next;
        if (p && p->score < min_score) {
            q = p;
            pre->next = p->next;
            p = p->next;
            free(q);
        }
    }
    return head;
}

代码解析:

  1. createlist() 函数

    • 初始化头指针和尾指针:head = tail = NULL;
    • 使用 scanf 读取学生信息(学号、姓名、成绩)。
    • 使用 malloc 为每个学生分配内存空间,并创建新的节点 p
    • 将学生信息填充到新节点 p 中。
    • 将新节点 p 插入到链表末尾。
    • 如果是第一个节点,则将 headtail 指向新节点。
    • 循环读取学生信息,直到输入学号为 0。
    • 返回链表头指针 head
  2. deletelist() 函数

    • 初始化指针 p 为头指针 head,用于遍历链表。
    • 如果头节点的成绩低于 min_score,则删除头节点并更新头指针。
    • 使用 pre 指针指向当前节点的前一个节点,用于删除当前节点。
    • 遍历链表,如果当前节点的成绩低于 min_score,则删除当前节点。
    • 使用 free 释放被删除节点的内存空间。
    • 返回结果链表的头指针 head

注意:

  • 使用 malloc 分配内存空间后,需要使用 free 释放内存,避免内存泄漏。
  • 在删除节点时,需要更新前一个节点的 next 指针,以维护链表的完整性。
  • 代码中的注释解释了每个步骤的逻辑和操作,帮助理解代码功能。

学习要点:

  • 理解单向链表的基本概念和操作。
  • 掌握创建链表、插入节点、删除节点、遍历链表等操作的实现方法。
  • 了解内存管理的重要性,并学会使用 mallocfree 函数。

拓展练习:

  • 实现双向链表操作。
  • 实现链表排序功能。
  • 实现链表查找功能。
  • 尝试使用链表解决其他问题,例如实现栈、队列等数据结构。

祝你学习愉快!


原文地址: https://www.cveoy.top/t/topic/nydK 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录