c++ - 无法分配给链表中的对象

标签 c++ c++11

Head 和 tail 正在填充,并打印出值,但 nodePtr 出于某种原因保持为空。当我在 VS2015 中调试时,头部和尾部编号被填充,而该字段保持为空

这是 Linked_List

#ifndef _LINKED_LIST_
#define _LINKED_LIST_

#include <iostream>

class LinkedList
{
public:

    struct Node
    {
        int number;
        Node * next;
        Node() : number(NULL), next(NULL) {};
        Node(int number_, Node * next_ = NULL)
        {
            number = number_;
            next = next_;
        }
    }*head, *tail, *nodePtr;


LinkedList();
~LinkedList();

void add(int num);

friend std::ostream& operator<<(std::ostream& out, LinkedList& list);

private:
    int size;
};

#endif // _LINKED_LIST_

执行文件

include "linkedlist.h"
#include <iostream>
using namespace std;

LinkedList::LinkedList() : head(NULL), tail(NULL), nodePtr(NULL) 
{
    nodePtr = new Node();
}

LinkedList::~LinkedList()
{
    Node * curr, *temp;
    curr = head;
    temp = head;

    while (curr != NULL)
    {
        curr = curr->next;
        delete temp;
        temp = curr;
    }
}


void LinkedList::add(int num)
{
    Node * newNode = new Node();
    newNode->number = num;
    cout << newNode->number;
    if (head == NULL)
    {
        head = newNode;
        tail = newNode;
        size++;
    }
    else
    {
        tail->next = newNode;
        newNode->next = NULL;
        tail = newNode;
        size++;
    }
    //cout << nodePtr->number; //empty, or some random
    //just some tests
    cout << head->number;
    if (head->next != NULL)
    {
        cout << head->next->number;
    }
    cout << tail->number;
    cout << endl;
}

std::ostream & operator<<(std::ostream & out, LinkedList & list)
{
    out << list.nodePtr->number << endl;
    return out;
}

main.cpp

#include <iostream>
#include "linkedlist.h"

using namespace std;

int main()
{
    int num;
    LinkedList list;

    list.add(1);
    list.add(2);
    list.add(3);
    cout << list;


    cout << "Press 1: ";
    cin >> num;
    return 0;
}

最佳答案

您在这里缺少一个基本概念。 nodePtr 不是某个神奇的节点,它知道所有其他节点,或者知道链表,或者可以用来打印它们的所有数字。

当你这样做时:

out << list.nodePtr->number << endl;

您所做的只是输出您在分配新的 Node 并将指针存储在 nodePtr 中时初始化的值:

nodePtr = new Node();

调用 Node 的默认构造函数,将 nodePtr->number 设置为零。 (旁注,您将其初始化为 NULL,而不是 0——您不应将整数类型与指针类型混合,因此将其更改为将值初始化为 0)。

它的值保持为 0,因为您永远不会修改它。并且 nodePtr 始终指向该单个节点,因为您从未修改过 nodePtr

您真正想要做的是打印出您的列表。让我建议执行此操作的常规方法,从 head 开始并遵循节点链接:

std::ostream & operator<<(std::ostream & out, const LinkedList & list)
{
    for( Node *node = list.head; node != nullptr; node = node->next )
    {
        out << node->number << std::endl;
    }
    return out;
}

最后,我建议您从类中完全删除 nodePtr

关于c++ - 无法分配给链表中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43358678/

相关文章:

c++ - remove_if 的惯用 C++

c++ - 如何在 ROOT (CERN) 中禁用屏幕输出

c++ - 为什么 std::unordered_set 不将 CComBSTR 类型作为键?

C++ 类变量作用域

c++ - 通过公共(public)继承仅公开某些方法

c++ - 将参数移动到 std::thread 中?

c++ - 我可以将函数指针 void* 转换为 std::function 吗?

c++ - g++ 编译器如何包含 boost::regex

c++ - C++ 中的全卷积网络训练

c++ - 使用无效方法实例化类模板