REF 6E   Stacks  written 2 ways

  Note:  With only 3 rings 7 MOVE commands could have solved the whole problem.
            A stack was used along with a sophisticated algorithm because beyond 3 the number
            of move grows tremendously

public class Hanoi_test

{  public static void main (String[] args )

    {Hanoi (3, ‘A’, ‘B’, ‘C’);   //play the game with 3 disks

    }

 

     private static void Hanoi (int n, char x, char y, char z)

     {  java.util.Stack stack = new java.util.Stack ( ) ;

         stack.push (new Quad (n, x, y, z) ) ‘

         while   (!stack.empty ( ) )

         {  Quad quad = (Quad) stack.pop ( ) ;

              n = quad.n;   x = quad.a;   y = quad.b;   z = quad.c;

              if  (n = = 1 )  System.out.println

                  (“Move top disk from peg  “ + quad.a + “    to peg “ + quad.c) ;

               else

               {  stack.push (new Quad (n-1, y, x, z) ) ;

                   stack.push (new Quad (1, x, y, z ) ) ;

                   stack.push (new Quad (n-1, x, z, y ) ) ;

 

            }

          }

        }

    }

class Quad

{ public int n;

   public char a, b, c;

   public Quad (int n, char a, char b, char c )

    {  this.n  = n ; this.a  = a ; this.b  =  b; this.c  = c;

    }

}

*******************************************************************************

public class Hanoi_test

{  public static void main (String[] args )

    {Hanoi (3, ‘A’, ‘B’, ‘C’);   //play the game with 3 disks

    }

 

     private static void Hanoi (int n, char x, char y, char z)

     {  java.util.Stack stack = new java.util.Stack ( ) ;

         stack.push (new Quad (n, x, y, z) ) ‘

         while   (!stack.empty ( ) )

         {  Quad quad = (Quad) stack.pop ( ) ;

              n = quad.getNumber();   x = quad.getValueofA();   y = quad.getValueofB();   z = quad.getValueofC();

              if  (n = = 1 )  System.out.println

                  (“Move top disk from peg  “ + quad.a + “    to peg “ + quad.c) ;

               else

               {  stack.push (new Quad (n-1, y, x, z) ) ;

                   stack.push (new Quad (1, x, y, z ) ) ;

                   stack.push (new Quad (n-1, x, z, y ) ) ;

 

            }

          }

        }

    }

class Quad

{

  private int n;

   private char a, b, c;

   public Quad (int a1, char c1, char c2, char c3 )

    {
         n=a1;
         a=c1;
         b=c2;
         c=c3;
 
}
 public int getNumber()
  {
     return(n);
  }
public int getValueofA()
  {
     return(a);
  }
public int getValueofB()
  {
     return(b);
  }
public int getValueofC()
  {
     return(c);
  }

 

 

}