学习C++——实现抽象数据类型——栈
来源:互联网 发布:中国消防网站域名 编辑:程序博客网 时间:2024/06/07 01:19
一、本例实现了一个比较简单的栈,用数组实现的栈空间。
- 创建栈空间
- 将数据项添加到栈顶
- 从栈顶弹出数据项
- 查看栈是否填满
- 查看栈是否为空
源代码
#ifndef STACK_H_#define STACK_H_typedef unsigned long Item;class Stack{private:enum { MAX = 10 };Item items[MAX];int top;public:Stack();~Stack();bool isempty() const;bool isfull() const;bool push(const Item & item);bool pop(Item & item);};#endif
#include "stack.h"/*Stack();~Stack();bool isempty() const;bool isfull() const;bool push(const Item & item);bool pop(Item & item);*/Stack::Stack(){top = 0;}bool Stack::isempty() const{if (top == 0)return true;elsereturn false;}bool Stack::isfull() const{if (top == MAX)return true;elsereturn false;}bool Stack::push(const Item & item){if(top < MAX){//top++;items[top++] = item;return true;}elsereturn false;}bool Stack::pop(Item & item){if(top > 0){item = items[--top];//top--;return true;}elsereturn false;}Stack::~Stack(){}
#include "stack.h"#include <iostream>#include <cctype>int main(){using namespace std;Stack st;char ch;unsigned long po;cout << "Enter A/a to add a new data," << "P/p to pop a data to po, or Q/q to quit.\n";while(cin >> ch && toupper(ch) != 'Q'){while(cin.get() != '\n')continue;if(!isalpha(ch)){cout << "\a";continue;}switch(ch){case 'A':case 'a':cout << "Add a new data to the stack:";cin >> po;if(st.isfull())cout << "The stack is full.\n";elsest.push(po);break;case 'P':case 'p':if(st.isempty()) cout << "The stack is empty,please push the data.\n"; else { st.pop(po); cout << "Po = " << po << endl; }break;}cout << "\nPlease enter A/a to add new data,enter P/p to pop a data.\n";}cout << "Bye.\n";cin.get();return 0;}
二、本例用动态数组实现栈
源代码
#ifndef STACK_H_#define STACK_H_typedef unsigned long Item;class Stack{private://enum { MAX = 10 };//Item items[MAX];int MAX;Item * pitem;int top;public:Stack();~Stack();bool isempty() const;bool isfull() const;bool push(const Item & item);bool pop(Item & item);};#endif
#include "stack.h"#include <iostream>using namespace std;/*Stack();~Stack();bool isempty() const;bool isfull() const;bool push(const Item & item);bool pop(Item & item);*/Stack::Stack(){top = 0;cout << "Please enter the MAX:";//输入栈的高度,也就是可以存数据的数目cin >> MAX;pitem = new Item[MAX];//动态分配内存空间,也就是分配栈空间}bool Stack::isempty() const{if (top == 0)return true;elsereturn false;}bool Stack::isfull() const{if (top == MAX)return true;elsereturn false;}bool Stack::push(const Item & item){if(top < MAX){//top++;pitem[top++] = item;return true;}elsereturn false;}bool Stack::pop(Item & item){if(top > 0){item = pitem[--top];//top--;return true;}elsereturn false;}Stack::~Stack(){delete [] pitem;//释放动态分配的内存}
#include "stack.h"#include <iostream>#include <cctype>int main(){using namespace std;Stack st;char ch;unsigned long po;cout << "Enter A/a to add a new data," << "P/p to pop a data to po, or Q/q to quit.\n";while(cin >> ch && toupper(ch) != 'Q'){while(cin.get() != '\n')continue;if(!isalpha(ch)){cout << "\a";continue;}switch(ch){case 'A':case 'a':cout << "Add a new data to the stack:";cin >> po;if(st.isfull())cout << "The stack is full.\n";elsest.push(po);break;case 'P':case 'p':if(st.isempty()) cout << "The stack is empty,please push the data.\n"; else { st.pop(po); cout << "Po = " << po << endl; }break;}cout << "\nPlease enter A/a to add new data,enter P/p to pop a data.\n";}cout << "Bye.\n";cin.get();return 0;}
0 0
- 学习C++——实现抽象数据类型——栈
- C++:实现LinkedList 学习抽象数据类型
- ADT黑盒设计——C的抽象数据类型的设计与实现
- 数据结构——抽象数据类型
- 抽象数据类型——表
- C语言学习——数据类型杂记
- C语言学习——数据类型
- 数据结构——1.2抽象数据类型
- 抽象数据类型——线性表
- C#—抽象方法
- ANSI-C面向对象编程翻译:第一章 抽象数据类型——信息隐藏
- C语言数据结构——串的定义及抽象数据类型
- 《Object-Oriented Programming With ANSI-C》之第一章(抽象数据类型—信息隐藏)
- 【c#】——数据类型
- C#——数据类型
- C#—数据类型
- C#—数据类型
- C#—数据类型
- Android createChooser方法源码简析
- 计算机视觉、机器学习相关领域论文和源代码
- 从屏幕底部弹出PopupWindow
- MyEclipse使用总结——MyEclipse10安装SVN插件
- LEETCODE: Insertion Sort List
- 学习C++——实现抽象数据类型——栈
- 【设计模式】初识
- .NET ORM框架
- Linux -- Nginx 负载均衡和反向代理
- Oracle spatial、openlayers、geoserver开发地理信息系统总结
- windows和linux下androidSDK是否通用?
- 黑马程序员——Java基础—字符串、StringBuffer
- 为ASP网站引用WCF后台
- u-boot分析(三)---boot命令实现以及内核的启动