Java Program to Solve Tower of Hanoi Problem using Stacks

This is a Java Program to solve Tower of Hanoi Problem using stacks. Stack is an area of memory that holds all local variables and parameters used by any function and remembers the order in which functions are called so that function returns occur correctly. ‘push’ operation is used to add an element to stack and ‘pop’ operation is used to remove an element from stack. ‘peek’ operation is also implemented returning the value of the top element without removing it. The relation between the push and pop operations is such that the stack is a Last-In-First-Out (LIFO) data structure.

The Tower of Hanoi is a mathematical game or puzzle. It consists of three rods, and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top, thus making a conical shape.
The objective of the puzzle is to move the entire stack to another rod, obeying the following rules:
i) Only one disk may be moved at a time.
ii) Each move consists of taking the upper disk from one of the rods and sliding it onto another rod, on top of the other disks that may already be present on that rod.
iii) No disk may be placed on top of a smaller disk.

Here is the source code of the Java program to solve Tower of Hanoi Problem using stacks. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

/*
 * Java Program to Solve Tower of Hanoi Problem using Stacks
 */
 
 import java.util.*;
 
 /* Class TowerOfHanoiUsingStacks */
 public class TowerOfHanoiUsingStacks
 {
     public static int N;
     /* Creating Stack array  */
     public static Stack<Integer>[] tower = new Stack[4]; 
 
     public static void main(String[] args)
     {
         Scanner scan = new Scanner(System.in);
         tower[1] = new Stack<Integer>();
         tower[2] = new Stack<Integer>();
         tower[3] = new Stack<Integer>();
         /* Accepting number of disks */         
         System.out.println("Enter number of disks");
         int num = scan.nextInt();
         N = num;
         toh(num);
     }
     /* Function to push disks into stack */
     public static void toh(int n)
     {
         for (int d = n; d > 0; d--)
             tower[1].push(d);
         display();
         move(n, 1, 2, 3);         
     }
     /* Recursive Function to move disks */
     public static void move(int n, int a, int b, int c)
     {
         if (n > 0)
         {
             move(n-1, a, c, b);     
             int d = tower[a].pop();                                             
             tower.push(d);
             display();                   
             move(n-1, b, a, c);     
         }         
     }
     /*  Function to display */
     public static void display()
     {
         System.out.println("  A  |  B  |  C");
         System.out.println("---------------");
         for(int i = N - 1; i >= 0; i--)
         {
             String d1 = " ", d2 = " ", d3 = " ";
             try
             {
                 d1 = String.valueOf(tower[1].get(i));
             }
             catch (Exception e){
             }    
             try
             {
                 d2 = String.valueOf(tower[2].get(i));
             }
             catch(Exception e){
             }
             try
             {
                 d3 = String.valueOf(tower[3].get(i));
             }
             catch (Exception e){
             }
             System.out.println("  "+d1+"  |  "+d2+"  |  "+d3);
         }
         System.out.println("\n");         
     }
 }

Output:

Enter number of disks
5
  A  |  B  |  C
---------------
  1  |     |
  2  |     |
  3  |     |
  4  |     |
  5  |     |
 
 
  A  |  B  |  C
---------------
     |     |
  2  |     |
  3  |     |
  4  |     |
  5  |     |  1
 
 
  A  |  B  |  C
---------------
     |     |
     |     |
  3  |     |
  4  |     |
  5  |  2  |  1
 
 
  A  |  B  |  C
---------------
     |     |
     |     |
  3  |     |
  4  |  1  |
  5  |  2  |
 
 
  A  |  B  |  C
---------------
     |     |
     |     |
     |     |
  4  |  1  |
  5  |  2  |  3
 
 
  A  |  B  |  C
---------------
     |     |
     |     |
  1  |     |
  4  |     |
  5  |  2  |  3
 
 
  A  |  B  |  C
---------------
     |     |
     |     |
  1  |     |
  4  |     |  2
  5  |     |  3
 
 
  A  |  B  |  C
---------------
     |     |
     |     |
     |     |  1
  4  |     |  2
  5  |     |  3
 
 
  A  |  B  |  C
---------------
     |     |
     |     |
     |     |  1
     |     |  2
  5  |  4  |  3
 
 
  A  |  B  |  C
---------------
     |     |
     |     |
     |     |
     |  1  |  2
  5  |  4  |  3
 
 
  A  |  B  |  C
---------------
     |     |
     |     |
     |     |
  2  |  1  |
  5  |  4  |  3
 
 
  A  |  B  |  C
