You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言链表嵌套中struct serie的访问与内存释放问题

问题解决方案

一、正确访问并创建struct serie链表

你写的struct list.serie start;存在两处错误:

  • list是指针类型,访问结构体成员必须用->而非.;
  • serie本身是struct serie*类型,不能直接用它声明变量,需明确使用struct serie*定义指针。

正确做法是:创建lista节点后,先将其serie指针初始化为NULL,再通过动态内存分配逐个添加serie节点,构建数列链表。可以增加逻辑让用户输入当前数列的元素个数和具体数值:

// 创建lista节点后,初始化serie链表指针
list->serie = NULL;
struct serie *serie_corrente = NULL;
struct serie *serie_precedente = NULL;

int num_elementi;
printf("Enter the number of elements for series %s: ", list->nome);
scanf("%d", &num_elementi);

for (int j = 0; j < num_elementi; j++) {
    serie_corrente = malloc(sizeof(struct serie));
    printf("Enter element %d: ", j+1);
    scanf("%d", &serie_corrente->n);
    serie_corrente->prossim = NULL;

    if (j == 0) {
        list->serie = serie_corrente; // 将第一个节点设为serie链表的头
    } else {
        serie_precedente->prossim = serie_corrente;
    }
    serie_precedente = serie_corrente;
}

二、释放struct serie链表的内存

释放内存时,需先遍历当前lista节点对应的serie链表,逐个释放每个serie节点,再释放lista节点,避免内存泄漏:

list = inizio;
while (list != NULL) {
    // 先释放当前lista节点关联的serie链表
    struct serie *serie_corrente = list->serie;
    while (serie_corrente != NULL) {
        struct serie *serie_prossima = serie_corrente->prossim;
        free(serie_corrente);
        serie_corrente = serie_prossima;
    }
    // 再释放lista节点
    struct lista *pros = list->prossima;
    free(list);
    list = pros;
}
list = NULL;

修改后的完整代码

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

struct lista
{
    char nome[30];
    struct serie *serie;
    struct lista *prossima;
};

struct serie
{
    int n;
    struct serie *prossim;
};

int main()
{
    int dim;

    struct lista *inizio = NULL;
    struct lista *list = NULL;
    struct lista *precedente = NULL;

    printf("How many series do you want to add? ");
    scanf("%d", &dim);

    for (int i = 0; i < dim; i++)
    {
        list = malloc(sizeof(struct lista));

        printf("Enter the name of the series: ");
        scanf("%s", list->nome);

        list->prossima = NULL;
        // 初始化serie链表指针
        list->serie = NULL;
        struct serie *serie_corrente = NULL;
        struct serie *serie_precedente = NULL;

        int num_elementi;
        printf("Enter the number of elements for series %s: ", list->nome);
        scanf("%d", &num_elementi);

        for (int j = 0; j < num_elementi; j++) {
            serie_corrente = malloc(sizeof(struct serie));
            printf("Enter element %d: ", j+1);
            scanf("%d", &serie_corrente->n);
            serie_corrente->prossim = NULL;

            if (j == 0) {
                list->serie = serie_corrente;
            } else {
                serie_precedente->prossim = serie_corrente;
            }
            serie_precedente = serie_corrente;
        }

        if (i == 0)
        {
            inizio = list;
        }
        else
        {
            precedente->prossima = list;
        }
        precedente = list;
    }
    
    // 打印所有数列的名称和元素
    list = inizio;
    while (list != NULL)
    {
        printf("The name of the series is: %s\n", list->nome);
        struct serie *serie_corrente = list->serie;
        printf("Elements: ");
        while (serie_corrente != NULL) {
            printf("%d ", serie_corrente->n);
            serie_corrente = serie_corrente->prossim;
        }
        printf("\n");
        list = list->prossima;
    }
    
    // 释放所有内存:先释放serie链表,再释放lista链表
    list = inizio;
    while (list != NULL)
    {
        struct serie *serie_corrente = list->serie;
        while (serie_corrente != NULL) {
            struct serie *serie_prossima = serie_corrente->prossim;
            free(serie_corrente);
            serie_corrente = serie_prossima;
        }
        struct lista *pros = list->prossima;
        free(list);
        list = pros;
    }
    list = NULL;
    
    return 0;
}

内容的提问来源于stack exchange,提问作者ShishRobot

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 17:17:55