通用链表指针访问

发布于 2024-12-07 13:42:16 字数 1993 浏览 7 评论 0原文

我正在使用模板在 C++ 中编写通用链表,并且在访问节点值时遇到分段错误。

为了使测试用例更简单,我实现了一个固定大小的两个节点的链表。

我有两个问题:

1a)为什么 aList.headNodePtr->prevNodePtr 不设置为 NULL?

1b) 为什么 aList.tailNodePtr->nextNodePtr 不设置为 NULL?

我在 LinkedList 构造函数中将这两个值设置为 NULL,但 main 中的输出显示:

head prevAddress: 0x89485ed18949ed31
tail nextAddress: 0x7fffe8849679

2) 为什么 main() 中的以下行会导致段错误?

aList.headNodePtr->nodeValue = 1;

完整代码如下:

#include <iostream>
using namespace std;

template <class T>
    class Node {
    public:
    Node<T>* prevNodePtr;
    Node<T>* nextNodePtr;
    T nodeValue;
};

template <typename T>
    class LinkedList {
    public:
    Node<T>* headNodePtr;
    Node<T>* tailNodePtr;

    LinkedList() {
        Node<T>* headNodePtr = new Node<T>;
        Node<T>* tailNodePtr = new Node<T>;

        headNodePtr->prevNodePtr = NULL;
        headNodePtr->nextNodePtr = tailNodePtr;
        tailNodePtr->prevNodePtr = headNodePtr;
        tailNodePtr->nextNodePtr = NULL;
    }

    ~LinkedList() {
        headNodePtr = NULL;
        tailNodePtr = NULL;
        delete headNodePtr;
        delete tailNodePtr;
    }
};

int main()
{
    LinkedList<int> aList;
    cout << "head Value: " << aList.headNodePtr->nodeValue << endl;
    cout << "head prevAddress: " << aList.headNodePtr->prevNodePtr << endl;
    cout << "head nextAddress: " << aList.headNodePtr->nextNodePtr << endl;
    cout << "tail Value: " << aList.tailNodePtr->nodeValue << endl;
    cout << "tail prevAddress: " << aList.tailNodePtr->prevNodePtr << endl;
    cout << "tail nextAddress: " << aList.tailNodePtr->nextNodePtr << endl;

    aList.headNodePtr->nodeValue = 1;
}

I am writing a generic linked list in C++ using templates, and am experiencing Segmentation Faults when accessing Node values.

To make the test case simpler, I have implemented a fixed size, two node, linked list.

I have two questions:

1a) Why isn't aList.headNodePtr->prevNodePtr set to NULL?

1b) Why isn't aList.tailNodePtr->nextNodePtr set to NULL?

I set both of these values to NULL in the LinkedList constructor, but the output in main shows that:

head prevAddress: 0x89485ed18949ed31
tail nextAddress: 0x7fffe8849679

2) Why does the following line in main() cause a Seg Fault?

aList.headNodePtr->nodeValue = 1;

The full code is below:

#include <iostream>
using namespace std;

template <class T>
    class Node {
    public:
    Node<T>* prevNodePtr;
    Node<T>* nextNodePtr;
    T nodeValue;
};

template <typename T>
    class LinkedList {
    public:
    Node<T>* headNodePtr;
    Node<T>* tailNodePtr;

    LinkedList() {
        Node<T>* headNodePtr = new Node<T>;
        Node<T>* tailNodePtr = new Node<T>;

        headNodePtr->prevNodePtr = NULL;
        headNodePtr->nextNodePtr = tailNodePtr;
        tailNodePtr->prevNodePtr = headNodePtr;
        tailNodePtr->nextNodePtr = NULL;
    }

    ~LinkedList() {
        headNodePtr = NULL;
        tailNodePtr = NULL;
        delete headNodePtr;
        delete tailNodePtr;
    }
};

int main()
{
    LinkedList<int> aList;
    cout << "head Value: " << aList.headNodePtr->nodeValue << endl;
    cout << "head prevAddress: " << aList.headNodePtr->prevNodePtr << endl;
    cout << "head nextAddress: " << aList.headNodePtr->nextNodePtr << endl;
    cout << "tail Value: " << aList.tailNodePtr->nodeValue << endl;
    cout << "tail prevAddress: " << aList.tailNodePtr->prevNodePtr << endl;
    cout << "tail nextAddress: " << aList.tailNodePtr->nextNodePtr << endl;

    aList.headNodePtr->nodeValue = 1;
}

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

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

发布评论

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

评论(1

静谧 2024-12-14 13:42:16

您实际上并没有设置成员,而是设置您在构造函数中声明的局部变量:

Node<T>* headNodePtr;  // <-- MEMBERS
Node<T>* tailNodePtr;

LinkedList() {
    Node<T>* headNodePtr = new Node<T>;  // <-- LOCALS
    Node<T>* tailNodePtr = new Node<T>;

请尝试以下操作:

Node<T>* headNodePtr;  // <-- MEMBERS
Node<T>* tailNodePtr;

LinkedList() {
    headNodePtr = new Node<T>;  // <-- MEMBER ACCESS
    tailNodePtr = new Node<T>;

You're not actually setting the members, you're setting the locals you declared in the ctor:

Node<T>* headNodePtr;  // <-- MEMBERS
Node<T>* tailNodePtr;

LinkedList() {
    Node<T>* headNodePtr = new Node<T>;  // <-- LOCALS
    Node<T>* tailNodePtr = new Node<T>;

Try this instead:

Node<T>* headNodePtr;  // <-- MEMBERS
Node<T>* tailNodePtr;

LinkedList() {
    headNodePtr = new Node<T>;  // <-- MEMBER ACCESS
    tailNodePtr = new Node<T>;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文