#include <stdio.h>

void move_disk(int n, int from, int to) {
    printf("Move disk %d from %d to %d.\n", n, from, to);
}

struct frame {
    int n, from, to, free;
    int phase;
};

struct frame stack[100];
struct frame *bottom = &stack[0];
struct frame *sp;

void push (int n, int from, int to, int free) {
    sp++;
    sp->n = n;
    sp->from = from;
    sp->to = to;
    sp->free = free;
    sp->phase = 1;
}

void pop () {
    sp--;
}

void hanoi (int n, int from, int to, int free) {
    sp = bottom;
    push(n, from, to, free);
    do {
        if (sp->n == 1) {
            move_disk(1, sp->from, sp->to);
            pop();
        } else {
            if (sp->phase == 1) {
                sp->phase = 2;
                push(sp->n-1, sp->from, sp->free, sp->to);
            } else if (sp->phase == 2) {
                move_disk(sp->n, sp->from, sp->to);
                sp->phase = 3;
                push(sp->n-1, sp->free, sp->to, sp->from);
            } else {
                pop();
            }
        }
    } while (sp > bottom);
}

int main () {
    hanoi(3, 1, 3, 2);
}
