Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Assignment: Using your Hanoi and Peg code from Program #2, do the following: Replace the use of in your Peg class with a Stack and

Assignment:

Using your Hanoi and Peg code from Program #2, do the following:

Replace the use of in your Peg class with a Stack and Node class that you write.

Your Node class should consist of an element of type int and a pointer to the Node class. It should have appropriate constructors, a destructor, accessors and mutators.

Your Stack class should implement a stack data type that holds Node objects which are dynamically allocated up on creation. It should have a constructor and methods for push, pop, reading the top element and the number of elements in the stack. Memory should be deallocated upon pop and destruction of the stack object.

When complete, your hanoi program should run as described in Program #1.

Create a .zip file of the program directory and post the .zip file to Canvas.

I don't understand why my code is failing with no errors.

stack.H

#pragma once #include "Node.h" class Stack { private: Node* top; // Pointer to the top node unsigned int numNodes; //void displayReverse(Node* temp); // Number of nodes in the stack public: // Constructor Stack(); // Push-Pop methods void push(int newPayload); int pop(); // Top accessor int readTop() const; // Num Elements accessor unsigned int getNumNodes() const; // Display the stack void displayStack() const; //void displayStackReverse();

// Destructor ~Stack(); };

Main

// File Name: Hanoi.cpp // Author: Tanner Crane // Description: Program that recursively solves the Towers of Hanoi with a stack and node class.

#include #include #include "Peg.h"

using namespace std;

const int NUM_DISKS(1); // Number of disks to simulate

int hanoi(int numDisks, Peg& start, Peg& goal, Peg& temp); void moveDisk(Peg& from, Peg& to);

int main() { // Initialize variables Peg peg1("Peg1", NUM_DISKS), peg2("Peg2"), peg3("Peg3",); int numMoves(0);

// Introduce the program

cout << "Welcome to Tanner's Tower of Hanoi simulator...this simulation will be running with " << NUM_DISKS << " disks." << endl << endl;

// Display the starting condition cout << "Starting condition of the three pegs:" << endl; peg1.printPeg(); peg2.printPeg(); peg3.printPeg(); cout << endl;

// Solve Tower of Hanoi - move the disks from peg 1 to peg 3, using peg2 temporarily, getting the number of required moves returned cout << "Moves required to move " << NUM_DISKS << " disks from " << peg1.getName() << " to " << peg3.getName() << ":" << endl; numMoves = hanoi(NUM_DISKS, peg1, peg3, peg2); // Move disks from peg 1 to peg 3 using peg 2

// // Display the ending condition cout << endl << "Ending condition of the three pegs:" << endl; peg1.printPeg(); peg2.printPeg(); peg3.printPeg(); cout << endl; cout << endl;

cout << endl << "A stack of " << NUM_DISKS << " disks can be transferred in " << numMoves << " moves." << endl << endl;

cout << "Thanks for using Tanner's Tower of Hanoi simulator! " << endl; system("PAUSE"); return 0; }

// Hanoi - recursive solution to the tower of hanoi problem int hanoi(int numDisks, Peg& start, Peg& goal, Peg& temp) { int numMoves(0);

// Only if there are disks to be moved if (numDisks > 0) { // First, move n-1 disks to the temporary peg, capturing the number of moves required numMoves = hanoi(numDisks - 1, start, temp, goal);

// Next, move the remaining disk to the goal peg; increment the move count cout << "Move disk " << start.topDisk() << " from " << start.getName() << " to " << goal.getName() << endl; moveDisk(start, goal); numMoves++;

// Finally, move n-1 disks to the goal peg, capturing the number of moves required numMoves += hanoi(numDisks - 1, temp, goal, start); }

return(numMoves); } // Function to move a disk from one peg to another void moveDisk(Peg& from, Peg& to) { // Insure the inputs are valid: "from" has a disk, and that disk isn't smaller than the one on "to"(if there's one there). //assert(from.getSize() > 0 || to.topDisk() > from.topDisk()); // Place the top disk from "from" onto "to" to.addDisk(from.topDisk()); // Remove the top disk from "from" from.removeDisk(); }

Stack.cpp

#include "Stack.h" #include "Node.h" #include #include using namespace std;

Stack::Stack() { Node* temp = top; numNodes = 0; }

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

int Stack::pop() { int tempPayload = top->getPayload(); Node* temp = top; //*top = *top.setNext(); delete temp; return tempPayload; }

void Stack::displayStack() const

{ Node* current = top; while (current != nullptr) { cout << current->getPayload(); current = current->getNext(); }

}

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

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

Stack::~Stack() // Stack destructor {

}

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

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

NODE.H

#pragma once class Node { private: // Private Attributes int payload; // Holds the dataNode Node* next; // Pointer to next public: // Constructor Node(int newPayload, Node* newNext); // Accessors int getPayload() const; Node* getNext() const; // Mutators void setNext(Node* next); void setPayload(int newPayload);

// Destructor ~Node(); };

NODE.CPP

#include "Node.h"

Node::Node(int newPayload, Node* newNext) // constructor { payload = newPayload; next = newNext; } int Node::getPayload() const { return payload; } Node* Node::getNext() const { return next; } void Node::setNext(Node* newNext) // assigning new node via newNext { next = newNext; } void Node::setPayload(int newPayload) // updating the value in payload with newPayload (integer value) { payload = newPayload; }

Node::~Node() //Destructor {

}

PEG.H

// Peg.h by Tanner Crane #pragma once #include #include #include "Stack.h" #include "Node.h"

using namespace std; class Peg { private: void loadDisk(int numDisks); Stack stack; // This is the structure we are using to hold the disks instead of vector string pegName; // The name of the peg

public: Peg(string newName, int numDisks = 0); int getSize() const; void setName(string newName); void printPeg() const; // method to display what disks are on the peg string getName() const; // accessor for the peg name int topDisk() const; // method to return the value of the top disk // methods to add and remove disks from the pegs int removeDisk(); void addDisk(int const& topDisk);;

// destructor ~Peg(); };

PEG.CPP

#include "Peg.h" #include #include #include #include "Stack.h" #include "Node.h" using namespace std; Peg::Peg(string newName, int numDisks) { setName(newName); loadDisk(numDisks); } void Peg::loadDisk(int numDisks) { // Load disks onto the pegs assert(numDisks >= 0); // Assume there is no negative disks for (int i = numDisks; i > 0; i--) { stack.push(i); }

} // accessors string Peg::getName() const { return pegName; }

int Peg::topDisk() const // return the value of the top disk on the peg { //assert(!stack.empty()); return stack.getNumNodes(); } int Peg::getSize() const { // assert(!stack.empty()); return stack.getNumNodes(); }

//Mutators

void Peg::setName(string newName) { pegName = newName; }

void Peg::printPeg() const

{ cout << pegName << " has " << stack.getNumNodes() << " disks: "; stack.displayStack(); cout << endl; }

void Peg::addDisk(int const& topDisk) {

stack.push(topDisk); }

int Peg::removeDisk() { //assert(!stack.empty()); int removedDisk = stack.readTop(); stack.pop();

return removedDisk;

}

//deconstructor Peg::~Peg() { // cout << "This is my Peg destructor" << endl; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions