Question: This code is towers of hanoi in c++ Replace the use of in your Peg class with a Stack class and a Node class that
This code is towers of hanoi in c++
Replace the use of in your Peg class with a Stack class and a 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, and accessors / mutators for each attribute.
Your Stack class should implement a stack data type that consists of Node objects which are dynamically allocated upon creation. It should have a constructor, a destructor, and methods for push, pop, reading the top element, returning the number of elements in the stack, and outputting the stack to the display. Memory should be deallocated upon pop and destruction of the stack object.
IM STILL KIND OF CONFUSED WITH NODES, PLEASE HELP!! THANK YOU!
HERES MY MAIN CODE:
#include "stdafx.h" #include #include #include "Peg.h"
using namespace std;
// Global types // Global consts const int NUM_DISKS(7); // Number of disks to simulate // Global function declarations
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", 0), peg3("Peg3", 0); int numMoves(0);
// Introduce the program cout << "Welcome to Cliff's Towers 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.displayPeg(); peg2.displayPeg(); peg3.displayPeg(); cout << endl;
// Solve Towers 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);
// Display the ending condition cout << endl << "Ending condition of the three pegs:" << endl; peg1.displayPeg(); peg2.displayPeg(); peg3.displayPeg(); cout << endl; cout << endl << "A stack of " << NUM_DISKS << " disks can be transferred in " << numMoves << " moves." << endl << endl; cout << "Thanks for using Cliff's Towers of Hanoi simulator!" << endl;
system("pause"); return 0; } // Functions // 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); }
// Let the caller know how many moves were made return(numMoves); } // Function to move a disk from one peg to another void moveDisk(Peg &from, Peg &to) { assert(from.getNumDisks() > 0); // Verify assumption that the "from" peg has at least one disk on it
if (to.getNumDisks() > 0) // Verify assumption that the diskon "from" is smaller than disk on "to" (only if there is a disk on "to") { assert(from.topDisk() < to.topDisk()); }
// Place the top disk from "from" onto "to" to.addDisk(from.removeDisk()); }
// PEG.CPP
#include "stdafx.h" #include #include #include "Peg.h"
// Static Variables int Peg::numCreatedPegs = 0;
// Constructors - default, with peg name, with # of disks, with peg name number of disks // Constructor - note that there is no default constructor to prevent mis-use of the Peg during initialization
Peg::Peg(string inputName, int numDisks) { name = inputName; loadDisks(numDisks); numCreatedPegs++; cout << "Peg <" << name << "> created, " << numCreatedPegs << " pegs active." << endl; }
// Accessors / Mutators // Function to load disks onto the peg void Peg::loadDisks(int numDisks) { assert(numDisks >= 0); // no negative number of disks! stack.clear(); // Remove any existing disks for (int i = numDisks; i > 0; i--) // Add disks in inverse order (biggest to smallest) { addDisk(i); } }
// Function to return the number of disks on the peg unsigned int Peg::getNumDisks() const { return(stack.size()); }
// Function to display what disks are on the peg void Peg::displayPeg() const { cout << name << " has " << stack.size() << " disks: "; for (int i = getNumDisks(); i > 0; i--) // Read stack from the top { cout << stack[i - 1]; } cout << endl; }
// Accessor for the peg name (intentionally no mutator) string Peg::getName() const { return(name); }
// Function to return the value of the top disk int Peg::topDisk() const { assert(getNumDisks() > 0); // Verify assumption that the Peg has a disk on it return(stack.back()); }
// Functions to add and remove disks from the peg void Peg::addDisk(int diskNum) { stack.push_back(diskNum); }
int Peg::removeDisk() { assert(getNumDisks() > 0); // Verify assumption that the Peg has a disk on it int returnVal = stack.back(); stack.pop_back(); return returnVal; }
// Destructor Peg::~Peg() { numCreatedPegs--; cout << "Peg <" << name << "> destroyed, " << numCreatedPegs << " pegs active." << endl; }
// PEG.H
#pragma once #include #include
using namespace std;
class Peg { private: static int numCreatedPegs; // Number of active peg objects string name; // Name of the peg vector stack; // Structure to hold the disks void loadDisks(int numDisks); // Private method to load numDisks disks onto a Peg
public: // Constructor (note the lack of a default constructor to prevent usage errors) Peg(string pegName, int numDisks);
// Method to return the number of disks on the Peg unsigned int getNumDisks() const;
// Method to display what disks are on the peg void displayPeg() const;
// Accessor for the peg name string getName() const;
// Method to return the value of the top disk int topDisk() const;
// Methods to add and remove disks from the peg void addDisk(int diskNum); int removeDisk();
// Destructor ~Peg(); };
// STACK.CPP
#include "stdafx.h" #include "Stack.h" #include #include
using namespace std;
Stack::Stack() { }
void Stack::push(int newPayload) { top = new Node(newPayload, top); numNodes++; }
int Stack::pop() { assert(top != nullptr); Node *tempPtr = top; int tempPayload = top->getPayload(); top = top->getNext(); delete tempPtr; numNodes--; return tempPayload; }
void Stack::displayStack() const { Node *current = top; while (current != nullptr) { cout << current->getPayload; current = current->getNext; } }
void Stack::reversePrintHelper(Node *current) { if (current != nullptr) { reversePrintHelper(current->getNext); cout << current->getPayload(); } }
void Stack::reversePrint(Node) { reversePrintHelper(top); }
Stack::~Stack() {
}
// STACK.H
#pragma once #include "Node.h"
using namespace std;
class Stack { private: Node *top; // Pointer to the top element int numNodes;
public: //Constructor Stack(); // push method void push(int newPayload);
int pop();
void displayStack() const;
void reversePrint(Node); void reversePrintHelper (Node *current);
// Deconstructor ~Stack();
};
// NODE.CPP
#include "stdafx.h" #include "Node.h"
Node::Node(int newPayload, Node* newNext) { Node *node = new Node(payload, nullptr); }
//Accessors int Node::getPayload() const { payload = node->getPayload(); }
Node::getNext() const { next = node->getNext(); }
// Mutators void Node::setPayload(int newPayload) { node->setPayload(newPayload); }
void Node::setNext(Node* next) { node->setNext(newNext); }
Node::~Node() { }
// NODE.H
#pragma once
using namespace std;
class Node { private: int payload; Node *next;
public: // Construtor Node(int newPayload, Node* newNext);
//Accessors int getPayload() const; Node *getNext () const;
// Mutators void setPayload(int newPayload); void setNext(Node* next);
// Deconstructor ~Node(); };
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
