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

Home » C++ Home » Data Structures Home » TOWER OF HANOI

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

Search Projects & Source Codes:

Title TOWER OF HANOI
Author Ravindra Kumar
Author Email ravindra [at] iitg.ernet.in
Description solution of tower of hanoi
Category C++ » Data Structures
Hits 375357
Code Select and Copy the Code
/*for any doubt any query mail me back ravindra@iitg.ernet.in*/ /*---------------sumitted by Ravindra kumar-------------------*/ /*-----------tested in Unix/Linux plateform-------------------*/ #include<iostream> //the c++ standard library for stream input output #include<cstdio> //the c standard library for standard input output #include<cstdlib> //for the exit function using namespace std; class arr //arr class that holds each stag { public: int a[100],b[100],c[100]; int topa,topb,topc; }hanoi; int move=1; //counts the no. of moves int main() { void tower(int ,int *,int *,int *,int *,int *,int *); //function prototype void show(int); //function prototype int i; system("clear"); cout<<"Enter the no of elements for which u want to solve the problem "; scanf("%d",&i); system("clear"); for(int j=0;j<i;j++) //feeds the elements in the arrs; { hanoi.a[j]=j+1; hanoi.b[j]=-1; hanoi.c[j]=-1; } hanoi.topa=i-1; //topa,topb,topc,mean the top of each arr hanoi.topb=-1; hanoi.topc=-1; cout<<" Initially "<<" "; for(int j=i-1;j>-1;j--) //show the statusm of each arr { show(hanoi.a[j]); show(hanoi.b[j]); show(hanoi.c[j]); cout<<" "; } cout<<" "; tower(i,hanoi.a,hanoi.c,hanoi.b,&(hanoi.topa),&(hanoi.topc),&(hanoi.topb)) ; //call to do the job } void tower(int n,int src[],int dest[],int aux[],int *ts,int *td,int *ta) //the tower function passes { //the arrs along with the top pointers void show (int); if(n==1) //if one element is there in source arr , { //then it is moved to the destination arr, dest[++(*td)]=src[(*ts)]; src[*ts]=-1; (*ts)--; int max; max=((*ts)>(*td)?(*ts):(*td)); max=(max>(*ta)?max:(*ta)); cout<<" Move "<<move++<<" "; for(int i=max;i>-1;i--) //status of arrs shown { show(hanoi.a[i]); show(hanoi.b[i]); show(hanoi.c[i]); cout<<" "; } cout<<" "; return; } tower(n-1,src,aux,dest,ts,ta,td); //else the tower(1,src,dest,aux,ts,td,ta); // problem is solved by tower(n-1,aux,dest,src,ta,td,ts); // recursive calls } void show(int a) //the show function shows the current status of the arrs { if(a==-1) cout<<"- "; else cout<<a<<" "; }

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-2024 SourceCodesWorld.com, All Rights Reserved.
Page URL: http://www.sourcecodesworld.com/source/show.asp?ScriptID=683


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