我有一个问题创建链接列表

发布于 2025-01-26 22:28:17 字数 1401 浏览 4 评论 0原文

#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 技术交流群。

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

发布评论

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

评论(2

因为看清所以看轻 2025-02-02 22:28:17

insert_frontinsert_rear都需要将头部修改转交回呼叫者,呼叫者需要收获该信息。两者都应宣布返回struct节点 *,这样做,并且main中的代码相应地做出了反应。例如:

#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>

struct node * insert_front(struct node *head, int block_number);
struct node * 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;

    list = insert_front(list, 10);
    list = insert_rear(list, 20);
    list = insert_front(list, 30);
    list = insert_rear(list, 40);

    print_list(list);

    return 0;
}

struct node *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;
}

struct node *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;
    }
    return head;
}

void print_list(struct node *head)
{
    struct node *p = head;
    while (p != NULL)
    {
        printf("--> %d ", p->block_number);
        p = p->next;
    }
    printf("\n");
}

输出

--> 30 --> 10 --> 20 --> 40 

我离开内存泄漏供您解决。

Both insert_front and insert_rear need to convey possibly head modification back to the caller, and the caller needs to reap that information. Both should be declared to return struct node *, do so, and the code in main react accordingly. E.g.:

#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>

struct node * insert_front(struct node *head, int block_number);
struct node * 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;

    list = insert_front(list, 10);
    list = insert_rear(list, 20);
    list = insert_front(list, 30);
    list = insert_rear(list, 40);

    print_list(list);

    return 0;
}

struct node *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;
}

struct node *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;
    }
    return head;
}

void print_list(struct node *head)
{
    struct node *p = head;
    while (p != NULL)
    {
        printf("--> %d ", p->block_number);
        p = p->next;
    }
    printf("\n");
}

Output

--> 30 --> 10 --> 20 --> 40 

I leave the memory leaks for you to resolve.

把回忆走一遍 2025-02-02 22:28:17

在c中,所有变量均通过value 传递 - 如果您通过指针,则它是复制的(当然不是对象,当然是...),并且功能参数除了从外部初始化外,只不过是局部变量。因此,通过head = p;您只需分配外部指针的本地复制,而不是后者本身!

为了解决您有两个选择:

  1. 返回新的头部,并使用户负责将返回的值重新分配给他自己的头指针。
  2. 接受头作为指针指针。

使用第二种方法,用户不能忘记重新分配(潜在的)新头部,所以这就是我要使用的:

void insert_whichEver(node** head, int block_number)
{
    // use `*head` where you had `head` before...
} 

void demo()
{
   node* head = NULL;
   insert_front(&head, 1012);
}

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:

  1. Return the new head and make the user responsible for re-assigning the returned value to his own head pointer.
  2. Accept the head as pointer to pointer.

With second approach a user cannot forget to re-assign the (potentially) new head, so that's what I'd go with:

void insert_whichEver(node** head, int block_number)
{
    // use `*head` where you had `head` before...
} 

void demo()
{
   node* head = NULL;
   insert_front(&head, 1012);
}

And in insert_front drop return head;, a function with void cannot return anything concrete and does not require a return at all (but bare return; can be used to exit a function prematurely).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文