Comments on: Non-recursive solution to tower of hanoi https://demo.ritambhara.in/non-recursive-solution-to-tower-of-hanoi/ Coding / System Design Interviews Mon, 21 Nov 2022 05:43:46 +0000 hourly 1 https://wordpress.org/?v=6.7.2 By: Sayantan patra https://demo.ritambhara.in/non-recursive-solution-to-tower-of-hanoi/#comment-6983 Mon, 21 Nov 2022 05:43:46 +0000 http://www.ritambhara.in/?p=1008#comment-6983 Easiest and fastest way

]]>
By: Abhay Gupta https://demo.ritambhara.in/non-recursive-solution-to-tower-of-hanoi/#comment-6694 Thu, 07 Jul 2022 13:27:10 +0000 http://www.ritambhara.in/?p=1008#comment-6694 if total number of moves required is 2^n-1 then how come complexity of your algorithm is O(n)?

]]>
By: Tafese Gechera https://demo.ritambhara.in/non-recursive-solution-to-tower-of-hanoi/#comment-6663 Tue, 21 Jun 2022 09:37:08 +0000 http://www.ritambhara.in/?p=1008#comment-6663 I want Python code for non-recursive Tower of Hanoi

]]>
By: a https://demo.ritambhara.in/non-recursive-solution-to-tower-of-hanoi/#comment-6006 Sun, 03 Oct 2021 22:51:53 +0000 http://www.ritambhara.in/?p=1008#comment-6006 with 3 rods and 10 disks your algorithm needs 29523 moves to finish.
with 11 disks it loops with a finished rod in the center
if i reverse the move_number %2 == 0, it needs 88572 moves on 11 disks

]]>
By: Dhoomil Sheta https://demo.ritambhara.in/non-recursive-solution-to-tower-of-hanoi/#comment-1926 Mon, 23 Mar 2015 15:20:35 +0000 http://www.ritambhara.in/?p=1008#comment-1926 import java.io.*;
import java.util.Stack;
import java.util.EmptyStackException;
public class TowerOfHanoi {
public static int legalMove(Stack A, Stack B)
{
int a,b;
try {
a = Integer.parseInt(A.peek().toString());
}
catch(EmptyStackException e){
a = 0;
}
try {
b = Integer.parseInt(B.peek().toString());
}
catch(EmptyStackException e){
b = 0;
}
if(a==b) return 0;
if(a == 0)
{
A.push(B.pop());
return 2;
}
else if(b == 0)
{
B.push(A.pop());
return 1;
}
if(a b)
{
A.push(B.pop());
return 2;
}
return -1;
}
public static void main(String[] args) {
int stepNumber = 0;
int status = 0;
try {
Stack A = new Stack();
Stack B = new Stack();
Stack C = new Stack();
System.out.println(“Enter the number of disks : “);
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(input.readLine());
if(n0; i–)
A.push(i);
int m = n%2;
do {
if(m==1)
{
if((status = legalMove(A,C)) == 1)
System.out.println((++stepNumber) + “. A –> C”);
else if (status == 2)
System.out.println((++stepNumber) + “. C –> A”);
if((status = legalMove(A,B)) == 1)
System.out.println((++stepNumber) + “. A –> B”);
else if(status == 2)
System.out.println((++stepNumber) + “. B –> A”);
else
break;
}
else
{
if((status = legalMove(A,B)) == 1)
System.out.println((++stepNumber) + “. A –> B”);
else if (status == 2)
System.out.println((++stepNumber) + “. B –> A”);
if((status = legalMove(A,C)) == 1)
System.out.println((++stepNumber) + “. A –> C”);
else if(status == 2)
System.out.println((++stepNumber) + “. C –> A”);
}
if((status = legalMove(B,C)) == 1)
System.out.println((++stepNumber) + “. B –> C”);
else if(status == 2)
System.out.println((++stepNumber) + “. C –> B”);
}while(C.size()!=n);
System.out.println(“X———————–X”);
}
catch (Exception e){
}
}
}

]]>
By: Ali Slt https://demo.ritambhara.in/non-recursive-solution-to-tower-of-hanoi/#comment-1925 Fri, 16 May 2014 15:19:50 +0000 http://www.ritambhara.in/?p=1008#comment-1925 please write the code to non recursive hanoi towers 🙁

]]>
By: A https://demo.ritambhara.in/non-recursive-solution-to-tower-of-hanoi/#comment-1924 Fri, 07 Jun 2013 19:47:31 +0000 http://www.ritambhara.in/?p=1008#comment-1924 Please write code.

]]>
By: sai https://demo.ritambhara.in/non-recursive-solution-to-tower-of-hanoi/#comment-1923 Fri, 25 Jan 2013 11:44:07 +0000 http://www.ritambhara.in/?p=1008#comment-1923 please write code for this in c language

]]>