具有递归和多个类的河内C ++塔

时间:2018-10-14 02:52:06

标签: c++ class pointers recursion towers-of-hanoi

我一直收到错误消息“抛出异常:读取访问冲突。这是nullptr。”我知道这意味着我正在尝试访问不属于我的内存,但是我不知道如何解决它。我一直在互联网上寻找如何修复它的方法,但没有发现任何问题。请帮忙。 Node类与Stack类进行对话,并且在我尝试运行该类时发生错误 代码cout << "New Stack in reverse: "; cookie.displayStackReverse();.

Hanoi.cpp文件

int main()
{
    {
        //Testing the Node Class
        Node pet(123, (Node*)0x12345678);
        cout << "Payload: " << pet.getPayload() << " should be (123)" << endl;
        cout << "Next: " << pet.getNext() << " should be (12345678)" << endl;
        pet.setPayload(456);
        pet.setNext((Node*)0x87654321);
        cout << "Payload: " << pet.getPayload() << " should be (456)" << endl;
        cout << "Next: " << pet.getNext() << " should be (87654321)" << endl;
    }
    {
        // Testing the Stack Class
        Stack cookie;
        cookie.push(123);
        cookie.push(456);
        cout << "Stack so far: ";  cookie.displayStack(); cout << " should be 456123" << endl;
        cout << "Removing top node " << cookie.readTop() << endl;
        cout << "Top node " << cookie.pop() << " removed.";
        cout << "The stack is now: "; cookie.displayStack(); cout << ", should be 123" << endl;
        cookie.push(789);
        cout << "New Stack in reverse: "; cookie.displayStackReverse(); cout << ", should be 123 789" << endl;
        cout << "Stack has " << cookie.getNumNodes() << " elements in the stack (should be 2)" << endl;
    }
}

Node.h文件

#pragma once

class Node
{
private:
    int payload;
    Node *next;

public:

    //Constructor
    Node(int newPayload, Node *newNext);

    //Accerrsors - Retrieve the contents of a protected data member
    int getPayload() const;
    Node *getNext() const;

    //Mutators - Change the contents of a protected data member
    void setPayload(int newPayload);
    void setNext(Node* next);

    //Default destructor
    ~Node();
};

Node.cpp文件

#include "Node.h"

Node::Node(int newPayload, Node *newNext)
{
    //Set the payload
    setPayload(newPayload);

    //Set the next Node
    setNext(newNext);
}

int Node::getPayload() const
{
    return payload;
}

Node* Node::getNext() const
{
    return next;
}

void Node::setPayload(int newPayload)
{
    payload = newPayload;
}

void Node::setNext(Node* newNext)
{
    next = newNext;
}

Node::~Node()
{
}

Stack.h文件

#pragma once
#include "Node.h"


class Stack
{
private:
    Node * top;
    int numNodes;
public:
    //Constructor
    Stack();

    //Push and pop methods
    void push(int newPayload);
    int pop();

    //Top Asscessor
    int readTop() const;

    //Number of nodes in the stack accessor
    int getNumNodes() const;

    //Display the stack
    void displayStack();
    void displayReverse(Node* temp);
    void displayStackReverse();

    //Get rid of all of the nodes on the stack
    void clearStack();
    ~Stack();
};

Stack.cpp文件

#include "Stack.h"
#include "Node.h"
#include <cassert>
#include <iostream>

using namespace std;

Stack::Stack()
{
}

void Stack::push(int newPayload)
{
    top = new Node(newPayload, top);
    numNodes++;
}

int Stack::pop()
{
    assert(numNodes > 0);

    int x = top->getPayload();
    Node* tempPtr = top;
    top = top->getNext();
    numNodes--;
    delete tempPtr;
    return x;
}

int Stack::readTop() const
{
    return top->getPayload();
}

int Stack::getNumNodes() const
{
    return numNodes;
}

void Stack::displayStack()
{
    Node* temp = top;
    while (temp != nullptr)
    {
        cout << temp->getPayload();
        temp = temp->getNext();
    }
}

void Stack::displayReverse(Node* node)
{
    if (node != nullptr)
    {
        displayReverse(node->getNext());
    }
    else
    {
        cout << node->getPayload();
    }
}

void Stack::displayStackReverse()
{
    displayReverse(top);
}

void Stack::clearStack()
{
    delete top;
}

Stack::~Stack()
{

}

1 个答案:

答案 0 :(得分:0)

我知道了。这是正确的方法。

void Stack::displayReverse(Node* node)
{
    if (node != nullptr)
    {
        displayReverse(node->getNext());
        cout << node->getPayload();
    }
}