简体   繁体   English

试图在 C 中“释放”一个链表

[英]Trying to 'free' a linked list in C

I've started C programming yesterday and tried to implement a linked list (just really really basic).我昨天开始了 C 编程,并试图实现一个链表(只是非常基本的)。

So far everything works pretty fine except for freeing a list.到目前为止,除了释放列表之外,一切都很好。

First, here's my code:首先,这是我的代码:

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

/*
 * Struct for elements of the linked list
 */
struct element {
    struct element* nextelement;
    int value;
};

/*
 * Struct for a list itself
 */
struct liste {
    struct element* headelement;
};

/*
 * Function to create new elements
 */
struct element* createelement(int value) {
    struct element* dummyelement = malloc(sizeof(struct element));
    dummyelement->nextelement = NULL;
    dummyelement->value = value;
    return dummyelement;
}

/*
 * Function to create new (empty) lists
 */
struct liste* createlist() {
    struct liste* dummylist = malloc(sizeof(struct liste));
    dummylist->headelement = NULL;
    return dummylist;
}

/*
 * Add an element to a given list
 */
void addelement(struct liste* liste, int value) {

    struct element* dummyelement = createelement(value);
    if (liste->headelement == NULL) {
        liste->headelement = dummyelement;
    } else {
        struct element* iterelement = liste->headelement;
        while (iterelement->nextelement != NULL) {
            iterelement = iterelement->nextelement;
        }
        iterelement->nextelement = dummyelement;
    }

}

/*
 * Plot the elements of a given list
 */
void plotlist(struct liste* liste) {
    if (liste->headelement != NULL) {
        struct element* iterelement = liste->headelement;
        printf("%d\n", iterelement->value);
        while (iterelement->nextelement != NULL) {
            iterelement = iterelement->nextelement;
            printf("%d\n", iterelement->value);
        }
    } else {
        printf("Where is my head?\n");
    }
}

/*
 * This should completely remove the list, but it fails...
 */
void removelist(struct liste* liste) {
    if (liste->headelement != NULL) {
        struct element* iterelement = liste->headelement;
        struct element* nextelement = iterelement->nextelement;
        free(iterelement);
        while (nextelement != NULL) {
            iterelement = nextelement;
            nextelement = iterelement->nextelement;
            free(iterelement);
        }
    }

    free(liste);
}

int main(void) {

    /*
     * Creates a new list
     * Plots the (empty) list
     * Adds two elements to the list
     * Plots the list again
     * Removes the list
     * Last plot shouldn't really happen, but it does.
     */
    struct liste* mylist = createlist();
    printf("First plot.\n");
    plotlist(mylist);
    addelement(mylist, 1);
    addelement(mylist, 2);
    printf("Second plot.\n");
    plotlist(mylist);
    removelist(mylist);
    printf("Third plot.\n");
    plotlist(mylist);

    return 0;

}

I get the following output:我得到以下输出:

First plot.
Where is my head?
Second plot.
1
2
Third plot.
33

Well, obviously the '33' is my problem.好吧,显然“33”是我的问题。 I really don't know how this can be the output... Furthermore I don't know why my 'removelist' isn't working properly.我真的不知道这怎么可能是输出......此外我不知道为什么我的“removelist”不能正常工作。 I am freeing everything which I've allocated with 'malloc'.我正在释放我用'malloc'分配的所有东西。 What am I doing wrong?我究竟做错了什么?

You do not check inside plotlist whether your list actually exists.您不会在plotlist内部检查您的列表是否确实存在。 You directly try to access the headelement .您直接尝试访问headelement Better do the following:最好做到以下几点:

void plotlist(struct liste* liste) {
    if(liste == NULL){
        printf("This list does not even exist.\n");
    }
    else if (liste->headelement != NULL) {
        // ...
    } else {
        printf("Where is my head?\n");
    }
}

As removelist also frees the list structure as such, you better set the pointer to NULL inside main.由于removelist也释放了列表结构,因此最好在 main 中将指针设置为NULL Otherwise you get undefined behaviour upon accessing freed memory.否则,访问释放的内存时会出现未定义的行为。

int main(void) {
    // ...
    removelist(mylist);
    mylist = NULL;
    printf("Third plot.\n");
    plotlist(mylist);

    return 0;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM