#include #include #define MAX 4 struct ss { int val; struct ss *next; }; typedef struct ss s_node; typedef s_node *link; link stack=NULL; int stacksize() { link top; int i; top=stack; i=0; while (top != NULL) { i++; top=top->next; } return i; } void push(int k) { link kk; kk=(link)malloc(sizeof(s_node)); kk->next=stack; kk->val=k; stack=kk; } void pop(int *k) { link top; if (stack != NULL) { *k=stack->val; top=stack; stack=stack->next; free(top); } } void hanoi(int c, int start, int using, int dest) { int swap; do { if (stacksize() != 0) { pop(&dest); pop(&using); pop(&start); pop(&c); printf("move %dth disk from %d to %d\n",c,start,dest); c--; swap=using; using=start; start=swap; } while (c > 0) { push(c); push(start); push(using); push(dest); c--; swap=using; using=dest; dest=swap; } } while (stacksize() > 0); } void main() { clrscr(); hanoi(MAX,1,2,3); }