汉罗塔非递归
来源:互联网 发布:bbc 纪录片 萌 知乎 编辑:程序博客网 时间:2024/05/29 13:50
/*汉诺塔递归和非递归算法实现*/#include <iostream>using namespace std;typedef struct Tower{ int height; char a,b,c;}Tower;typedef struct Node{ Tower element; Node* pNext;}Node,*LinkList;typedef struct { LinkList Top;}Stack;void InitStack(Stack& stack){ stack.Top=(LinkList)malloc(sizeof(Node)); stack.Top->pNext=NULL;}void DestroyStack(Stack& stack){ free(stack.Top); stack.Top=NULL;}void Push(Stack& stack,int height,char a,char b,char c){ LinkList temp=(LinkList)malloc(sizeof(Node)); temp->element.height=height; temp->element.a=a; temp->element.b=b; temp->element.c=c; temp->pNext=stack.Top->pNext; stack.Top->pNext=temp;}Tower Pop(Stack& stack){ LinkList temp=stack.Top->pNext; stack.Top->pNext=temp->pNext; Tower element=temp->element; free(temp); return element;}int EmptyStack(Stack stack){ if (stack.Top->pNext==NULL) { return 1; } else { return 0; }}void main(){ Stack stack; InitStack(stack); Push(stack,10,'A','B','C'); while (!EmptyStack(stack)) { Tower temp=Pop(stack); if (temp.height==1) { cout<<temp.a<<"-->"<<temp.c<<endl; } else { Push(stack,temp.height-1,temp.b,temp.a,temp.c); Push(stack,1,temp.a,temp.b,temp.c); Push(stack,temp.height-1,temp.a,temp.c,temp.b); } } DestroyStack(stack);}
0 0
- 汉罗塔非递归
- 递归
- 递归
- 递归
- 递归
- 递归
- 递归
- 递归
- 递归
- 递归
- 递归
- 递归
- 递归
- 递归
- 递归
- 递归
- 递归
- 递归
- jdk, scala, maven, gradle, ant install shell bash, and Dockerfile for Ubuntu:14:04
- 如何让应用程序在计算机开机后延迟启动?
- android倒计时功能的实现(CountDownTimer)
- 三星上传问题解答(特别是2014年7月1日后新注册的开发者)
- Android签名
- 汉罗塔非递归
- 如何判断链表是否有环?如何计算环的长度?
- Spring注解详解
- 午间学习笔记-20160623-Java String
- 关于三星应用市场 提交应用
- c primer plus第9章总结:函数
- C盘不够用-删除D盘空间贡献给C盘的简单方法
- PLupload 上传模块
- 打字初体验