栈的基本操作
来源:互联网 发布:淘宝备注在哪 编辑:程序博客网 时间:2024/06/05 20:03
栈的基本操作
注:利用模板类建类,进行栈定义
模板类定义在整个类定义之前
#include<iostream>#include<string>#include<cassert>using namespace std;template<class Type> class Stack {private: Type *urls; int max_size, top_index;public: Stack(int length_input) { urls = new Type[length_input]; max_size = length_input; top_index = -1; } ~Stack() { delete[] urls; } bool push(const Type &element) { if (top_index >= max_size - 1) { return false; } top_index++; urls[top_index] = element; return true; } bool pop() { if (top_index < 0) { return false; } top_index--; return true; } Type top(){ assert(top_index>=0); return urls[top_index]; }};int main() { int n, m; cin >> n >> m; Stack<string> stack(n); for (int i = 1; i <= m; i++) { int opr; cin >> opr; if (opr == 0) { string element; cin >> element; if (stack.push(element)) { cout << "push success!" << endl; } else { cout << "push failed!" << endl; } } else if (opr == 1) { if (stack.pop()) { cout << "pop success!" << endl; } else { cout << "pop failed!" << endl; } } else if(opr==2){ cout<<stack.top()<<endl; } } return 0;}
0 0
- 栈的基本操作
- 栈的基本操作
- 栈 的基本操作。
- 栈的基本操作
- 栈的基本操作
- 栈的基本操作
- 栈的基本操作
- 栈的基本操作
- 栈的基本操作
- 栈的基本操作
- 栈的基本操作
- 栈的基本操作
- 栈的基本操作
- 栈的基本操作
- 栈的基本操作
- 栈的基本操作
- 栈的基本操作
- 栈的基本操作
- eclipse中DDMS报错:你的主机中的软件中止了一个已建立的连接。
- how to run an android emulator in ubuntu 14.04
- Oracle 11gExtension Statistics and Expression Statistics
- 图的邻接表存储与深度优先遍历代码实现
- 框架 day38 SVN安装及使用,SSH练习项目CRM,环境搭建(myeclipse hibernate 反转引擎生成PO和映射)
- 栈的基本操作
- 11G新特性 -- Expression Statistics
- 正则表达式的学习之提取字符串中用“|”分割开的单词
- windows下盘符路径
- leetcode #41 in cpp
- 栈_数列反转
- Watch Face Complications
- Leetcode no. 200
- Deferred Statistics Publishing