1

I was working on the problem Tower of Hanoi and thus implemented a stack based version of tower of Hanoi, however I'm not sure whether my implementation follows all the rules of tower of Hanoi. it works for all the cases but I want to verify it here. P.S my stack is not a template but will only work for int data type.

void moveDiscs(int num,Stack& source, Stack& destination, Stack & auxillary)
{
    if (num > 0)
    {
        int temp;
        moveDiscs(num - 1, source, auxillary, destination);
        //------------------------------------

        source.pop(temp);
        destination.push(temp);
        //------------------------------------
        moveDiscs(num-1, auxillary, destination, source);
    }
  • 2
    If you have working code, it generally goes to stackexchange for review or improvements. Also most of the code here is in class Stack, which is not posted. (incidentally there exists a stack implementation in std) – Kenny Ostrom Apr 10 '18 at 22:17
  • 2
    I see nothing here that enforces the rule against moving a larger disk onto a smaller. If you start with the standard starting position, you'll be all right, but given a non-standard (albeit legal) starting position, this code can break the rule. – Beta Apr 10 '18 at 22:20
  • You are substituting the call stack with your `Stack&` i.e. your Stack is completely superfluous. – Captain Giraffe Apr 10 '18 at 23:30
  • you should check out [this answer in - Tower of Hanoi: Recursive Algorithm](https://stackoverflow.com/a/58259294/7541700) – costargc Oct 06 '19 at 17:46

0 Answers0