Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Help with witch case to help user navigate tower of hanoi puzzle class Stack { public: Stack ( ) ; ~Stack ( ) ; bool

Help with witch case to help user navigate tower of hanoi puzzle class Stack {
public:
Stack();
~Stack();
bool isEmpty();
bool isFull();
void push(int val);
int pop();
int count();
int peek(int pos);
void change(int pos, int val);
void display();
private:
int top;
int arr[4];
}; #include
#include "main_1.h"
using namespace std;
Stack::Stack(){
top =-1;
for (int i =0; i <4; i++){
arr[i]=0;
}
}
Stack::~Stack(){}
bool Stack::isEmpty(){
if(top ==-1){
cout <<" is empty" << endl;
return true;
}
else{
cout <<" is not empty." << endl;
return false;
}
}
bool Stack::isFull(){
if(top ==3){
cout <<" is full." << endl;
return true;
}
else{
cout <<"is not full." << endl;
return false;
}
}
void Stack::push(int val){
if (isFull()){
cout << "Overflow" << endl;
} else {
top++;
arr[top]= val;
}
}
int Stack::pop(){
if (isEmpty()){
cout << "Stack underflow" << endl;
return 0;
} else {
int popValue = arr[top];
arr[top]=0;
top--;
return popValue;
}
}
int Stack::count(){
return (top +1);
}
int Stack::peek(int pos){
if (isEmpty()){
cout << "Stack underflow" << endl;
return 0;
} else {
return arr[pos];
}
}
void Stack::change(int pos, int val){
arr[pos]= val;
cout << "Value changed at position "<< pos << endl;
}
void Stack::display(){
cout << "All items in stack are:" << endl;
for (int i = top; i >=0; i--){
cout << arr[i]<< endl;
}
}
void towerOfHanoi(int disks, Stack &source, Stack &auxiliary, Stack &destination){
if (disks ==1){
int disk = source.pop();
destination.push(disk);
cout << "Move disk "<< disk <<" from Tower "<< &source <<" to Tower "<< &destination << endl;
return;
}
towerOfHanoi(disks -1, source, destination, auxiliary);
int disk = source.pop();
destination.push(disk);
cout << "Move disk "<< disk <<" from Tower "<< &source <<" to Tower "<< &destination << endl;
towerOfHanoi(disks -1, auxiliary, source, destination);
}
int main(){
int numDisks =3; // Change the number of disks here
Stack towerA, towerB, towerC;
char answer;
int option, choice;
cout << "Would you like to see stack options? Y or N ";
cin >> answer;
if(answer =='Y'|| answer =='y'){
do{
cout << "What operation do you want to perform? Select Option number. Enter 0 to exit." << endl; // HELP WITH OPTION PROMPTING USER TO TAKE STEPS TO MOVE RINGS AND SOLVE PUZZLE

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Professional SQL Server 2012 Internals And Troubleshooting

Authors: Christian Bolton, Justin Langford

1st Edition

1118177657, 9781118177655

More Books

Students also viewed these Databases questions

Question

How do you calculate the breakeven point in units? In dollars?

Answered: 1 week ago

Question

Describe effectiveness of reading at night?

Answered: 1 week ago

Question

find all matrices A (a) A = 13 (b) A + A = 213

Answered: 1 week ago

Question

How can you listen critically to others public speeches?

Answered: 1 week ago