Question: Here is my assignment: Using the code from Program #5, do the following: Modify the Stack and Node classes so they become templated classes. Modify
Here is my assignment:
Using the code from Program #5, do the following:
Modify the Stack and Node classes so they become templated classes.
Modify the Peg class to utilize disks of type char instead of type int.
The following test code will be added at the beginning of main() during the grading process:
// Let's play with our Stack class
{ Stack
Stack
}
When complete the program should run the same way it ran in Program #5, except the disks will be indicated by letters instead of numbers.
Create a .zip file of the program directory and post the .zip file to Canvas.
Below is my code:
NODE.CPP
#include "Node.h" #include
Node::Node(int newPayload, Node* newNext) // constructor { payload = newPayload; next = newNext; } int Node::getPayload() const { return payload; }
ostream& operator <<(ostream& outStream, const Node& node) { outStream << node.getPayload(); return(outStream); }
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 {
}
NODE.H
#pragma once #include
PEG.CPP
#include "Peg.h" #include
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.getNumNodes() > 0); return stack.readTop(); } int Peg::getSize() const { return stack.getNumNodes(); // size of the stack }
//Mutators
void Peg::setName(string newName) { pegName = newName; }
ostream& operator << (ostream& outStream, const Peg& peg) { outStream << peg.getName() << " has " << peg.getSize() << " disks: " << peg.stack << endl; return(outStream); }
void Peg::addDisk(int const topDisk) //& removed {
stack.push(topDisk); }
int Peg::removeDisk() { assert(stack.getNumNodes() > 0); int removedDisk = stack.readTop(); stack.pop();
return removedDisk;
}
//Peg destructor Peg::~Peg() { }
PEG.H
// Peg.h by Tanner Crane #pragma once #include
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); // Class Constructor 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); friend ostream& operator <<(ostream& outStream, const Peg& peg);
// destructor ~Peg(); };
STACK.H
#pragma once #include "Node.h" #include
public: // Constructor Stack(); Stack(const Stack& copy); // Push-Pop methods void push(int newPayload); int pop(); // Top accessor int readTop() const; // Num Elements accessor unsigned int getNumNodes() const; // Display the stack friend ostream& operator <<(ostream& outStream, const Stack& stack); friend bool operator == (const Stack& lvalue, const Stack& rvalue); Stack& operator= (Stack& copy); void copyHelper(Node* top, Stack newStack); void copyHelper(Node* top); // Destructor ~Stack(); };
STACK.CPP
#include "Stack.h" #include "Node.h" #include
Stack::Stack() { top = nullptr; numNodes = 0; }
Stack::Stack(const Stack& copy) {
top = nullptr; numNodes = 0; copyHelper(copy.top); } void Stack::copyHelper(Node* top) { if (top != nullptr) { copyHelper(top->getNext()); push(top->getPayload()); } } void Stack::push(int newPayload) { try { top = new Node(newPayload, top); numNodes++; } catch (exception & e) { cerr << "Caught: " << e.what() << endl; cerr << "Type: " << typeid(e).name() << endl; }
}
int Stack::pop() { assert(top != nullptr);
if (numNodes == 0) { throw runtime_error("Illegal pop"); } int tempPayload = top->getPayload(); Node* temp = top; top = top->getNext(); delete temp; numNodes--; return tempPayload; }
int Stack::readTop() const { assert(top != nullptr); if (numNodes == 0) {
throw runtime_error("Illegal ReadTop"); }
return top->getPayload(); }
unsigned int Stack::getNumNodes() const { return numNodes; }
Stack::~Stack() // Stack destructor { while (top != nullptr) { pop(); } }
ostream& operator << (ostream & outStream, const Stack & stack) { stack.displayReverse(outStream, stack.top); return(outStream);
} bool operator == (const Stack& lvalue, const Stack& rvalue) { bool result = true; Node* ltemp = lvalue.top; Node* rtemp = rvalue.top;
if (ltemp->getPayload() != rtemp->getPayload()) { result = false; }
ltemp = ltemp->getNext(); rtemp = rtemp->getNext();
return result; }
Stack& Stack::operator=(Stack& copy) { Node* temp = copy.top; while (top != nullptr) { pop(); } copyHelper(copy.top); return *this; } void Stack::displayReverse(ostream & outStream, Node * temp) const { // Recurse to the end of the stack if (temp != nullptr) {
displayReverse(outStream, temp->getNext()); outStream << *temp << " "; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
