C语言向链表插入节点的函数

发布于 2025-01-16 20:21:17 字数 859 浏览 4 评论 0原文

我正在尝试学习 C 语言的数据结构,但我被困在我制作的第一个函数上。 如果我运行这个,什么也不会发生。
我没有收到任何错误,但程序不打印任何内容。


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

typedef struct node{
    int data;
    struct node *next;
}node;

void insert(int data, node *head){
    node *new_node = malloc(sizeof(node));
    new_node->data = data;
    head = new_node;
}

int main(){
    node *head = NULL;
    insert(8, head);
    printf("head.data: %d\n", head->data);
}

但是,如果我将函数插入中的代码放入主函数中,它就可以工作。

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

typedef struct node{
    int data;
    struct node *next;
}node;

int main(){

    node *head = NULL;
    node *new_node = malloc(sizeof(node));
    new_node->data = 5;
    head = new_node;
    printf("head.data: %d\n", head->data);
}

我是否不知道如何使用 C 中的函数或者我的第一个代码有什么问题?

I'm trying to learn data structures in C and I'm stuck at the first function I made.
If I run this nothing happens.
I get no errors but the program doesn't print anything.


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

typedef struct node{
    int data;
    struct node *next;
}node;

void insert(int data, node *head){
    node *new_node = malloc(sizeof(node));
    new_node->data = data;
    head = new_node;
}

int main(){
    node *head = NULL;
    insert(8, head);
    printf("head.data: %d\n", head->data);
}

But if I put the code from the function insert in the main function it works.

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

typedef struct node{
    int data;
    struct node *next;
}node;

int main(){

    node *head = NULL;
    node *new_node = malloc(sizeof(node));
    new_node->data = 5;
    head = new_node;
    printf("head.data: %d\n", head->data);
}

Do I not know how to use functions in C or what is the problem with my first code?

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

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

发布评论

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

评论(2

几度春秋 2025-01-23 20:21:17

在此调用中,

insert(8, head);

指针 head 按值传递给函数。

这意味着该函数处理指针值的副本。

更改函数内的副本不会反映指针的原始值。

您需要通过指针通过引用传递指针,或者从函数返回指针的新值并将其分配给原始指针。

此外,您还忘记将创建的节点的数据成员 next 设置为 NULL 或更准确地说设置为 head

给你。

int insert( node **head, int data )
{
    node *new_node = malloc( sizeof( node ) );
    int success = new_node != NULL;

    if ( success )
    {
        new_node->data = data;
        new_node->next = *head;
        *head = new_node;
    }

    return success;
}

函数被称为类似

insert( &head, 8 );

or

if ( !insert( &head, 8 ) )
{
    puts( "Error: not enough memory." );
}

另一种方法是从函数返回指针头的新值。

例如,

node * insert( node *head, int data )
{
    node *new_node = malloc( sizeof( node ) );
    
    if ( new_node != NULL )
    {
        new_node->data = data;
        new_node->next = head;
    }

    return new_node;
} 

在这种情况下,您需要谨慎使用该功能。

例如

node *tmp = insert( head, 8 );
if ( tmp != NULL ) 
{
    head = tmp;
}
else
{
    puts( "Error: not enough memory." );
}

In this call

insert(8, head);

the pointer head is passed to the function by value.

It means that the function deals with a copy of the value of the pointer.

Changing the copy within the function does not reflect on the original value of the pointer.

Either you need to pass the pointer by reference through a pointer to it or to return the new value of the pointer from the function and to assign it to the original pointer.

Also you forgot to set the data member next of the created node to NULL or more precisely to head.

Here you are.

int insert( node **head, int data )
{
    node *new_node = malloc( sizeof( node ) );
    int success = new_node != NULL;

    if ( success )
    {
        new_node->data = data;
        new_node->next = *head;
        *head = new_node;
    }

    return success;
}

and the function is called like

insert( &head, 8 );

or

if ( !insert( &head, 8 ) )
{
    puts( "Error: not enough memory." );
}

Another approach is to return the new value of the pointer head from the function.

For example

node * insert( node *head, int data )
{
    node *new_node = malloc( sizeof( node ) );
    
    if ( new_node != NULL )
    {
        new_node->data = data;
        new_node->next = head;
    }

    return new_node;
} 

In this case you need to use the function with caution.

For example

node *tmp = insert( head, 8 );
if ( tmp != NULL ) 
{
    head = tmp;
}
else
{
    puts( "Error: not enough memory." );
}
迷途知返 2025-01-23 20:21:17

main 中的变量 headinsert 中的变量 head 是两个不同的变量。当您在 insert 中为该局部变量赋值时,它不会影响 main 中的变量 head

您可以通过不同的方式解决这个问题。一种是将 head地址传递给函数,因此 insert 实际上可以修改该地址处的内容:

不是你的问题,而是你的问题还应该初始化 new_nodenext 成员:

void insert(int data, node **headPtr){
    node *new_node = malloc(sizeof(node));
    new_node->data = data;
    new_node->next = *headPtr;
    *headPtr = new_node;
}

int main(){
    node *head = NULL;
    insert(8, &head);
    printf("head.data: %d\n", head->data);
}

The variable head in main and the variable head in insert are two different variables. When in insert you assign something to that local variable, it does not affect the variable head in main.

You can solve this in different ways. One is to pass the address of head to the function, so insert can actually modify what is at that address:

Not your question, but you should also initialise the next member of new_node:

void insert(int data, node **headPtr){
    node *new_node = malloc(sizeof(node));
    new_node->data = data;
    new_node->next = *headPtr;
    *headPtr = new_node;
}

int main(){
    node *head = NULL;
    insert(8, &head);
    printf("head.data: %d\n", head->data);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文