ハンノタワー再帰法と非再帰法

1088 ワード

3つの皿はA、B、Cで、Cは仲介盤で、私たちは移動規則を守ってAの皿をすべてBを通じてCに移動します.
#include
#include
using namespace std;
#define MaxSize 50
typedef struct
{
    int n;      //     
    char x,y,z; //3   
    bool flag;  //        true,  false
}ElemType;      //        
typedef struct
{
    ElemType data[MaxSize];//    
    int top;   //    
}StackType;    //      
void InitStack(StackType *&s)//   
{
    s=(StackType *)malloc(sizeof(StackType));
    s->top=-1;
}
void DestoryStack(StackType *&s)//   
{
    free(s);
}
bool StackEmpty(StackType *s)//       
{
    return(s->top==-1);
}
bool Push(StackType *&s,ElemType e)//  
{
    if(s->top==MaxSize-1)//  
        return false;
    s->top++;
    s->data[s->top]=e;
    return true;
}
bool Pop(StackType *&s,ElemType &e)//  
{
    if(s->top==-1)//   
        return false;
    e=s->data[s->top];//     
    s->top--;         //     1
    return true;
}
void Hanoil(int n,char X,char Y,char Z)//    
{
    if(n==1)
        cout<>n;
    cout<