how to write towers of hanoi in c implementing stack concept?
Aravind Sekar
2006-07-21 21:18:16 UTC
it should be in c using stacks concept
Five answers:
Brian S
2006-07-21 21:30:28 UTC
This is a great problem, and demonstrates the power of recursion and mathematical induction. I would be willing to provide C code to you but only after you showed understanding of the proof...which is the valuable part of the lesson.
scientia
2006-07-21 23:33:17 UTC
Actually, I wrote a non-recursive, non-stack based solution for this in college.
This problem isn't that difficult. You basically start at the bottom and try to move the largest disc to where you want it. If a disc is blocking then you call the same function recursively and try to move the blocking piece. Eventually it will get to a piece it can move and then backtrack. You call the function with the argument being the piece to be moved. Check to see if it can be moved, if not then call the function again with the new argument as the blocking piece, if the piece can be moved you return.
The recursive solution doesn't require a stack since this is included in the recursive call. If you have to explicitly create a stack then it sounds like you are simulating recursion manually. It will work this way as well but you use a loop instead and manually push arguments on a stack which takes the place of local variables. The only thing the stack needs to hold is what the current argument is (the disc you are trying to place).
Charles G
2006-07-21 21:49:36 UTC
You'll need to use some kind of stack to make it work -- the problem literally uses physical stacks.
Do you understand what to do physically, or not?
Do you understand how to code in C, or not?
I could help with either, but starting from the ground up could be hard.
The n=1 case is trivial -- just move the stone from #1 to #3. The n=2 case is also easy -- move #1 to #2, #1 to #3, #2 to #3. You can use recursion from here pretty easily.
Mark aka jack573
2006-07-24 11:04:31 UTC
I will not give you the code, but a description of what is needed.
I hope you understand a stack, and its two functions 'push' and 'pop'.
You will need three stacks.
'Push' all the blocks / discs onto the first stack to initialise the towers. The other two stacks should be empty.
You will need to simulate the moving of the disks by 'pop'ping one from a stack and 'push'ing it onto another.
Continue this process until the end where one stack should contain all blocks / discs and the other two stacks are empty.
eisenhauer
2016-11-25 06:13:27 UTC
To "Q & a guy": Your argument isn't very valid. even as a woman has conceived a baby, the toddler, of route, isn't actual this second completely grown. It starts off as a fetus without each and every of the particular platforms that you may want to work out in a human. also, even even as the youngster is born, it maintains to make stronger. the youngster even finally ends up with more effective bones than it once had; particular applications of an toddler's mind get weaker as they become older too because they finally end up being out of date. As we improve, our cells continuously multiply causing us to get more effective in length, so the theory that all of us began from a unmarried celled organism isn't too some distance fetched. no longer to tutor, numerous microorganisms that reason ailment evolve as our drugs receives more effective stepped ahead. it is all about the organism adapting so as that it might want to be previous to the game and insure the continuation of the species. EDIT: yet another aspect that I forgot to tutor is that our "layout" isn't even perfect. we are at possibility of mutations and diseases. also some parts of the body are not fairly needed; there are some issues that our bodies won't be able to obviously get over besides. Now to the question: the purely component to the 'God theory' that seems life like is the "enable there be gentle" aspect. perchance this changed into possibly the large bang. perchance this universe changed into created by twist of destiny by using some smart beings or some organic occurring phenomenon. perchance it's going to take position lower back. the theory of an omnipotent being has only advanced with mankind over the years to describe what couldn't be defined. Even now when we can not clarify some thing we associate it with issues that we are already commonplace with.
ⓘ
This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.