Hello, I am trying to write a Tower of Hanoi program using exceptions. I was a little unsure about how to store the disks in each tower so I used stack/LIFO. But for some reason when I run the code, it gives me an error that says "Signal: killing". I'm assuming that I am making a mistake when creating the towers and the problem lies in lines 34 and 40. I'm quite new to the topic of stacks so I would really like to know how to fix it.
If you're going to use 3 distinct "Tower" objects, then each Tower is a stack, which is what you have.
But the initialization is to place the disks on one tower, run the algorithm, and watch them move. But you haven't done that, you've initialized each tower with 3 disks.
Think procedurally; what are the steps you need to follow, then do that. The Object Oriented thing you're doing is leading to more complexity.
L42 Initialize() should be void.
L48 Add() should be void
function Print(). This is an infinite loop if the stack is not initially empty. If not empty then it will continuously display the top entry - as top() does not remove the entry. There are no standard methods to access a std::stack entries (eg like a vector). To display a std::stack, then something like:
which recursively gets the top element, pops it, displays it. Once all displayed, it pushes the pop-ed elements back. For a large stack this can consume excessive time.
NB If you want the elements to be printed in the other order, just switch the recursive call and the std::cout statement.
However, to display a stack without changing it, there is a 'hack'.
std::stack is based upon another container. By default this is std::dequeue but an alternative can be specified when the stack is created.
The stack contents is actually a container of this type within the std::stack class as a protected member (called c). Hence any class derived from std::stack can have access to it.
To solve the "Tower of Hanoi" problem, think about this: Moving n discs from tower "A" to tower "B" can be broken down into moving the top n-1 discs from "A" to "C", then moving the one remaining (biggest) disc from "A" to "B", and finally moving the other n-1 discs (which we previously moved to "C") back from "C" to "B".
Okay, but how exactly do we move the stack of n-1 discs from one tower (source tower) to another tower (destination tower)? Well, just recursively apply the same idea as before: First move the n-2 top discs from the source tower to the other tower, then move the one remaining disc (i.e. the n-1'th disc) from source tower to the destination tower, finally move the n-2 discs from the other tower to the destination tower. Et voilĂ !
This way you can break things down recursively until you only ever need to move a single disc at a time :-)
For 3 disks
Move disk 1 from 1 to 3
Move disk 2 from 1 to 2
Move disk 1 from 3 to 2
Move disk 3 from 1 to 3
Move disk 1 from 2 to 1
Move disk 2 from 2 to 3
Move disk 1 from 1 to 3
Took 7 moves
For 4 disks
Move disk 1 from 1 to 2
Move disk 2 from 1 to 3
Move disk 1 from 2 to 3
Move disk 3 from 1 to 2
Move disk 1 from 3 to 1
Move disk 2 from 3 to 2
Move disk 1 from 1 to 2
Move disk 4 from 1 to 3
Move disk 1 from 2 to 3
Move disk 2 from 2 to 1
Move disk 1 from 3 to 1
Move disk 3 from 2 to 3
Move disk 1 from 1 to 2
Move disk 2 from 1 to 3
Move disk 1 from 2 to 3
Took 15 moves
Thank you all for the help, but unfortunately some of the programming language used in your codes I am unfamiliar with. The only problem that remains now is that in my code, my disks aren't moving to the targeted stack tower. Currently this is the code I have, I took out the exceptions so it is a little easier to read.
if you don't understand something, try looking it up, and if you can't find it or understand the reference you found, then ask what it is. There is a lot of good stuff above, don't throw it out just because its different looking.