Towers of Hanoi problem means we
have three towers
Here source ,intermediate and
destination are the three towers. We have to transfer all the disks from source
to destination towers. Here the restriction is not to place a big disk on
smaller one . for this we use intermediate tower. Finally the arrangements in
the destination tower must be as same as the disks in the source tower at
first.
Algorithm:
Step 1: start
Step 2: declare the no
Step 3: read the no value
Step 4: if (no<1) Print nothing to move
Else Print
nonrecursion
Step 5:Hanoi non recursion(no,.A’,’B’,’C’)
Step 6:stop
Sub program:
Step 1: Declare num,sndl,indl,dndl,
stkn[ ],stksndl[ ],stkdndl [ ],stkadd[
],
temp,top,add
Step 2: declare the top=NULL
Step 3: one:
If(num==1)then
Print
the out put value
Goto
four
Step 4: two:
Top=top+1
Stkn[top]=num
Stksndl[top]=sndl
Stkindl[top]=indl
Stkdndl[top]=dndl
Stkadd[top]=3
Num=num-1
Sndl=sndl
Temp=indl
Indl=dndl
Dndl=temp
Goto one. Goto step 3
Step 5:
Three:
Print
the output
Top=top+1
Stkn[top]=num
Stksndl[top]=sndl
Stkindl[top]=indl
Stkdndl[top]=dndl
Stkadd[top]=5
Num=num-1
temp=sndl
sndl=indl
Indl=temp
Dndl=dndl
Goto one. Goto step 3
Step 6:
Four:
If(top==NULL)
Return
to main program
Num=
stkn[top]
Sndl= stksndl[top]
Indl= stkindl[top]
Dndl=stkdndl[top]
Add=stkadd[top]
Top=top-1
If(add==3)
Goto
three. Goto step 5
Else
If(add==5)
Goto
four. Goto step 6.
Step 7: return to main program
Program:
#include<stdio.h>
#include<conio.h>
void Hanoinonrecursion(int num,char sndl,char indl,char
dndl)
{
char
stkn[100],stksndl[100],stkindl[100],stkdndl[100],stkadd[100],temp;
int top,add;
top=NULL;
one:
if(num==1)
{
printf("\n Move top disk from needle
%c to needle %c",sndl,dndl);
goto four;
}
two:
top=top+1;
stkn[top]=num;
stksndl[top]=sndl;
stkindl[top]=indl;
stkdndl[top]=dndl;
stkadd[top]=3;
num=num-1;
sndl=sndl;
temp=indl;
indl=dndl;
dndl=temp;
goto one;
three:
printf("\n
Move top disk from needle %c to needle %c",sndl,dndl);
top=top+1;
stkn[top]=num;
stksndl[top]=sndl;
stkindl[top]=indl;
stkdndl[top]=dndl;
stkadd[top]=5;
num=num-1;
temp=sndl;
sndl=indl;
indl=temp;
dndl=dndl;
goto one;
four:
if(top==NULL)
return;
num=stkn[top];
sndl=stksndl[top];
indl=stkindl[top];
dndl=stkdndl[top];
add=stkadd[top];
top=top-1;
if(add==3)
goto three;
else if(add==5)
goto four;
}
void main()
{
int no;
clrscr();
printf("Enter the no. of diss to be
transferred:");
scanf("%d",&no);
if(no<1)
printf("\n
There's nothing to move");
else
printf("\n nonrecursive");
Hanoinonrecursion(no,'A','B','C');
getch();
}
Output:
1.Enter the no. of diss to be
transferred:3
nonrecursive
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
Conclusion: The program is error freed
VIVA QUESATIONS:
1)
What is
purpose of towers of Hanoi
?
Ans: We have to transfer all the disks from source to destination
towers. Here the restriction is not to place a big disk on smaller one . for
this we use intermediate tower. Finally the arrangements in the destination
tower must be as same as the disks in the source tower at first.
2) What is an array ?
Ans: An array is a sequence of
memory location of same data type.
No comments:
Post a Comment