C语言向链表插入节点的函数
我正在尝试学习 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在此调用中,
指针
head
按值传递给函数。这意味着该函数处理指针值的副本。
更改函数内的副本不会反映指针的原始值。
您需要通过指针通过引用传递指针,或者从函数返回指针的新值并将其分配给原始指针。
此外,您还忘记将创建的节点的数据成员
next
设置为NULL
或更准确地说设置为head
。给你。
函数被称为类似
or
另一种方法是从函数返回指针头的新值。
例如,
在这种情况下,您需要谨慎使用该功能。
例如
In this call
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 toNULL
or more precisely tohead
.Here you are.
and the function is called like
or
Another approach is to return the new value of the pointer head from the function.
For example
In this case you need to use the function with caution.
For example
main
中的变量head
和insert
中的变量head
是两个不同的变量。当您在insert
中为该局部变量赋值时,它不会影响main
中的变量head
。您可以通过不同的方式解决这个问题。一种是将
head
的地址传递给函数,因此insert
实际上可以修改该地址处的内容:不是你的问题,而是你的问题还应该初始化
new_node
的next
成员:The variable
head
inmain
and the variablehead
ininsert
are two different variables. When ininsert
you assign something to that local variable, it does not affect the variablehead
inmain
.You can solve this in different ways. One is to pass the address of
head
to the function, soinsert
can actually modify what is at that address:Not your question, but you should also initialise the
next
member ofnew_node
: