Find a nonrecursive algorithm for the Tower of Hanoi puzzle

Find a nonrecursive algorithm for the Tower of Hanoi puzzle and implement it in the language of your choice.

Solution


#include<stdio.h>
/* Non-Recursive Function*/
void TowerHanoi(int num,char src,char mid,char dest)
{
char a[50],b[50],c[50],d[50],e[50],temp;
int top,add;
top=NULL;
one:
if(num==1)
{
printf(\"\ Move top disk from needle %c to needle %c \",src,dest);
goto four;
}
two:
top=top+1;
a[top]=num;
b[top]=src;
c[top]=mid;
d[top]=dest;
e[top]=3;
num=num-1;
src=src;
temp=mid;
mid=dest;
dest=temp;
goto one;
three:
printf(\"\ Move top disk from needle %c to needle %c \",src,dest);
top=top+1;
a[top]=num;
b[top]=src;
c[top]=mid;
d[top]=dest;
e[top]=5;
num=num-1;
temp=src;
src=mid;
mid=temp;
dest=dest;
goto one;
four:
if(top==NULL)
return;
num=a[top];
src=b[top];
mid=c[top];
dest=d[top];
add=e[top];
top=top-1;
if(add==3)
goto three;
else if(add==5)
goto four;
}

int main()
{
int no;
  
printf(\"Enter the no. of disks: \");
scanf(\"%d\",&no);
if(no<1)
printf(\"\ There\'s nothing to move.\");
else
printf(\"Non-Recursive\");
TowerHanoi(no,\'A\',\'B\',\'C\');

return 0;
}

=====================================================

Enter the no. of disks: 3
Non-Recursive
Move top disk from needle A to needle C
Move top disk from needle A to needle B
Move top disk from needle C to needle B
Move top disk from needle A to needle C
Move top disk from needle B to needle A
Move top disk from needle B to needle C
Move top disk from needle A to needle C

Find a nonrecursive algorithm for the Tower of Hanoi puzzle and implement it in the language of your choice.Solution #include<stdio.h> /* Non-Recursive Fu
Find a nonrecursive algorithm for the Tower of Hanoi puzzle and implement it in the language of your choice.Solution #include<stdio.h> /* Non-Recursive Fu

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site