Computer Science: The Tower of Hanoi

BY IN IB Computer Science Comments Off on Computer Science: The Tower of Hanoi

One of the popular application of stack is called: The Tower of Hanoi. You have three vertical pegs and here is a cone-shaped tower on the leftmost peg, consisting of a series of doughnut-shaped discs. For example, this is what a four-story tower looks like:

tower of hanoi

Your Mission:
Move all discs from peg 1 to peg 3 with the condition that no large disc may be placed on top of a smaller disc, and only one disc (the topmost disc on a peg) may be moved at any one time.

For the above case, you need to initialize the first peg with the sequential numbers: 7, 5, 3, 1 (from bottom to top). While processing, you can follow to this procedure:
1 to 2 –
1 to 3 –
2 to 3 –
1 to 2 –
3 to 1 –
3 to 2 –
1 to 2 –
1 to 3 –
2 to 3 –
2 to 1 –
3 to 1 –
2 to 3 –
1 to 2 –
1 to 3 –
2 to 3 –

Apply the whole process in Pascal. Remember, you need to create three stacks with the maximum of 4 elements of array (according to the case above). Theses stacks will be considered as the pegs that allow you to transfer the element from one to another. The output of your program should be the first condition of the Tower of Hanoi before the operation and the condition of the Tower after the process has done.

Good Luck! 🙂




Comments are closed.