123 Eng

### Engineering the engineers™

Home
Source Codes
Engineering Colleges

Training  Reports
Seminar Reports
Placement Papers

Forums

Computer Science / IT
Electronics
Electrical
Mechanical
Chemical
Civil

CAT / MBA

GMAT / Foreign MBA
Latest Jobs

Displaying  Source Code(s)

 Implementation of Tower of Hanoi algorithm using recursion. -------------------------------------------------------------------------------- Description : This program shows the movements of disk from one tower to another when a key is pressed. Code : /*------------------------------------------------------------------------ --- PROGRAM TO IMPLEMENT TOWER OF HANOI ALOGITHM USING RECURSION. JAGDISH VASANI VIIT PUNE -------------------------------------------------------------------------- -*/ #include #include #include class tower { int *t1,*t2,*t3; int x,y,z; public: void disp_tower(); void move_disk(int tx,int ty); void toh(int n,int a,int b,int c); tower(int no); ~tower(); }; tower :: tower(int no) { t1 = new int[no+1]; t2 = new int[no+1]; t3 = new int[no+1]; x = no; y = z = 0; for(int i=0,j=no ; i=1) { toh(n-1,tx,tz,ty); move_disk(tx,ty); //x to y disp_tower(); toh(n-1,tz,ty,tx); } } void tower :: move_disk(int tx,int ty) { switch(tx) { case 1: { if(ty==2) t2[y++] = t1[--x]; else t3[z++] = t1[--x]; }break; case 2: { if(ty==1) t1[x++] = t2[--y]; else t3[z++] = t2[--y]; }break; case 3: { if(ty==1) t1[x++] = t3[--z]; else t2[y++] = t3[--z]; }break; }//end of switch } //------------------------------------------------------------------------ --- int main(void) { clrscr(); cout<<"Enter the no. of disks::"; int no; cin>>no; tower obj(no); obj.disp_tower(); obj.toh(no,1,2,3); getch(); return 0; } //------------------------------------------------------------------------ ---

Contribute content or training reports / feedback / Comments
job placement papers