Example – Tower of Hanoi

The next example is a significantly more complex application of stacks. It is related to the mathematical game Tower of Hanoi. Let's start with the rules. The game requires three rods, onto which you can put discs. Each disc has a different size. At the beginning, all discs are placed on the first rod, forming the stack, ordered from the smallest (at the top) to the biggest (at the bottom), as follows:

The aim of the game is to move all the discs from the first rod (FROM) to the second one (TO). However, during the whole game, you cannot place a bigger disc on a smaller one. Moreover, you can only move one disc at ...

Get C# Data Structures and Algorithms now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.