Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please help I am trying to show the output of steps of the discs moving from each tower in this tower of hanoi program using
Please help I am trying to show the output of steps of the discs moving from each tower in this tower of hanoi program using stacks. #include
#include
#include
#include "mainh
#include "Hanoi.h
using namespace std;
Stack::Stack
top ;
forint i ; i ; i
arri;
Stack::~Stack
bool Stack::isEmpty
iftop
return true;
else
return false;
bool Stack::isFull
iftop
return true;
else
return false;
void Stack::pushint val
ifisFull
cout "Overflow" endl;
else
top;
arrtopval;
int Stack::pop
ifisEmpty
cout "stack underflow" endl;
return ;
else
int popValue arrtop;
arrtop;
top;
return popValue;
int Stack::count
return top ;
int Stack::peekint pos
ifisEmpty
cout "Stack underflow" endl;
return ;
else
return arrpos;
void Stack::changeint pos, int val
arrposval;
cout "Value changed at position" pos endl;
void Stack::display
cout "all items in stack are endl;
forint i ; i ; i
cout arri endl;
void moveDiskint disks, char fromRod, char toRod
cout"Moving disk top
source.pushtop;
source.pushtop;
moveDisktop d s;
void towerOfHanoiint disks, Stack &source, Stack &auxiliary, Stack &destination
char s S d D a A;
ifdisks
char var d;
d a;
a var;
int numMoves pow disks;
forint i disks; i ; i
source.pushi;
forint i ; i numMoves; i
ifi
moveDiskHelperauxiliary destination, a d;
else ifi
moveDiskHelpersource auxiliary, s a;
else ifi
moveDiskHelpersource destination, s d;
int main
int numDisks ;
Stack towerA, towerB, towerC;
forint i numDisks; i ; i
towerA.pushi;
cout "initial configuration:" endl;
towerA.display;
towerB.display;
towerC.display;
towerOfHanoinumDisks towerA, towerB, towerC;
cout "Final configuration:" endl;
towerA.display;
towerB.display;
towerC.display;
return ;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started