472,353 Members | 1,256 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

c program to implement oprations on tower of hanoi

This operation involves push/pop operations on a stack,arranging the smaller discs to sit on larger ones.How can I write a program to implement this?
Oct 27 '06 #1
2 4953
Banfa
9,065 Expert Mod 8TB
Use linked lists to create your stack.
Oct 27 '06 #2
jessy
106 100+
here's the code hunny ...

it took me 4 hours to get it ...tracing this code is like hell

# include <iostream.h>
# .....

void towers(int n,char frompeg,char topeg,char auxpeg)

{
if(n==1) //if only one disk just make the move and return
{
cout<<"\n"<<"move disk 1 from peg"<<frompeg<<"to peg"<<topeg<<"\n";
return;
}
towers(n-1,frompeg,auxpeg,topeg);
cout<<"\n"<<"move disk"<<" "<<n<<"from peg"<<frompeg<<"to peg"<<topeg<<"\n;

towers(n-1,auxpeg,topeg,frompeg);

}

void main()
{ int n;
cout<<"enter no of disks 2 be moved "<<endl;
cin>>n;
towers(n,'A','C','B');
getch();

}
Oct 27 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Sonia | last post by:
Hi, I have written this code for the Tower of Hanoi problem. I was just wondering if there is any way of optimizing this code ? It's a failry...
1
by: elsheikhmh | last post by:
Constant wrote: > > I need a programe that will deal with the solving of the problem ..i > have 3 pegs A, B, C...and I want to move the disk A to...
2
by: Marco Ippolito | last post by:
Hi, I am giving a seminar on C# interop and I would like to feature an original sample class to stimulate some interest. Does anybody want to...
7
by: ashishnh33 | last post by:
hi.......is there nyone to tell me how to solve the "tower of hanoi" problems
0
by: huzaifaa | last post by:
The Towers of Hanoi is an old famous puzzle. The task is to build a program (in assembly language for the simulator of the 6800 used in the tutorial)...
3
by: kamvisiouma | last post by:
Hi there, I'm fresh in learning c++ and I have to solve an exercise with hanoi and my problem is that I'm running out of time! The exercise is...
3
by: shaghayeghcute2003 | last post by:
I'm currently trying to write The Tower Of Hanoi (http:// illuminations.nctm.org/ActivityDetail.aspx?ID=40) in C#. I have a panel on the form and I...
5
by: seemanikam | last post by:
Can anyone suggest me how to devise an efficient divide-and-conquer algorithm for the Tower-of-Hanoi problem when the disks are colored alternately...
11
by: 960392954 | last post by:
The Towers of Hanoi is a famous problem about moving a certain number of disks from one peg to another.I write a recursive program for it.If you...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.