#include
#define maxstack 10
int c = 1;
struct hanoi {
int n;
char x, y, z;
};
void move(char x, int n, char y)
{
printf("%d. move disk %d from %c to %cn", c++, n, x, y);
}
void hanoi(int n, char x, char y, char z)
{
if (1 == n)
move(x, 1, z);
else {
hanoi(n - 1, x, z, y);
move(x, n, z);
hanoi(n - 1, y, x, z);
}
}
void push(struct hanoi *p, int top, char x, char y, char z,int n)
{
p[top+1].n = n - 1;
p[top+1].x = x;
p[top+1].y = y;
p[top+1].z = z;
}
void unreverse_hanoi(struct hanoi *p)
{
int top = 0;
while (top >= 0) {
while (p[top].n > 1) {
push(p, top, p[top].x, p[top].z, p[top].y, p[top].n);
top++;
}
if (p[top].n == 1) {
move(p[top].x, 1, p[top].z);
top--;
}
if (top >= 0) {
move(p[top].x, p[top].n, p[top].z);
top--;
push(p, top, p[top+1].y, p[top+1].x, p[top+1].z, p[top+1].n);
top++;
}
}
}
int main(void)
{
int i;
printf("reverse program:n");
hanoi(3, 'x', 'y', 'z');
printf("unreverse program:n");
struct hanoi p[maxstack];
c = 1;
p[0].n = 3;
p[0].x = 'x', p[0].y = 'y', p[0].z = 'z';
unreverse_hanoi(p);
return 0;
}