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

无法使用void函数在C语言链表头部插入节点的问题

问题本质

这是C语言传值调用的典型问题:当你将head指针传递给insertAtBeginning时,函数接收到的是原指针的副本。函数内部执行head = newNode只是修改了这个副本的指向,完全不会影响main函数中原来的head指针,所以头部插入的新节点无法被外部感知。

解决方法

要修改原指针的指向,必须传递指针的指针(二级指针),让函数能直接操作原指针的内存地址。

修改insertAtBeginning函数:

void insertAtBeginning(struct Node **head, int data)
{
    struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
    newNode->data = data;
    newNode->next = *head; // 新节点指向原链表的头节点
    *head = newNode; // 更新原头指针指向新节点
}

在main中调用时,传入head的地址:

insertAtBeginning(&head, 20);
额外修复点

你的代码存在两处内存泄漏问题,顺便一起修复:

  • insertAtIndex和insertAtEnd中,无需为temp指针分配新内存,直接复用现有指针即可,否则每次调用都会产生一块未释放的内存:
    把struct Node *temp = (struct Node *)malloc(sizeof(struct Node));改为struct Node *temp = head;
  • insertAfterNode中声明的temp指针完全未使用,直接删除即可。

完整修复后的代码:

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

struct Node
{
    int data;
    struct Node *next;
};

void linkedListTraversal(struct Node *ptr)
{
    while (ptr != NULL)
    {
        printf("element: %d\n", ptr->data);
        ptr = ptr->next;
    }
}

void insertAtBeginning(struct Node **head, int data)
{
    struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));

    newNode->data = data;
    newNode->next = *head;

    *head = newNode;
}

void insertAtIndex(struct Node *head, int data, int index)
{
    struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
    struct Node *temp = head;
    int i = 0;
    while (i < index - 1)
    {
        temp = temp->next;
        i++;
    }
    newNode->data = data;
    newNode->next = temp->next;
    temp->next = newNode;
}

void insertAtEnd(struct Node *head, int data)
{
    struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
    struct Node *temp = head;
    while (temp->next != NULL)
    {
        temp = temp->next;
    }
    temp->next = newNode;
    newNode->data = data;
    newNode->next = NULL;
}

void insertAfterNode(struct Node *previous, int data)
{
    struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));

    newNode->data = data;

    newNode->next = previous->next;
    previous->next = newNode;
}

int main()
{
    struct Node *head;
    struct Node *second;
    struct Node *third;
    struct Node *fourth;

    // Allocate memory for the nodes of the linkedLists in the heap
    head = (struct Node *)malloc(sizeof(struct Node));
    second = (struct Node *)malloc(sizeof(struct Node));
    third = (struct Node *)malloc(sizeof(struct Node));
    fourth = (struct Node *)malloc(sizeof(struct Node));

    // Link first and second nodes
    head->data = 12;
    head->next = second;

    // Link second and third nodes
    second->data = 98;
    second->next = third;

    // Link third and fourth nodes
    third->data = 38;
    third->next = fourth;

    // Terminate the linkedList at the fourth node
    fourth->data = 37;
    fourth->next = NULL;

    printf("Linked list before insertion\n");
    linkedListTraversal(head);

    insertAtBeginning(&head, 20);

    // insertAtIndex(head, 22, 3);

    // insertAtEnd(head, 110);

    // insertAfterNode(second, 27);

    printf("Linked list after insertion\n");
    linkedListTraversal(head);

    return 0;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 13:55:17