自定义stack类模版,实现empty?full?push,pop,gettop,clear,display
来源:互联网 发布:淘宝淘营销活动有用吗 编辑:程序博客网 时间:2024/06/03 10:37
//stack.h定义
#include<iostream>#include<cassert>#ifndef STACK#define STACKtemplate<class T>class stack{int top;T *a;int cn;public:stack(int c);~stack();bool empty()const;bool full()const;void push(const T &item);T gettop()const;void pop();void clear();void display(const stack<T> &s1);};template<class T>//初始化栈,栈顶top=-1stack<T>::stack(int c){assert(c > 0);cn = c;a = new T[cn];//动态数组建立if (a!= 0)top = -1;elseassert(a != 0);}template<class T>//析构函数调用清除动态数组stack<T>::~stack(){delete[]a;}template<class T>//判空,top=-1则为空bool stack<T>::empty()const{return(top == -1);}template<class T>//栈满判断bool stack<T>::full()const{return(top == cn - 1);}template<class T>//压栈,首先判断是否栈满void stack<T>::push(const T &item){assert(!full());a[++top] = item;//压栈}template<class T>//取栈顶元素T stack<T>::gettop()const{assert(!empty());return a[top];}template<class T>//弹栈void stack<T>::pop(){assert(!empty());return a[top--];}template<class T>//清除栈void stack<T>::clear(){top = -1;}template<class T>//栈显示,从栈顶到栈底void stack<T>::display(const stack<T> &s1){for (int i = cn-1; i >= 0; i--)cout << s1.a[i] << " " << endl;}#endif
//main函数测试
#include<iostream>#include"stack.h"using namespace std;int main(){stack<int>s(3);cout << "stack is empty?" << s.empty() << endl;s.push(1);s.push(2);s.push(3);cout << "the stack is:" << endl;s.display(s);cout << "the top is:" << s.gettop() << endl;;s.clear();stack<char>a(3);a.push('x');a.push('u');a.push('z');a.display(a);system("pause");return 0;}
阅读全文
0 0
- 自定义stack类模版,实现empty?full?push,pop,gettop,clear,display
- 编程实现一个堆栈类Stack,要有push()压栈方法,pop()出栈方法,clear()方法
- 栈程序演示(init,push,pop,clear,empty,traverse)
- 编写一个类,实现简单的栈操作。数据的操作按先进后出的顺序。成员函数为pop; push; size; full; empty; print等等
- stack type, full-ascending, full-descending, empty-ascending, empty-descending
- stack manipulation push and pop
- priority_queue::push();pop(),empty,back(),front();size()
- 自定义 push 和 pop 实现有趣的相册翻开效果
- 控制器转场动画自定义(1):push/pop的实现
- 自定义PUSH POP跳转动画
- pop push 自定义转场效果
- 实现一个栈stack,push出栈,pop入栈,min返回最小值
- #类模板# push,pop
- C++ template--类模版Stack的实现
- 实现一个栈Stack,要求实现Push、Pop、Min(返回最小值的操作)的时间复杂度为O(1)
- UINavigationController自定义,push和pop动画
- UINavigationController自定义,push和pop动画
- iOS 自定义动画 push/pop动画
- 双塔问题
- 软链接,硬链接,以及区别
- 串的堆分配存储的连接
- RTSP/RTMP
- Android解决ScrollView默认不从顶部显示
- 自定义stack类模版,实现empty?full?push,pop,gettop,clear,display
- python日常笔记
- JavaScript高级程序设计笔记(1)_javascript简介
- U-Boot源码(u-boot-2012.04.01)分析start.S中重定位代码
- LeetCode——Valid Parenthese
- 自考总结
- 购物车自定义view 加减号
- 文章标题
- finding_donors