C 中的链表

发布于 2024-10-15 10:11:23 字数 710 浏览 3 评论 0原文

我在使用双链表时遇到问题:我无法通过另一个节点从节点获取数据。这样:节点->上一个->上一个。但是node->prev就可以了。 有人知道为什么吗?

代码:

/*Add value - right side*/
void addListRight(doubleList *node, int value)
        {
        doubleList *newNode;
        newNode = createList();
        newNode->val = value;
        newNode->right = node->right;
        newNode->left = node;
        node->right->left = newNode; /*<-Error start here - 'segmentation error' or something like this*/
        node->right = newNode;
        }

用Google,我发现有些人把()这样写:(node->right)->right。我尝试过但结果是一样的。

使用 GCC/Ubuntu 10.10

*我想说“->”的正确词但我不知道英文。我的错..对不起!这个问题的标签也是如此!

I'm having problem with double linked lists: I can't get data from nodes throgh another node. This way: node->prev->prev. But node->prev is fine.
Anyone knows why?

The code:

/*Add value - right side*/
void addListRight(doubleList *node, int value)
        {
        doubleList *newNode;
        newNode = createList();
        newNode->val = value;
        newNode->right = node->right;
        newNode->left = node;
        node->right->left = newNode; /*<-Error start here - 'segmentation error' or something like this*/
        node->right = newNode;
        }

Using Google, I found that some guys put () like this: (node->right)->right. I tryed it but the result is the same.

Using GCC/Ubuntu 10.10

*I would like to say the proprer word for '->' but I don't know it in English. My bad.. sorry! Same about the tags of this question!

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

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

发布评论

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

评论(2

神爱温柔 2024-10-22 10:11:23

在使用这样的指针之前应该检查 NULL。列表的开头不会有上一个。

You should check for NULL before using a pointer like that. There will be no prev at the head of the list.

忘你却要生生世世 2024-10-22 10:11:23

您似乎收到了分段错误错误。这意味着您正在尝试访问无效内存。我的猜测是你还没有分配node->right或者它是NULL。确保所有指针均有效且已正确分配。

作为参考,下面是一个链表实现示例:

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

typedef struct doubleList doubleList;
struct doubleList
{
   int value;
   doubleList *left, *right;
};

doubleList *addListRight(doubleList *node, int value)
{
    doubleList *newNode;
    newNode = malloc(sizeof(doubleList));
    if(newNode == NULL)
    {
        return NULL;
    }
    newNode->value = value;
    newNode->left = NULL;
    newNode->right = NULL;
    if(node != NULL)
    {
        newNode->left = node;
        node->right = newNode;
    }
    return newNode;
}

int main(int argc, char **argv)
{
    doubleList *list = addListRight(NULL, 5);
    addListRight(list, 2);

    // Outputs: 5, 2
    printf("%d, %d", list->value, list->right->value);

    return 0;
}

It looks like you're getting a segmentation fault error. This means you are trying to access invalid memory. My guess is that you haven't allocated node->right or it's NULL. Make sure all your pointers are valid and have been allocated properly.

For reference, here is an example linked list implementation:

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

typedef struct doubleList doubleList;
struct doubleList
{
   int value;
   doubleList *left, *right;
};

doubleList *addListRight(doubleList *node, int value)
{
    doubleList *newNode;
    newNode = malloc(sizeof(doubleList));
    if(newNode == NULL)
    {
        return NULL;
    }
    newNode->value = value;
    newNode->left = NULL;
    newNode->right = NULL;
    if(node != NULL)
    {
        newNode->left = node;
        node->right = newNode;
    }
    return newNode;
}

int main(int argc, char **argv)
{
    doubleList *list = addListRight(NULL, 5);
    addListRight(list, 2);

    // Outputs: 5, 2
    printf("%d, %d", list->value, list->right->value);

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