链表删除节点bug

问题描述 投票:0回答:1

不敢相信我会问这个。

我写了一个简单的链表,似乎有一个奇怪的错误我找不到。请帮助我。

我添加

1
,然后将
2
添加到前面。然后我删除一个值为
2
的节点,即头节点。

就在

delete(head, 2);
返回之前,我有
head->data == 1
。但在 main 中调用之后,我得到了
head->value == 0
。 这是代码:

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

typedef struct linked_list
{
    int data;
    struct linked_list *next;
} linked_list_t;

void print_list(linked_list_t *head)
{
    linked_list_t *curr = head;
    printf("LIST: ");
    while (curr)
    {
        printf("%d ", curr->data);
        curr = curr->next;
    }
    printf("\n");
    fflush(stdout);
    
}

linked_list_t *add_to_front(linked_list_t *head, int data) 
{
    linked_list_t *new = malloc(sizeof(linked_list_t));
    if (!new) exit(1);
    new->next = head;
    new->data = data;
    return new;
}

void delete(linked_list_t *head, int data) 
{
    linked_list_t *current = head;
    linked_list_t *previous = NULL;
    // move current to the position of the node to delete
    while (current != NULL && current->data != data) 
    {
        previous = current;
        current = current->next;
    }

    if (current)
    {
        if (previous) 
        {
            // if the node to delete is not head
            previous->next = current->next;
        }
        else
        {
            // if the node to delete is head
            head = current->next;
        }
    }
    free(current);
}

int main()
{
    linked_list_t *head = NULL;
    print_list(head);
    // prints "LIST:"
    head = add_to_front(head, 1);
    head = add_to_front(head, 2);
    print_list(head);
    // prints "LIST: 2 1"
    // head->data = 1 just before delete() exits
    delete(head, 2);
    // head->data = 0 right after delete() exits
    print_list(head);
    // prints "LIST: 0 1" instead of "LIST: 1" 
}
c debugging linked-list
1个回答
0
投票

你的程序在第三个出现段错误,嗯,可以说是最终的,请致电

print_list()
。这是因为之前对
delete(head, 2)
的调用最终释放了
head
指向的节点。您有两个选择,要么返回新的
head
,要么传入
**head
,以便您可以更新它:

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

typedef struct linked_list {
    int data;
    struct linked_list *next;
} linked_list_t;

void print_list(linked_list_t *head) {
    printf("LIST:");
    for(linked_list_t *curr = head; curr; curr = curr->next) {
        printf(" %d", curr->data);
    }
    printf("\n");
    fflush(stdout);

}

linked_list_t *add_to_front(linked_list_t *head, int data) {
    linked_list_t *new = malloc(sizeof(linked_list_t));
    if (!new) exit(1);
    new->next = head;
    new->data = data;
    return new;
}

linked_list_t *delete(linked_list_t *head, int data) {
    linked_list_t *current = head;
    linked_list_t *previous = NULL;
    for(; current && current->data != data; current = current->next) {
        previous = current;
    }
    if (current) {
        if (previous) {
            previous->next = current->next;
            free(current);
        } else {
            current = current->next;
            free(head);
            head = current;
        }
    }
    return head;
}

int main(void) {
    linked_list_t *head = NULL;
    print_list(head);
    head = add_to_front(head, 1);
    head = add_to_front(head, 2);
    print_list(head);
    head = delete(head, 2);
    print_list(head);
}

和示例运行:

LIST:
LIST: 2 1
LIST: 1
© www.soinside.com 2019 - 2024. All rights reserved.