简单静态栈实现
来源:互联网 发布:描述淘宝小铺的句子 编辑:程序博客网 时间:2024/06/16 10:12
简单栈的实现
</pre><pre>
//Stack.cppenum Error_code {SUCCESS, UNDERFLOW, OVERFLOW};typedef int Stack_entry;const int maxstack = 5; //使用小容量方便测试class Stack {public: Stack(); bool empty() const; //状态不可改 Error_code pop(); //为什么弹出的状态没有const??? Error_code top(Stack_entry& item) const; //就是说这个top不能调用pop(),只能调用const函数,例如empty() Error_code push(const Stack_entry &item);private: int count; Stack_entry entry[maxstack];};Stack::Stack() { count = 0;}bool Stack::empty() const { if (count == 0) { return true; } else { return false; }}Error_code Stack::pop() { if (count == 0) { return UNDERFLOW; } else { --count; return SUCCESS; }}Error_code Stack::push(const Stack_entry& item) { if (count >= maxstack) { return OVERFLOW; } else { entry[count] = item; ++count; return SUCCESS; }}Error_code Stack::top(Stack_entry &item) const { if (count == 0) { return UNDERFLOW; } else { item = entry[count-1]; //top()只返回当前顶端的值,count的值应该不变 return SUCCESS; }}
//main.cpp#include <iostream>#include "Stack.h"using namespace std;int main() { Stack myStack; int temp; myStack.push(1); myStack.push(3); myStack.push(4); myStack.push(8); myStack.push(2); //2,8,4,3,1 myStack.push(0); //2,8,4,3,1 myStack.top(temp); cout << temp << endl; myStack.pop(); //8,4,3,1 myStack.top(temp); cout << temp << endl; myStack.push(9); //9,8,4,3,1 myStack.top(temp); cout << temp << endl; myStack.pop(); //8,4,3,1 myStack.top(temp); cout << temp << endl; myStack.pop(); //4,3,1 myStack.pop(); //3,1 myStack.pop(); //1 myStack.pop(); //(empty) myStack.pop(); //(empty) myStack.top(temp); //输出为8,说明了其实top()函数最好还是要有Error_code的判断 cout << temp << endl; return 0;}
0 0
- 简单静态栈实现
- 一个静态数组实现的简单栈
- 简单实现伪静态
- 实现简单静态AOP
- 简单静态通讯录的实现
- PHP伪静态简单实现(一)
- PHP伪静态简单实现(二)
- PHP伪静态简单实现(三)
- PHP伪静态简单实现(四)
- PHP伪静态简单实现(五)
- PHP伪静态简单实现(六)
- PHP伪静态简单实现(七)
- PHP伪静态简单实现(八)
- 利用 Urlrewrite 简单实现伪静态
- 【其实很简单】C++实现静态连接
- 一个静态数组实现的简单队列
- C++ 实现简单的静态队列
- 静态邻接表的简单实现
- python error
- APP第一次使用会显示的页面,之后再次打开应用不会再显示
- Mysql在大型网站的应用架构演变
- 【编程之法习题】第一章 字符串
- SAP设置信贷控制范围有什么作用?
- 简单静态栈实现
- jquery autocomplete ajxa (json list)处理
- ASP.NET Excel数据导入数据库
- Android流式布局-FlowLayout
- 第14周项目1.3验证二叉排序树相关算法
- QT5自定义控件
- 浏览器兼容性问题
- com.android.dex.DexException: Multiple dex files define Lorg/apache/http/entity/mime/FormBodyPart;
- ld: crti.o: No such file