汉诺塔移动法则典型模型——栈
来源:互联网 发布:天津java培训学校 编辑:程序博客网 时间:2024/06/04 20:03
汉诺塔游戏流传已久,它主要存在着两种限制:1、先进后出;2、上小下大,而“先进后出”或者“LIFO”(后进先出,Last In First Out)就是汉诺塔移动的法则。
下面是用C++写的栈stack,经过一些简单测试可以实现栈的一些简单操作,测试环境为MS VS8。
- #include <tchar.h>
- /*
- 数据结构——栈
- 栈要遵守“后进先出的规则”
- */
- template <class ElemType>
- class stack{
- private:
- ElemType* stk_;
- int size_; //栈的大小
- int length_; //栈中元素的个数
- public:
- stack(int initsize);
- ~stack();
- void InitStack(int initsize); //初始化栈
- void DestroyStack(); //销毁栈
- int GetLength(); //取得栈中元素个数
- ElemType* GetTop(); //取得顶点元素
- bool StackEmpty(); //栈是否空
- bool StackFull(); //栈是否满
- bool push(ElemType* aItem); //添加元素,
- ElemType* pop(); //删除元素
- ElemType* Get(int index); //取得index位置的元素
- };
- template<class ElemType>
- void stack<ElemType>::InitStack(int initsize)
- {
- stk_ = new ElemType[initsize];
- size_ = initsize;
- length_ = 0;
- }
- template<class ElemType>
- void stack<ElemType>::DestroyStack()
- {
- delete[] stk_;
- length_ = 0;
- }
- template<class ElemType>
- stack<ElemType>::stack(int initsize)
- {
- InitStack(initsize);
- }
- template<class ElemType>
- stack<ElemType>::~stack()
- {
- DestroyStack();
- }
- template<class ElemType>
- int stack<ElemType>::GetLength()
- {
- return length_;
- }
- template<class ElemType>
- ElemType* stack<ElemType>::GetTop()
- {
- return &stk_[length_];
- }
- template<class ElemType>
- ElemType* stack<ElemType>::Get(int index)
- {
- if(index < length_)
- {
- return &stk_[index];
- }
- else return NULL;
- }
- template<class ElemType>
- bool stack<ElemType>::StackEmpty()
- {
- return(length_ == 0);
- }
- template<class ElemType>
- bool stack<ElemType>::StackFull()
- {
- return(length_ == size_);
- }
- template<class ElemType>
- bool stack<ElemType>::push(ElemType* aItem)
- {
- if(!StackFull())
- {
- stk_[length_] = *aItem;
- length_ ++;
- return true;
- }
- else return false;
- }
- template<class ElemType>
- ElemType* stack<ElemType>::pop()
- {
- ElemType* p = NULL;
- if(!StackEmpty())
- {
- length_ --;
- p = &stk_[length_];
- }
- return p;
- }
测试代码如下:
- stack<int>* stk = new stack<int>(10);
- for(int i = 0; i < 4; i++)
- {
- if (stk->push(&i))
- printf("%d has been pushed./n",i);
- else
- printf("%d pushed failed!/n",i);
- }
- printf("%d has get top./n",stk->GetTop());
- for(int i = 0; i < 4; i++)
- {
- printf("%d has been getted./n",*stk->Get(i));
- }
- for(int i = 0; i < 3; i++)
- {
- printf("%d has been pop./n",*stk->pop());
- }
- for(int i = 0; i < 4; i++)
- {
- int* a = stk->Get(i);
- if (a != NULL)
- printf("%d has been getted./n",*a);
- else
- printf("No%d. Item is not exist!/n",i);
- }
- delete stk;
- 汉诺塔移动法则典型模型——栈
- 生活法则——自知法则
- 生活法则——社会法则
- 蓝桥杯——递归二:典型递归模型(2017.2.20)
- 并发服务器 典型模型
- 典型的开发模型
- 典型的服务器模型
- 典型的开发模型
- HoloLens开发——Hololens Spatial mapping(空间映射)中移动子模型后父模型移动的实现方法
- 移动应用的成功法则
- Makefile——典型范例
- 人工神经网络的典型模型
- 人工神经网络的典型模型
- 素质模型的典型应用
- 典型的集群架构模型
- 典型的信息系统生命周期模型
- 典型的开发模型有
- 腾讯典型研发模型--极速模型
- 一切为了“谁”
- sql 行列转换
- java打印处理
- the implementation of system call in linux
- 转帖一篇 牛文。。教父人物Bruce Ecke:Thinking in Java和 Thinking in C++ 的作者 谈论有关 java ,ajax 和 flex 的。
- 汉诺塔移动法则典型模型——栈
- sql 中将日期中分秒化为零
- 女人与电脑
- POJ1061
- libtorrent快把人折磨疯了
- Javascript的使用技巧大全
- 十一前夕,有些人表现得不咋的.....................
- [Microsoft][SQLServer 2000 Driver for JDBC]ResultSet can not re-read row data for column 1.
- javamail的bug