c - 如何从链表中删除节点

标签 c struct linked-list singly-linked-list

我想在两个节点之间进行二元运算,将结果存储在一个节点中并消除另一个节点。这是我写的:

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

    struct n{
        double value;
        char op;
        struct n *next;
    };

    void delete(struct n *head);
    void add_item(struct n **ptr, double *data);

    int main(){
        struct n *head = NULL;
        double result;
        add_item(&head, 5);
        add_item(&head, 3);
        head->op = '*';
        result = (head->next)->value * head->value;
        (head->next)->value = result;
        delete(head);
        printf("%lf\n",head->value);
        free(head); 
        return 0;        
    }

    void add_item(struct n **ptr, double *data)
    {
            struct n *item = malloc(sizeof *item);

            item->value = *data;
            item->next = *ptr;
            item->op = '?';
            *ptr = item;
    }

    void delete(struct n *head)
    {
        struct n *temp;
        temp = head->next;
        head->next = temp->next;
        free(temp);
    }

在这个例子中,我有一个像这样的列表 3 -> 5 -> Null。我想要得到这个 15 -> NUll。 当我尝试打印剩余节点的值时,我得到 3 而不是 15

最佳答案

这两个函数都无效。

对于 add_item 函数,您不通过引用传递数据(并且通过引用传递数据没有意义)。

    add_item(&head, 5);
    add_item(&head, 3);

所以函数应该像这样声明和定义

void add_item(struct n **ptr, double data)
{
        struct n *item = malloc(sizeof *item);

        item->value = data;
        item->next = *ptr;
        item->op = '?';
        *ptr = item;
}

您还必须通过引用将头节点传递给函数 delete

void delete(struct n **head)
{
    if ( *head )
    {
        struct n *temp = *head;
        *head = ( *head )->next;
        free( temp );
    }
}

并称它为

delete( &head );

When I try to print the value of the node remaining I get 3 instead of 15

因为你在head之后的节点写的操作结果,你删除了head之后的节点,而不是删除了head节点。

(head->next)->value = result;

这是你更新的程序

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

    struct n{
        double value;
        char op;
        struct n *next;
    };

    void delete(struct n **head);
    void add_item(struct n **ptr, double data);

    int main(){
        struct n *head = NULL;
        double result;
        add_item(&head, 5);
        add_item(&head, 3);
        head->op = '*';
        result = (head->next)->value * head->value;
        (head->next)->value = result;
        delete(&head);
        printf("%lf\n",head->value);
        free(head); 
        return 0;        
    }

void add_item(struct n **ptr, double data)
{
        struct n *item = malloc(sizeof *item);

        item->value = data;
        item->next = *ptr;
        item->op = '?';
        *ptr = item;
}

void delete(struct n **head)
{
    if ( *head )
    {
        struct n *temp = *head;
        *head = ( *head )->next;
        free( temp );
    }
}

它的输出是

15.000000

关于c - 如何从链表中删除节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58642396/

相关文章:

C++ 从链表中删除节点

algorithm - 如何连接循环双向链表

c++ - 如何使用 ffmpeg 以便不需要将其 dll 包含在您的应用程序文件夹中?

c - c 中的文件处理未产生所需的结果

c - 如何在 C 中创建具有两个可变大小数组的结构

c - 初始化复合文字内的变量

c - 在 C 中修剪 int 数组以进行桶排序

c - 管道问题 : first child writes too much in to pipe

c++ - 在 C++ 中访问通用结构的成员会出错

C - 使用文件处理和链表登录