C中的单链表 - 节点删除问题

发布于 2024-10-03 23:46:01 字数 1682 浏览 3 评论 0原文

我正在用 C 实现一个单链表,但被删除节点函数困住了。 它删除该元素,链接其两个邻居,但后面的节点将下一个节点地址设置为 NULL。为什么? 有人可以帮忙吗?

  struct node{
        struct node* next;
        int value;
    };

    struct list{
        struct node* head;
        struct node* tail;
    };

void remove_node(struct list* plist, int value){

    struct node* current;
    struct node* temp;
    current = plist->head;
    if (!(current)) return; 
    if ( current->value == value ){
        if (!(current->next)){
            plist->head = NULL; plist->tail = NULL;
        }
        else { 
            plist->head = current->next;
            free(current);
        }
    }
    else {
        while(current->next){
            if(current->next->value==value){
                if ((current->next)->next){ 
                    temp = current->next;
                    current->next = (current->next)->next;
                    free(temp);
                }
                else{
                    temp = current->next;
                    plist->tail = current;      
                    current->next = NULL;
                    free(temp);
                    break;
                }
            }
        current = current->next;    
        }
    }
} 


Node current current->next
0 0x9f39018 0x9f39028
1 0x9f39028 0x9f39038
2 0x9f39038 0x9f39048
3 0x9f39048 0x9f39058
4 0x9f39058 0x9f39068
5 0x9f39068 0x9f39078
6 0x9f39078 0x9f39088
7 0x9f39088 0x9f39098
8 0x9f39098 0x9f390a8
9 0x9f390a8 (nil)

after remove(5)

0 0x9f39018 0x9f39028
1 0x9f39028 0x9f39038
2 0x9f39038 0x9f39048
3 0x9f39048 0x9f39058
4 0x9f39058 0x9f39078
6 0x9f39078 (nil)

I'm implementing a singly-linked list in C and got stuck with the remove node function.
It removes the element, links two of its neighbours, but the following node gets the next node address set to NULL. Why?
Can anybody help?

  struct node{
        struct node* next;
        int value;
    };

    struct list{
        struct node* head;
        struct node* tail;
    };

void remove_node(struct list* plist, int value){

    struct node* current;
    struct node* temp;
    current = plist->head;
    if (!(current)) return; 
    if ( current->value == value ){
        if (!(current->next)){
            plist->head = NULL; plist->tail = NULL;
        }
        else { 
            plist->head = current->next;
            free(current);
        }
    }
    else {
        while(current->next){
            if(current->next->value==value){
                if ((current->next)->next){ 
                    temp = current->next;
                    current->next = (current->next)->next;
                    free(temp);
                }
                else{
                    temp = current->next;
                    plist->tail = current;      
                    current->next = NULL;
                    free(temp);
                    break;
                }
            }
        current = current->next;    
        }
    }
} 


Node current current->next
0 0x9f39018 0x9f39028
1 0x9f39028 0x9f39038
2 0x9f39038 0x9f39048
3 0x9f39048 0x9f39058
4 0x9f39058 0x9f39068
5 0x9f39068 0x9f39078
6 0x9f39078 0x9f39088
7 0x9f39088 0x9f39098
8 0x9f39098 0x9f390a8
9 0x9f390a8 (nil)

after remove(5)

0 0x9f39018 0x9f39028
1 0x9f39028 0x9f39038
2 0x9f39038 0x9f39048
3 0x9f39048 0x9f39058
4 0x9f39058 0x9f39078
6 0x9f39078 (nil)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

¢蛋碎的人ぎ生 2024-10-10 23:46:01

此代码:

if ((current->next)->next){  
    current->next = (current->next)->next; 
    free(current->next); 

将下一个节点从列表中删除后释放它。换句话说,您释放了错误的节点。

This code:

if ((current->next)->next){  
    current->next = (current->next)->next; 
    free(current->next); 

frees the next node after you have already removed it from the list. In other words, you are freeing the wrong node.

遥远的绿洲 2024-10-10 23:46:01

请注意,您更正后的代码也会在空列表上崩溃。

Note that your corrected code will also crash on an empty list..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文