deyanmalinov

Tower of Hanoi

Jul 20th, 2021
945
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.company;
  2. import java.util.ArrayDeque;
  3. public class Main {
  4.     public static void main(String[] args){
  5.         ArrayDeque<Integer> first = new ArrayDeque<>();
  6.         first.push(3);
  7.         first.push(2);
  8.         first.push(1);
  9.         ArrayDeque<Integer> spare = new ArrayDeque<>();
  10.         ArrayDeque<Integer> last = new ArrayDeque<>();
  11.         int disk = 3;
  12.         Move(3, first, spare, last);
  13.         for (Integer integer : last) {
  14.             System.out.println(integer);
  15.         }
  16.     }
  17.     private static void Move(int disk, ArrayDeque<Integer> first, ArrayDeque<Integer> spare, ArrayDeque<Integer> last) {
  18.         if (disk < 1) {
  19.             return;
  20.         }else {
  21.  
  22.             Move(disk -1, first,last,spare);
  23.             last.push(disk);
  24.             first.pop();
  25.             Move(disk -1, spare,first,last);
  26.         }
  27.     }
  28. }
  29.  
RAW Paste Data