I have many header files for stacks such as pushStack, popStack, destroyStack, etc eg

//stacksADT.h file

#include "P3-06.h" /* Stack ADT Definitions */
#include "P3-07.h" /* Create Stack */
#include "P3-08.h" /* Push Stack */
#include "P3-09.h" /* Pop Stack */
#include "P3-10.h" /* Retrieve Stack Top */
#include "P3-11.h" /* Empty Stack */
#include "P3-12.h" /* Full Stack */
#include "P3-13.h" /* Stack Count */
#include "P3-14.h" /* DestroyStack */


So I wrote the main program to make a simple tower of hanoi using stacks. But I am stuck. I don't know how to use push and pop the elements and how to make the move legally, eg bigger disk cannot put on a smaller disk, etc. Here is my incomplete code:

Just give me any suggestions how can I build a proper tower of hanoi regarding to these problems. Thanks in advance.

#include <stdio.h>
#include <stdbool.h>
#include "stacksADT.h"
#DEFINE smallest 0

int main (void) {
    // Local Definitions
   	void push();
   	pop();
   	void hanoi(int N, char source, char dest, char aux);
	int* moveDisk;
	STACK* stack;
	
	// Statements
	stack = createStack ();
	
	printf("Enter a disc(s): ");
	scanf ("%d", N);
	
	if (N > 0) {
          pushStack(dest, pop(source));
          }
    else {
         hanoi(N - 1, source, dest, aux);
         pushStack(dest, pop(source));
         hanoi(N - 1, aux, source, dest);
    }
    
void makeLegalMove() {
     if (smallest <= source) {
                  
    
    printf("\nThe tower of hanoi\n");
    while(!emptyStack(stack)) {
                              
	
	destroyStack (stack);
	system("pause");
	return 0;
}

Dani AI

Generated

You do not need recursion here; your ADT is a perfect fit for the standard iterative solution using three stacks. Treat each peg as a stack and push disk sizes N..1 onto the source (smaller number = smaller disk). Then repeat a fixed sequence of legal moves until the destination stack holds N disks. Key fixes first: create three stacks (src, aux, dst), read N with scanf("%d", &N);, push disks N..1 onto src, and remove the unused void push(); pop(); prototypes.

A single helper that enforces legality is enough. Replace empty(), peek(), pop(), and push() with your ADT’s names (use your "Retrieve Stack Top" for peek).

bool move_between(char fromName, Stack* from, char toName, Stack* to) {
    if (empty(to) && empty(from)) return false;

    // If one side is empty, move from the non-empty side to the empty side
    if (empty(from)) { int d = pop(to); push(from, d); printf("Moving disk %d from Tower %c to Tower %c\n", d, toName, fromName); return true; }
    if (empty(to))   { int d = pop(from); push(to, d);   printf("Moving disk %d from Tower %c to Tower %c\n", d, fromName, toName); return true; }

    int a = peek(from), b = peek(to);
    if (a < b) { int d = pop(from); push(to, d); printf("Moving disk %d from Tower %c to Tower %c\n", d, fromName, toName); }
    else       { int d = pop(to);   push(from, d); printf("Moving disk %d from Tower %c to Tower %c\n", d, toName, fromName); }
    return true;
}

Driver (outline): if N is odd, repeatedly call moves in order (src,dst), (src,aux), (aux,dst). If N is even, swap dst and aux (or call in order (src,aux), (src,dst), (aux,dst)). Stop when stackCount(dst) == N. This guarantees only legal moves occur and prints the exact sequence. For a worked explanation of this iterative pattern, see iterative Tower of Hanoi with stacks.

Recommended Answers

All 3 Replies

Whether you use 3 linked lists, 3 small arrays, or 3 small LIFO stacks (I refer to FIFO structs as queues), just remember that if you want to move an odd number of disks to needle #1, then you start by moving the first disk TO needle #1. When you have an even number of disks, move the first disk AWAY from needle #1.

Since the computer can make the moves for the game so fast, it's great if you'd add a delay midway through the move of each disk - I put mine right at the very top of the needle the disk was being removed from.

It's extra work, but you can make a very colorful Tower of Hanoi game, using just the text console. Making each disk with it's own color, is a great improvement in the looks of the game, imo.

Note that the player should not be selecting a disk number to move. The player selects the *needle* number that he wants to remove a disk from - and only the top disk can be moved.

Whether you use 3 linked lists, 3 small arrays, or 3 small LIFO stacks (I refer to FIFO structs as queues), just remember that if you want to move an odd number of disks to needle #1, then you start by moving the first disk TO needle #1. When you have an even number of disks, move the first disk AWAY from needle #1.

Since the computer can make the moves for the game so fast, it's great if you'd add a delay midway through the move of each disk - I put mine right at the very top of the needle the disk was being removed from.

It's extra work, but you can make a very colorful Tower of Hanoi game, using just the text console. Making each disk with it's own color, is a great improvement in the looks of the game, imo.

Note that the player should not be selecting a disk number to move. The player selects the *needle* number that he wants to remove a disk from - and only the top disk can be moved.

I don't require to do an extra work and I don't want to add colors. All I need is a simple Tower of Hanoi, for example:

Moving disc 1 from Tower 1 to Tower 3
Moving disc 2 from Tower 1 to Tower 2
etc

1. I created a stack - createStack();
2. add N for input, eg. if i put 3, then N - 1, source, dest, aux
3. push them into a stack
4. pop one disk that moves to another with the make of moves legally, eg bigger disk can't be on smaller one.
5. bool non-emptyStack then run.

That's it but my real problem is that it is hard for me to convert this algorithm to codes! Any suggestions?

You have three needles, so you need three stacks.

Push each disk's value (way I did it was to have the value equal the width of the disk), onto one stack. Let's say you had 3 disks to be moved, and all the disks had to end up on needle #3, after starting out on #1 needle.

The moves would be:


1) 1 - 3 (because you have an odd number of disks to be moved from this needle)
2) 1 - 2 (because now you have an even number "" "" "" "" "" "" "".
3) 3 - 2 clearing the needle #3
4) 1 - 3 Back to an odd number - so move to the final goal needle
5) 2 - 1 There are an even number on this needle, so move away
6) 2 - 3 Odd
7) 1 - 3 Odd

Done! ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.