Question: For this machine problem you are to program a solution to the Towers of Hanoi problem. Instead of a recursive program you are to implement
For this machine problem you are to program a solution to the Towers of
Hanoi problem. Instead of a recursive program you are to implement a
solution using a loop and a stack. The algorithm you are to implement is:
PROCEDURE HANOI
Move n disks from pole to pole using pole
S empty stack
S n
while S empty
n i j k S
if n then
move the top disk from pole i to pole k
else
S n j i k
S i j k
S n i k j
end HANOI
S n means push n onto the stack S
n i j k S means to pop S and store the result in n i j k
This notation has been used in examples in class. As usual the move statement should be implemented as output.
The stack is to be implemented using a linked list. You are to fill in the
missing parts of the provided program. Use cassert for error conditions where
ever appropriate. You should test your program for disk, disks and for
disks.
#include
#include
using namespace std;
struct HanoiRecord
int numberDisks;
int sourcePole, sparePole, destinationPole;
HanoiRecordsourcePole sparePole destinationPole ;
numberDisks ;
HanoiRecordint numberDisks, int sourcePole, int sparePole,
int destinationPole
numberDisks numberDisks;
sourcePole sourcePole;
sparePole sparePole;
destinationPole destinationPole;
;
class stack
private:
struct HanoiRecordLink
HanoiRecord value;
HanoiRecordLink next;
;
HanoiRecordLink top;
public:
stack missing code
void push HanoiRecord ;
HanoiRecord pop;
HanoiRecord top
missing code
bool isempty missing code
;
void stack::push missing code
missing code
HanoiRecord stack::pop
missing code
end of stack class definition
int main
int numberOfDisks ;
HanoiRecord tRecord;
stack HanoiStack;
cout "How many disks? flush;
cin numberOfDisks;
HanoiStack.push missing code ;
missing code
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
