双链表;新手尝试

发布于 2024-11-05 00:40:30 字数 1763 浏览 0 评论 0原文

我刚刚开始学习C,并且(似乎)到目前为止,大多数内容都在点击。但是,我在尝试使用双链表来追踪问题时遇到了一些麻烦。当我尝试构建/运行此代码时,我不断收到seg-fault。我正在使用 Cygwin 通过 NetBeans 提供的 gcc 进行编译。

我讨厌只转储一段代码并说“帮助”,但我现在不知道还有哪些其他相关细节,因此如有必要,请随时询问详细信息:

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

struct node_t{
    struct node_t *prev;
    struct node_t *next;
};

struct list_t{
    struct node_t *head;
    struct node_t *tail;
    int length;
};

struct node_t *new_node(void);
struct list_t *new_list(void);
int append_list_node(struct list_t *list, struct node_t *node);

int main(void) {

    int i = 0, length = 0;
    struct node_t *node;
    struct list_t *list = new_list();

    for(i = 0; i < 10; i++){
        length = append_list_node(list, new_node());
        printf("%d", length);
    }

    return 0;

}

struct node_t *new_node(void){
    struct node_t *node = malloc(sizeof(struct node_t));
    return node;
}

struct list_t *new_list(void){
    struct list_t *list = malloc(sizeof(struct list_t));
    list->length = 0;
    return list;
}

int append_list_node(struct list_t *list, struct node_t *new_node){
    if(list->head == NULL){
        list->head          = new_node; // edited
        new_node->prev      = NULL;
    }else{
        list->tail->next    = new_node;
        new_node->prev      = list->tail;
    }
    return (++list->length);
}

感谢大家的反应超级快,所有答案都是正确的。当我简要查看 F5-ing 之间的代码时,我意识到我没有设置 tail,因此我决定更改标记为 edited< 的行/code> 如下:

list->head = list->tail = new_node;

我还将决定使用 calloc() 但是,我读到频繁使用它可能会导致执行时间的巨大成本,因为它正在清除和分配。想法?

I've just started out learning C, and (seemingly) so far most stuff is clicking. However, I'm having some trouble tracking down an issue with an attempt at a double linked list. I keep getting a seg-fault when I attempt to build/run this code. I'm compiling with the Cygwin supplied gcc via NetBeans.

I hate to just dump a block of code and say "help", but I don't know what other details are pertinent at this time, so feel free to ask for details if necessary:

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

struct node_t{
    struct node_t *prev;
    struct node_t *next;
};

struct list_t{
    struct node_t *head;
    struct node_t *tail;
    int length;
};

struct node_t *new_node(void);
struct list_t *new_list(void);
int append_list_node(struct list_t *list, struct node_t *node);

int main(void) {

    int i = 0, length = 0;
    struct node_t *node;
    struct list_t *list = new_list();

    for(i = 0; i < 10; i++){
        length = append_list_node(list, new_node());
        printf("%d", length);
    }

    return 0;

}

struct node_t *new_node(void){
    struct node_t *node = malloc(sizeof(struct node_t));
    return node;
}

struct list_t *new_list(void){
    struct list_t *list = malloc(sizeof(struct list_t));
    list->length = 0;
    return list;
}

int append_list_node(struct list_t *list, struct node_t *new_node){
    if(list->head == NULL){
        list->head          = new_node; // edited
        new_node->prev      = NULL;
    }else{
        list->tail->next    = new_node;
        new_node->prev      = list->tail;
    }
    return (++list->length);
}

Thanks for the super quick responses everyone, all the answers are correct. As I was briefly looking over the code between F5-ing, I realized I wasn't setting the tail, so I resolved to change the line marked edited as follows:

list->head = list->tail = new_node;

I'll also resolve to use calloc() however, I've read that frequent use of it can cause considerable costs to execution time since it's clearing and allocating. Thoughts?

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

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

发布评论

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

评论(3

笔落惊风雨 2024-11-12 00:40:30

使用calloc()分配内存。 malloc() 函数不会将内存初始化为零(因此指针将设置为 NULL)。您假设指针默认为 NULL。

Use calloc() to allocate memory. The malloc() function does not initialize the memory to zeros (so the pointers will be set to NULL). You are making the assumption that the pointers are NULL by default.

梦中的蝴蝶 2024-11-12 00:40:30

C 不会为你做任何初始化。所以当你这样做时:

struct list_t *new_list(void){
    struct list_t *list = malloc(sizeof(struct list_t));
    list->length = 0;
    return list;
}

list->head 可以是任何东西......并且可能不会是 NULL。

C doesn't do any initialization for you. So when you do:

struct list_t *new_list(void){
    struct list_t *list = malloc(sizeof(struct list_t));
    list->length = 0;
    return list;
}

list->head can be anything.. and probably won't be NULL.

胡大本事 2024-11-12 00:40:30

一个问题是,您永远不会将 list->tail 设置为 anything,然后在 list->head 不为 NULL 时尝试访问 list->tail->next(如其他人已经指出,无论如何都不能保证。)

One issue is that you never set list->tail to anything, and then attempt to access list->tail->next if list->head isn't NULL (which, as other have pointed out, isn't guaranteed anyway.)

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