我有一个问题创建链接列表
#include<stdio.h>
#include<stdlib.h>
void insert_front(struct node* head, int block_number);
void insert_rear(struct node* head, int block_number);
void print_list(struct node* head);
struct node {
int block_number;
struct node* next;
};
int main(void)
{
struct node* list = NULL;
insert_front(list, 10);
insert_rear(list, 20);
insert_front(list, 30);
insert_rear(list, 40);
print_list(list);
return 0;
}
void insert_front(struct node* head, int block_number)
{
struct node* p = malloc(sizeof(struct node));
p->block_number = block_number;
p->next = head;
head = p;
return head;
}
void insert_rear(struct node* head, int block_number)
{
struct node* p = malloc(sizeof(struct node));
p->block_number = block_number;
p->next = NULL;
if (head == NULL) {
head = p;
}
else {
struct node* q = head;
while (q->next != NULL) {
q = q->next;
}
q->next = p;
}
}
void print_list(struct node* head)
{
struct node* p = head;
while (p != NULL) {
printf("--> %d ", p->block_number);
p = p->next;
}
printf("\n");
}
当我运行它时,根本没有结果。
现在,在insert_front函数p-&gt; block_number = block_number中,出现一条消息,说null指针'p'正在被删除...(在insert_rear函数中出现了同一件事。)
可能是我声明我是声明的。指针错误?
#include<stdio.h>
#include<stdlib.h>
void insert_front(struct node* head, int block_number);
void insert_rear(struct node* head, int block_number);
void print_list(struct node* head);
struct node {
int block_number;
struct node* next;
};
int main(void)
{
struct node* list = NULL;
insert_front(list, 10);
insert_rear(list, 20);
insert_front(list, 30);
insert_rear(list, 40);
print_list(list);
return 0;
}
void insert_front(struct node* head, int block_number)
{
struct node* p = malloc(sizeof(struct node));
p->block_number = block_number;
p->next = head;
head = p;
return head;
}
void insert_rear(struct node* head, int block_number)
{
struct node* p = malloc(sizeof(struct node));
p->block_number = block_number;
p->next = NULL;
if (head == NULL) {
head = p;
}
else {
struct node* q = head;
while (q->next != NULL) {
q = q->next;
}
q->next = p;
}
}
void print_list(struct node* head)
{
struct node* p = head;
while (p != NULL) {
printf("--> %d ", p->block_number);
p = p->next;
}
printf("\n");
}
When I ran it, there was no result at all.
Now, in the insert_front function p->block_number = block_number, a message appears saying that the NULL pointer 'p' is being dereferenced... (The same thing appears in the insert_rear function.)
Could it be that I am declaring the pointer wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
insert_front
和insert_rear
都需要将头部修改转交回呼叫者,呼叫者需要收获该信息。两者都应宣布返回struct节点 *
,这样做,并且main
中的代码相应地做出了反应。例如:输出
我离开内存泄漏供您解决。
Both
insert_front
andinsert_rear
need to convey possibly head modification back to the caller, and the caller needs to reap that information. Both should be declared to returnstruct node *
, do so, and the code inmain
react accordingly. E.g.:Output
I leave the memory leaks for you to resolve.
在c中,所有变量均通过value 传递 - 如果您通过指针,则它是复制的(当然不是对象,当然是...),并且功能参数除了从外部初始化外,只不过是局部变量。因此,通过
head = p;
您只需分配外部指针的本地复制,而不是后者本身!为了解决您有两个选择:
使用第二种方法,用户不能忘记重新分配(潜在的)新头部,所以这就是我要使用的:
in
insert_front
drop retoct return; ,a使用void
函数无法返回任何具体的东西,并且根本不需要返回
(但是裸露return;
can be be用于过早退出功能)。In C all variables are passed by value – if you pass a pointer, then it is copied, too (not the pointed to object, of course...), and function parameters, apart from being initialised from outside, are nothing more than local variables. Thus via
head = p;
you just assign the local copy of the outside pointer, not the latter itself!To fix that you have two options:
With second approach a user cannot forget to re-assign the (potentially) new head, so that's what I'd go with:
And in
insert_front
dropreturn head;
, a function withvoid
cannot return anything concrete and does not require areturn
at all (but barereturn;
can be used to exit a function prematurely).