数据结构学习之数组栈实现
2012-10-26 14:51
573 查看
#ifndef ARRAYSTACK_H #define ARRAYSTACK_H #include <iostream> #include <assert.h> using namespace std; template <class T> class ArrayStack{ int size; int top; T* contain; public: ArrayStack():size(0),top(-1),contain(NULL){} ArrayStack(int MaxSize); void Push(T& element); T& GetTop(); T& Pop(); bool IsEmpty(); void MakeEmpty(); int getSize(){return size;}; }; template<class T> ArrayStack<T>::ArrayStack(int MaxSize){ size = MaxSize; top = -1; contain = new T[size]; } template<class T> void ArrayStack<T>::Push(T& element){ assert(top != size-1); contain[++top] = element; } template<class T> T& ArrayStack<T>::Pop(){ assert(top!=-1); return contain[top--]; } template<class T> T& ArrayStack<T>::GetTop(){ assert(top!=-1); return contain[top]; } template<class T> void ArrayStack<T>::MakeEmpty(){ top = -1; } template<class T> bool ArrayStack<T>::IsEmpty(){ return top==-1; } #endif void show(ArrayStack<int> as){ ArrayStack<int> data(as.getSize()); int temp=0; while(!as.IsEmpty()){ temp=as.Pop(); cout<<temp<<" "; data.Push(temp); } cout<<endl; } void main(){ ArrayStack<int> a(10); int b=0; for(int i=0;i<10;i++){ b++; a.Push(b); } show(a); for(i=0;i<5;i++){ a.Pop(); } show(a); cout<<a.IsEmpty()<<endl; cout<<a.GetTop()<<endl; a.MakeEmpty(); cout<<a.IsEmpty()<<endl; }
相关文章推荐
- 【C++数据结构学习笔记---栈】用数组实现栈
- 学习数据结构笔记--顺序表的数组实现
- JavaScript学习总结(二十一)——使用JavaScript的数组实现数据结构中的队列与堆栈
- 数据结构学习---堆栈的动态数组实现及链表实现
- 数据结构学习---堆栈的动态数组实现及链表实现
- 【C++数据结构学习笔记---线性表】用数组实现线性表
- 【数据结构】栈学习之数组实现(C语言)
- 韩顺平_PHP程序员玩转算法公开课(第一季)07_使用数组实现堆栈_学习笔记_源代码图解_PPT文档整理
- 数据结构之循环数组实现队列
- 一个无聊男人的疯狂《数据结构与算法分析-C++描述》学习笔记 习题2.8 随机数组的三种生成算法(补) 将bash的实现翻译成比较纯正的bash风格
- 数据结构学习笔记之链表分析与实现(二)
- Linux IGMP PROXY 学习笔记 之 一 igmp proxy相关的数据结构及igmp proxy的大致实现要点
- 数据结构---队列(数组实现)
- 数据结构(一) -- 循环队列数组实现
- Java实现一部分简单的排序算法和数据结构(学习笔记20171022002)
- 【数据结构】HashTable原理及实现学习总结
- 【Java数据结构学习笔记之三】Java数据结构与算法之队列(Queue)实现
- 数据结构学习记录连载10(队列提高要求实现)
- php学习_第7章_PHP数组及数据结构
- 第九周 数据结构实现项目--数组和广义表【项目2 - 对称矩阵压缩存储的实现与应用】