Pages

Monday, April 7, 2014

Tower of Hanoi operations


/*A program to impelement Tower of Hanoi operations to understand stack.
compiler- Turbo c++     author- Mangilal Sharma
--------------------------------------------------------*/
#include<iostream.h>
#include<conio.h>
class hanoi
{
 public:
 void tower(char,char,char,int);
 hanoi();
};

void hanoi::tower(char peg1,char peg2,char peg3,int n)
{
 if(n<=0)
 cout<<"
Illagal entery ";

 if(n==1)
 cout<<"
Move Disk from  "<<peg1<<" to "<<peg3;

 else
 {
  tower(peg1,peg3,peg2,n-1);
  tower(peg1,peg2,peg3,1);
  tower(peg2,peg1,peg3,n-1);
 }
}

hanoi::hanoi()
{
 int n;
 char ch=y;
 while(ch==y||ch==Y)
 {
  cout<<"
Input the no. of disks:";

  cin>>n;
  cout<<"
Tower of Hanoi for "<<n<<" disc";

  tower(x,y,z,n);
  cout<<"

Enter Y/y For again enter:";

  cin>>ch;
 }
 return;
}
/*=========================18Nov.,2010=============================*/

Related Posts by Categories

0 comments:

Post a Comment