Thursday 29 November 2012

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<<"\n Illagal entery ";
 if(n==1)
 cout<<"\nMove 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<<"\nInput the no. of disks:";
  cin>>n;
  cout<<"\nTower of Hanoi for "<<n<<" disc";
  tower('x','y','z',n);
  cout<<"\n\nEnter Y/y For again enter:";
  cin>>ch;
 }
 return;
}
/*=========================18Nov.,2010=============================*/

No comments:

Post a Comment

You are welcome, your comment helps me to make this blog more better.