This is untested code, but you should be able to get the idea.

#include <stdio.h>

class h
{
public:
        h(int _n, int _from, int _to, int _use);
        int n, from, to, use;
};

h(int _n, int _from, int _to, int _use) : n(_n), from(_from), to(_to),
use(_use)
{}

int main(int argc, char* argv[])
{
        stack<stackEle> s;
        const int n = 5;

        stackEle *se = new h(n,1,3,2);
        s.push(se);

        while(!s.empty())
        {
                se = s.pop();
                if (se.n == 1)
                        printf("Move disk from %d to %d\n", se.from, se.to);
                else
                {
                        s.push(new h(se.n-1, se.from, se.use,   se.to));
                        s.push(new h(     1, se.from,  se.to,  se.use));
                        s.push(new h(se.n-1,  se.use,  se.to, se.from));
                }
                delete se;
        }
        return 0;
}

On Sep 20, 12:52 pm, prasanth n <nprasnt...@gmail.com> wrote:
> @don:
> if call stack does not count as stack??..
>
>
>
> On Tue, Sep 20, 2011 at 11:13 PM, Don <dondod...@gmail.com> wrote:
> > #include <stdio.h>
>
> > void move(int n, int from, int to, int use)
> > {
> >        if (n == 1) printf("Move disk from peg %d to peg %d\n", from, to);
> >        else
> >        {
> >                move(n-1, from, use, to);
> >                move(1, from , to, use);
> >                move(n-1, use, to, from);
> >        }
> > }
>
> > int main(int argc, char* argv[])
> > {
> >        const int n = 5;
>
> >        move(n, 1, 2, 3);
>
> >        return 0;
> > }
>
> > On Sep 20, 12:37 pm, prasanth n <nprasnt...@gmail.com> wrote:
> > > @don:
> > > yes it think
>
> > > On Tue, Sep 20, 2011 at 11:00 PM, Don <dondod...@gmail.com> wrote:
> > > > Does the call stack count as a stack?
>
> > > > Don
>
> > > > On Sep 20, 12:27 pm, prasanth n <nprasnt...@gmail.com> wrote:
> > > > > In the classic problem of the Towers of Hanoi, you have 3 rods and N
> > > > disks
> > > > > of different sizes which can slide onto any tower. The puzzle starts
> > with
> > > > > disks sorted in ascending order of size from top to bottom (e.g.,
> > each
> > > > disk
> > > > > sits on top of an even larger one). You have the following
> > constraints:
> > > > > (A) Only one disk can be moved at a time.
> > > > > (B) A disk is slid off the top of one rod onto the next rod.
> > > > > (C) A disk can only be placed on top of a larger disk.
> > > > > Write a program to move the disks from the first rod to the last
> > using
> > > > > Stacks.
>
> > > > > --
> > > > > *prasanth*
>
> > > > --
> > > > You received this message because you are subscribed to the Google
> > Groups
> > > > "Algorithm Geeks" group.
> > > > To post to this group, send email to algogeeks@googlegroups.com.
> > > > To unsubscribe from this group, send email to
> > > > algogeeks+unsubscr...@googlegroups.com.
> > > > For more options, visit this group at
> > > >http://groups.google.com/group/algogeeks?hl=en.
>
> > > --
> > > *prasanth*
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Algorithm Geeks" group.
> > To post to this group, send email to algogeeks@googlegroups.com.
> > To unsubscribe from this group, send email to
> > algogeeks+unsubscr...@googlegroups.com.
> > For more options, visit this group at
> >http://groups.google.com/group/algogeeks?hl=en.
>
> --
> *prasanth*

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to