This operation involves push/pop operations on a stack,arranging the smaller discs to sit on larger ones.How can I write a program to implement this?
c program to implement oprations on tower of hanoi
Collapse
X
-
Tags: None
-
here's the code hunny ...
it took me 4 hours to get it ...tracing this code is like hell
# include <iostream.h>
# .....
void towers(int n,char frompeg,char topeg,char auxpeg)
{
if(n==1) //if only one disk just make the move and return
{
cout<<"\n"<<"mo ve disk 1 from peg"<<frompeg<< "to peg"<<topeg<<"\ n";
return;
}
towers(n-1,frompeg,auxpe g,topeg);
cout<<"\n"<<"mo ve disk"<<" "<<n<<"from peg"<<frompeg<< "to peg"<<topeg<<"\ n;
towers(n-1,auxpeg,topeg, frompeg);
}
void main()
{ int n;
cout<<"enter no of disks 2 be moved "<<endl;
cin>>n;
towers(n,'A','C ','B');
getch();
}Comment
Comment