---------------
     |     |
     |     |
  1  |     |
  2  |     |
  5  |  4  |  3
 
 
  A  |  B  |  C
---------------
     |     |
     |     |
  1  |     |
  2  |  3  |
  5  |  4  |
 
 
  A  |  B  |  C
---------------
     |     |
     |     |
     |     |
  2  |  3  |
  5  |  4  |  1
 
 
  A  |  B  |  C
---------------
     |     |
     |     |
     |  2  |
     |  3  |
  5  |  4  |  1
 
 
  A  |  B  |  C
---------------
     |     |
     |  1  |
     |  2  |
     |  3  |
  5  |  4  |
 
 
  A  |  B  |  C
---------------
     |     |
     |  1  |
     |  2  |
     |  3  |
     |  4  |  5
 
 
  A  |  B  |  C
---------------
     |     |
     |     |
     |  2  |
     |  3  |
  1  |  4  |  5
 
 
  A  |  B  |  C
---------------
     |     |
     |     |
     |     |
     |  3  |  2
  1  |  4  |  5
 
 
  A  |  B  |  C
---------------
     |     |
     |     |
     |     |  1
     |  3  |  2
     |  4  |  5
 
 
  A  |  B  |  C
---------------
     |     |
     |     |
     |     |  1
     |     |  2
  3  |  4  |  5
 
 
  A  |  B  |  C
---------------
     |     |
     |     |
     |     |
     |  1  |  2
  3  |  4  |  5
 
 
  A  |  B  |  C
---------------
     |     |
     |     |
     |     |
  2  |  1  |
  3  |  4  |  5
 
 
  A  |  B  |  C
---------------
     |     |
     |     |
  1  |     |
  2  |     |
  3  |  4  |  5
 
 
  A  |  B  |  C
---------------
     |     |
     |     |
  1  |     |
  2  |     |  4
  3  |     |  5
 
 
  A  |  B  |  C
---------------
     |     |
     |     |
     |     |  1
  2  |     |  4
  3  |     |  5
 
 
  A  |  B  |  C
---------------
     |     |
     |     |
     |     |  1
     |     |  4
  3  |  2  |  5
 
 
  A  |  B  |  C
---------------
     |     |
     |     |
     |     |
     |  1  |  4
  3  |  2  |  5
 
 
  A  |  B  |  C
---------------
     |     |
     |     |
     |     |  3
     |  1  |  4
     |  2  |  5
 
 
  A  |  B  |  C
---------------
     |     |
     |     |
     |     |  3
     |     |  4
  1  |  2  |  5
 
 
  A  |  B  |  C
---------------
     |     |
     |     |  2
     |     |  3
     |     |  4
  1  |     |  5
 
 
  A  |  B  |  C
---------------
     |     |  1
     |     |  2
     |     |  3
     |     |  4
     |     |  5

Related posts:

Java Program to Implement Euclid GCD Algorithm
Java Program to Implement the Checksum Method for Small String Messages and Detect
So sánh HashMap và HashSet trong Java
Tìm hiểu cơ chế Lazy Evaluation của Stream trong Java 8
Java Program to Compute the Volume of a Tetrahedron Using Determinants
Spring REST API with Protocol Buffers
More Jackson Annotations
Java Program to Generate All Possible Subsets with Exactly k Elements in Each Subset
Java Program to Implement wheel Sieve to Generate Prime Numbers Between Given Range
Java Program to Construct K-D Tree for 2 Dimensional Data
Service Registration with Eureka
Spring Boot Change Context Path
Java Program to Find Number of Spanning Trees in a Complete Bipartite Graph
Java Program to Check if a Directed Graph is a Tree or Not Using DFS
Java Program to Implement Stack
Java Program to Implement Fermat Primality Test Algorithm
Instance Profile Credentials using Spring Cloud
Java Program to Implement Queue
Java Program to Perform Insertion in a BST
Java Program to Perform Searching Based on Locality of Reference
Sorting Query Results with Spring Data
Serverless Functions with Spring Cloud Function
Jackson – Unmarshall to Collection/Array
Java Program to Perform Optimal Paranthesization Using Dynamic Programming
Java Program to Apply Above-Below-on Test to Find the Position of a Point with respect to a Line
Using a Mutex Object in Java
Basic Authentication with the RestTemplate
The Difference Between Collection.stream().forEach() and Collection.forEach()
Guide to BufferedReader
Filtering a Stream of Optionals in Java
Java Program to Implement Johnson’s Algorithm
Java Streams vs Vavr Streams