汉诺塔非递归算法C语言实现.doc

上传人:scccc 文档编号:12018058 上传时间:2021-12-01 格式:DOC 页数:4 大小:20KB
返回 下载 相关 举报
汉诺塔非递归算法C语言实现.doc_第1页
第1页 / 共4页
汉诺塔非递归算法C语言实现.doc_第2页
第2页 / 共4页
汉诺塔非递归算法C语言实现.doc_第3页
第3页 / 共4页
亲,该文档总共4页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《汉诺塔非递归算法C语言实现.doc》由会员分享,可在线阅读,更多相关《汉诺塔非递归算法C语言实现.doc(4页珍藏版)》请在三一文库上搜索。

1、汉诺塔非递归算法 C 语言实现#include<>#include<>#define CSZL 10#define FPZL 10typedef struct hanoiint n;char x,y,z;hanoi;typedef struct Stackhanoi *base,*top;int stacksize;Stack;int InitStack(Stack *S)S->base=(hanoi *)malloc(CSZL*sizeof(hanoi); if(!S->base)return 0;S->top=S->base;S->st

2、acksize=CSZL; return 1;int PushStack(Stack *S,int n,char x,char y,char z)if(S->top-S->base=S->stacksize)S->base=(hanoi *)realloc(S->base,(S->stacksize+FPZL)*sizeof(hanoi); if(!S->base)return 0;S->top=S->base+S->stacksize;S->stacksize+=FPZL;S->top->n=n;S->top

3、->x=x;S->top->y=y;S->top->z=z;S->top+;return 1;int PopStack(Stack *S,int *n,char *x,char *y,char *z)if(S->top=S->base)return 0;elseS->top-;*n=S->top->n;*x=S->top->x;*y=S->top->y;*z=S->top->z;return 1;int EmptyStack(Stack *S)if(S->base=S->top)r

4、eturn 1;elsereturn 0;int i=1;void Move(char x,char z)printf("nntt 第%d部,从 %c移到%c。 ",i+,x,z);int main()int n;char x,y,z;Stack *S;S=(Stack *)malloc(sizeof(Stack);if(!S)return 0;if(!InitStack(S)return 0;printf("nntt 请输入汉诺塔的初始盘子数量以及轴的名称: ");scanf("%d%c%c%c",&n,&x,&am

5、p;y,&z);if(!PushStack(S,n,x,y,z)return 0;while(!EmptyStack(S)if(!PopStack(S,&n,&x,&y,&z)break;if(n=1)Move(x,z);elseif(!PushStack(S,n-1,y,x,z) break;if(!PushStack(S,1,x,y,z) break;if(!PushStack(S,n-1,x,z,y) break;free(S->base);S->base=NULL;S->top=NULL;S->stacksize=0;return 0;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1