Add to Favorites    Make Home Page 2509 Online  
 Language Categories  
 Our Services  

Home » C++ Home » Algorithms Home » Implementation of Tower of Hanoi algorithm using recursion.

A D V E R T I S E M E N T

Search Projects & Source Codes:

Title Implementation of Tower of Hanoi algorithm using recursion.
Author Jagdish Vasani
Author Email jagdish.vasani [at] gmail.com
Description This program shows the movements of disk from one tower to another when a key is pressed.

Category C++ » Algorithms
Hits 327806
Code Select and Copy the Code
Code : /*------------------------------------------------------------------------ --- PROGRAM TO IMPLEMENT TOWER OF HANOI ALOGITHM USING RECURSION. JAGDISH VASANI VIIT PUNE -------------------------------------------------------------------------- -*/ #include<iostream.h> #include<stdio.h> #include<conio.h> 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<no ; i++,j--) { t1[i] = j; t2[i] = t2[i] = 0; } t1[no] = t2[no] = t3[no] = 0; } tower :: ~tower() { delete []t1; delete []t2; delete []t3; } void tower :: disp_tower() { clrscr(); cout<<" X :: "; for(int i=0;i<x;i++) { cout<<" "<<t1[i]; } cout<<" Y :: "; for(i=0;i<y;i++) { cout<<" "<<t2[i]; } cout<<" Z :: "; for(i=0;i<z;i++) { cout<<" "<<t3[i]; } getch(); } void tower :: toh(int n,int tx,int ty,int tz) //x to y using z { if(n>=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; } //------------------------------------------------------------------------ ---

Related Source Codes

Script Name Author
Moving ball screen saver karlmarx
The Classic Game of Snake & Ladder Lakshmi Narayana .A
Railway seat reservation question which comes in sapient VyomWorld
To calculate percentile Ravi Mathur
Send to folder ANIMESH SAHU
Analog clock and calendar Nazia & Rida
HIGH/LOW GAME MOLLY ARORA
Data structure (stack Implimentation) Swapnil B Adsure
Memory Game AnirudhSanyal
Easy Calc Anirudh Sanyal
GK Quiz Anirudh Sanyal
Hangman Game Manish Jain
Snakeman Manish Jain
Full month Calendar Nigi
Cursor shapes nigi

A D V E R T I S E M E N T




Google Groups Subscribe to SourceCodesWorld - Techies Talk
Email:

Free eBook - Interview Questions: Get over 1,000 Interview Questions in an eBook for free when you join JobsAssist. Just click on the button below to join JobsAssist and you will immediately receive the Free eBook with thousands of Interview Questions in an ebook when you join.

New! Click here to Add your Code!


ASP Home | C Home | C++ Home | COBOL Home | Java Home | Pascal Home
Source Codes Home Page

 Advertisements  

Google Search

Google

Source Codes World.com is a part of Vyom Network.

Vyom Network : Web Hosting | Dedicated Server | Free SMS, GRE, GMAT, MBA | Online Exams | Freshers Jobs | Software Downloads | Interview Questions | Jobs, Discussions | Placement Papers | Free eBooks | Free eBooks | Free Business Info | Interview Questions | Free Tutorials | Arabic, French, German | IAS Preparation | Jokes, Songs, Fun | Free Classifieds | Free Recipes | Free Downloads | Bangalore Info | Tech Solutions | Project Outsourcing, Web Hosting | GATE Preparation | MBA Preparation | SAP Info | Software Testing | Google Logo Maker | Freshers Jobs

Sitemap | Privacy Policy | Terms and Conditions | Important Websites
Copyright ©2003-2022 SourceCodesWorld.com, All Rights Reserved.
Page URL: http://www.sourcecodesworld.com/source/show.asp?ScriptID=1006


Download Yahoo Messenger | Placement Papers | Free SMS | C Interview Questions | C++ Interview Questions | Quick2Host Review