Thread: Tower Of Hanoi

  1. #1
    Unregistered
    Guest

    Tower Of Hanoi

    Does anyone have the code for the tower of hanoi that is not recursive and works for codewarrior 6.0 or Visual C++ 6.0

    I have looked at a bunch of web sites for this and most of there source is done in Java or will not compile correctly if done in C(mem errors).

    If you know of a site that has the correct code that would work just as well.

    Thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use the search, Luke!

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    34
    Can anyone translate this into C?



    #include<iostream.h>


    class cell {
    int disc;
    cell *next;
    cell (int d)
    {
    disc = d;
    next = this;
    }
    cell (int d, cell *n)
    {
    disc = d;
    next = n;
    }
    friend class list;
    };

    class list{
    cell *rear;
    public:
    int empty() { return rear == rear->next; }


    void pop(int &i)
    {
    i = 0;
    if( empty() ) return;
    cell *front = rear->next;
    rear->next = front->next;
    i = front->disc;
    delete front;
    return;
    }

    void push(int d)
    {
    rear->next = new cell(d,rear->next);
    }



    list() { rear = new cell(0); }

    ~list()
    {
    int i,j,l;
    long k;
    while(!empty()) pop(i);
    }
    };


    list pegs[3];

    typedef int (*oper)(int);

    int decr(int a)
    {
    a--;
    if(a<0)
    a=2;
    return a;
    }

    int incr(int a)
    {
    a = (a+1)%3;
    return a;
    }



    int main(void)
    {
    int n;
    oper incr_decr;
    int curr=0;
    int p1 = 1, p2 = 2;
    int d0,d1,d2;
    cout << "Enter Number Of Discs:";
    cin >> n;
    for(int i = n; i > 0; i--)
    pegs[0].push(i);
    incr_decr = n%2 ? decr : incr;
    while(1)
    {
    pegs[curr].pop(d0);
    cout<<"Disc "<<d0<<" From "<<curr;
    curr = incr_decr(curr);
    pegs[curr].push(d0);
    cout<<" To "<<curr<<endl;
    p1 = incr_decr(p1);
    p2 = incr_decr(p2);
    if(!pegs[p1].empty() && !pegs[p2].empty())
    {
    pegs[p1].pop(d1);
    pegs[p2].pop(d2);

    if(d1 < d2)
    {
    pegs[p2].push(d2);
    pegs[p2].push(d1);
    cout<<"Disc "<<d1<<" From "<<p1<<" To "<<p2<<endl;
    }
    else
    {
    pegs[p1].push(d1);
    pegs[p1].push(d2);
    cout<<"Disc "<<d2<<" From "<<p2<<" To "<<p1<<endl;
    }
    }
    else
    {
    if(pegs[p1].empty() && pegs[p2].empty())
    break;
    if(pegs[p1].empty())
    {
    pegs[p2].pop(d2);
    pegs[p1].push(d2);
    cout<<"Disc "<<d2<<" From "<<p2<<" To "<<p1<<endl;
    }
    else
    {
    pegs[p1].pop(d1);
    pegs[p2].push(d1);
    cout<<"Disc "<<d1<<" From "<<p1<<" To "<<p2<<endl;
    }
    }
    }
    return 1;
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Are you asking me, or the original poster? Yes, I can do it.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    34
    Quzah you the star and we all just following your lead. Will you do it though?
    Thanks

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Haven't tested it, but i think this'll work:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /* The 'class'. */
    struct cell {
    	int disc;
    	struct cell *next;
    };
    
    /* The initializer for 'cell'. */
    struct cell *newCell( int d )
    {
        struct cell *c = malloc( sizeof( struct cell ) );
        c->disc = d;
        c->next = NULL;
    	return c;
    }
    
    /* The 'list' class. */
    struct list {
    	struct cell *rear;
    };
    
    /**
    *** Their implementation of 'empty()' appears to be done incorrectly.
    *** IMO, a null list would crash their application.
    **/
    int empty( struct list *l ) { return rear == NULL; }
    
    /* The 'pop' function. */
    int *pop( struct list * l )
    {
        struct cell *c = NULL;
        int d = -1;
    
        if( !l ) { perror( "'pop( )' passed NULL list'" ); exit( 0 ); }
    
        if( l->rear )
    	{
    		c = l->rear;
    		l->rear = l->rear->next;
    		d = c->disc;
    		free( c );
    	}
    	return d;
    }
    
    /* The 'push' function. */
    void push(int d, struct list *l ) 
    { 
    	struct cell *c;
    	c = newCell( d );
        c->next = l->rear;
        l->rear = c;
    } 
    
    void freeList( struct list *l ) { while( !empty( l ) ) pop( l ); }
    
    typedef int (*oper)(int); 
    
    int decr(int a) 
    { 
    a--; 
    if(a<0) 
    a=2; 
    return a; 
    } 
    
    int incr(int a) 
    { 
    a = (a+1)%3; 
    return a; 
    } 
    
    int main(void) 
    {
    	struct list pegs[3];
    
    	int n, curr, p1=1, p2=2, d0,d1,d2, i;
    	oper incr_decr;
    
    	printf("Enter the number of discs: ");
        scanf( "%d", &n );
    
    	for( i = n; i > 0; i-- )
            push( i, pegs[curr] );
    
    	incr_decr = n%2 ? decr : incr; 
    	while(1)
    	{
    		d0 = pop( pegs[curr] );
    		printf("Disc %d from %d ", d0, curr );
    
    		curr = incr_decr(curr); 
    		printf("to %d.\n", curr );
    
    		p1 = incr_decr(p1); 
    		p2 = incr_decr(p2); 
    
    		if(!empry(pegs[p1]) && !empty(pegs[p2])) 
    		{ 
    			d1 = pop( pegs[p1] );
    			d2 = pop( pegs[p2] );
    
    			if(d1 < d2) 
    			{ 
    				push( d2, pegs[p2] );
    				push( d1, pegs[p2] );
    				printf("Disc %d from %d to %d.\n", d1, p1, p2 );
    			}
    			else 
    			{
    				push( d1, pegs[p1] );
    				push( d2, pegs[p1] );
    				printf("Disc %d from %d to %d.\n", d2, p2, p1 );
    			}
    		} 
    		else 
    		{ 
    			if( empty( pegs[p1] ) && empty( pegs[p2] ) ) 
    				break; 
    			if( empty( pegs[p1] ) ) 
    			{ 
    				d2 = pop( pegs[p2] ); 
    				d1 = pop( pegs[p1] ); 
    				printf("Disc %d from %d to %d.\n", d2, p2, p1 );
    			} 
    			else 
    			{
    				d1 = pop( pegs[p1] );
    				push( d1, pegs[p2] );
    				printf("Disc %d from %d to %d.\n", d1, p1, p2 ); 
    			} 
    		} 
    	} 
    	return 1;
    }
    Quzah.
    Last edited by quzah; 12-18-2001 at 10:21 PM.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    34

    Unhappy

    The C++ version of the Code ended up working.
    The prob that i came across with your version was that all your push and pop and such were not correct, that and your rear was not declared.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    'rear' is declared in 'list'. Thus, every instance of 'list' has a member named 'rear'. Additionally, pop is also correct. Pop is also correct. (glancing at the code now) In each case, the function should work correctly. Perhaps I didn't call them correctly. The only thing I can think of is perhaps:

    push( thisNumber, &pegs[curr] );

    Anyway, those functions are designed correctly and they should work if called correctly. Pass them a list and they'll pop a node from it or, or push one on it. Shrug. I donno, I'm tired and work is almost done for the day.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tower of Hanoi
    By dmkanz07 in forum C Programming
    Replies: 13
    Last Post: 03-29-2007, 12:37 PM
  2. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  3. Little Problem on Presentation in the Tower of Hanoi
    By momegao in forum C++ Programming
    Replies: 3
    Last Post: 04-20-2003, 06:22 PM
  4. Tower of Hanoi
    By carrja99 in forum C++ Programming
    Replies: 2
    Last Post: 02-09-2003, 12:15 PM
  5. Hanoi tower
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 06:35 PM