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

Home » C Home » Beginners / Lab Assignments Home » Implementation of Tower of Hanoi algorithm using Iterative

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 Iterative
Author Jagdish Vasani
Author Email jagdish.vasani [at] gmail.com
Description Iterative solution for Tower of Hanoi Problem.
This program shows the movements of disk from one tower to another when a key is pressed.
Category C » Beginners / Lab Assignments
Hits 369780
Code Select and Copy the Code
Code : /*------------------------------------------------------------------------ --- PROGRAM TO IMPLEMENT TOWER OF HANOI ALGORITHM USING ITERATIVE METHOD. JAGDISH VASANI COLLEGE : VIIT PUNE DATE : 18/07/2005 -------------------------------------------------------------------------- -*/ #include<iostream.h> #include<stdio.h> #include<conio.h> #define ODD 1 #define EVEN 0 #define JV 246 #define JAG(j) count+=j int count = 0; //count for complexity class tower { int *t1,*t2,*t3; int x,y,z; int src,dest; public: void disp_tower(); void move_disk(int tx,int ty); void toh(); int get_dest_peg(int src); 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; t1[-1] = t2[-1] = t3[-1] = JV; 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() //x to y using z { int total_rings = x; src = 1; if(y==0 && z==0 && (t1[0]%2)==ODD) //only for 1st disk dest = 2; else dest = 3; JAG(4); while(1) { JAG(1); move_disk(src,dest); disp_tower(); //no count for disp JAG(1); if(y==total_rings) break; JAG(1); if( (t1[x-1]>t2[y-1] && t1[x-1]<t3[z-1]) || (t1[x-1]>t3[z-1] && t1[x-1]<t2[y-1])) { src = 1; //if peg 1 contains mid ring } else { JAG(1); if((t2[y-1]>t1[x-1] && t2[y-1]<t3[z-1]) || (t2[y-1]>t3[z-1] && t2[y-1]<t1[x-1])) src = 2; else src = 3; } JAG(1); //for above src assignment JAG(2); //for following 2 stmts dest = t1[x-1] > t2[y-1] ? (t1[x-1]>t3[z-1] ? 1:3) : (t2[y-1]>t3[z-1] ? 2:3); //finds max = dest move_disk(src,dest); disp_tower(); //no count for disp JAG(2); //for following 2 stmts src = t1[x-1] < t2[y-1] ? (t1[x-1]<t3[z-1] ? 1:3) : (t2[y-1]<t3[z-1] ? 2:3); //finds minimum = src dest = get_dest_peg(src); //ie dest = smaller + even } } int tower :: get_dest_peg(int src_peg) { switch(src_peg) { case 1: { if(t2[y-1]%2==0 && t3[z-1]%2==0) //if both r even then select smaller one { if(t2[y-1]>t3[z-1]) return 3; else return 2; } else { if(t2[y-1]%2==0) //select even return 2; else return 3; } } case 2: { if(t1[x-1]%2==0 && t3[z-1]%2==0) //if both r even then select smaller one { if(t1[x-1]>t3[z-1]) return 3; else return 1; } else { if(t1[x-1]%2==0) //select even return 1; else return 3; } } case 3: { if(t1[x-1]%2==0 && t2[y-1]%2==0) //if both r even then select smaller one { if(t1[x-1]>t2[y-1]) return 2; else return 1; } else { if(t1[x-1]%2==0) //select even return 1; else return 2; } } }//end of switch JAG(3); //bcos each case takes only 3 steps return 0; } void tower :: move_disk(int tx,int ty) //tx = src & ty = dest { 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 count++; //this for if count++; //this for = assignment } //------------------------------------------------------------------------ --- int main(void) { clrscr(); cout<<"Enter the no. of disks::"; int no; cin>>no; tower obj(no); obj.disp_tower(); obj.toh(); JAG(1); printf(" Total count = %d",count); printf(" Complexity = 6*2^n (approx)"); printf(" ie O(2^n)"); getch(); return 0; } //------------------------------------------------------------------------ ---

Related Source Codes

Script Name Author
The Game Opposite as seen on Nokia 2300 Mobile Manikanta
RECURSIVE BALANCED QUICK SORT ashish
Radix Sort ashish
Change your mouse pointer Ashim
The blinking star Shashank
Data Validation Crylittlebaby
To search a file by giving file type like mp3 or mpeg or doc Prashanth SR
Menus Demonstration B.Chidhambaram
Employee Database Project Using C. Reenku Raman Nayak
Creating a Lexical Analyzer in c fahad bader al-buhairi դ ? ??
Calendar Program Omkar & Devendra
Stop double Process for start in C Cedrik Jurak
Stop double Process for start in C Cedrik Jurak
Time Scheduler Atiq Anwar
A timepass game between atmost two players Rahul Roy

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=1029


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