Professional Documents
Culture Documents
Tower of Hanoi Non-Recursive
Tower of Hanoi Non-Recursive
#include <iostream>
struct Node
int n;
char a,b,c;
};
temp->a=a;
temp->b=b;
temp->c=c;
temp->n=n;
temp->next=top;
top=temp;
void pop()
if(top==NULL)
return;
temp=top;
top=top->next;
free(temp);
}
int empty()
return(top==NULL)?1:0;
return top;
int main()
int n;
scanf("%d",&n);
push(n,'A','B','C');
while(!empty())
n=temp->n;
char a=temp->a;
char b=temp->b;
char c=temp->c;
pop();
if(n==1)
else
push(n-1,b,a,c);
push(1,a,b,c);
push(n-1,a,c,b);
}