数据结构之栈篇
来源:互联网 发布:淘宝优惠券名称怎么写 编辑:程序博客网 时间:2024/05/20 06:41
栈是一种特殊的线性表 其插入(也称为入栈和压栈)和删除(出栈和弹栈)操作都在痛一端进行,一端为栈顶 一端为栈底
#include"stack.h"#include"exception.h"#include<algorithm>#include<sstream>using namespace std;template<class T>class arrayStack :public stack < T >{public: arrayStack(int initialCapacity = 10);//构造函数 ~arrayStack(){ delete[] stack; }//析构函数 bool empty() const{ return stackTop == -1; } int size() const { return stackTop + 1; } void changeLengthld(T* &a, int oldLength, int newLength);//栈容量已满的情况下增加栈容量的容量 T & top() //返回栈顶元素 { if (stackTop == -1) { throw stackEmpty(); } else { return stack[stackTop]; } } void pop() //删除栈顶元素 { if (stackTop == -1) { throw stackEmpty(); } stack[stackTop--].~T(); //T的析构函数 等价于stackTop--; } void push(const T& theElement);private: int arrayLength; //栈容量 int stackTop;//栈顶 T *stack;//元素数组};template<class T>arrayStack<T>::arrayStack(int initialCapacity){ //构造函数 if (initialCapacity < 1) { ostringstream s; s << "InitialCapacity = " << initialCapacity << "must be > 0"; throw illegalParameterValue(s.str()); } arrayLength = initialCapacity; stackTop = -1; stack = new T[arrayLength];}template<class T>void arrayStack<T>::changeLengthld(T* &a, int oldLength, int newLength){ if (newLength < 0) throw illegalParameterValue("new length must be >= 0"); T* temp = new T[newLength]; //新数组 int number = min(oldLength, newLength); // 取小的数组进行复制 copy(a, a + number, temp); delete[] a; // 释放旧数组的长度 a = temp;}template<class T>void arrayStack<T>::push(const T &theElement){ if (stackTop == arrayLength - 1) {//栈容量已满 ,增加容量 changeLengthld(stack, arrayLength, 2*arrayLength); arrayLength *= 2; } //栈顶插入 stack[++stackTop] = theElement;}
template<class T>class stack{public: virtual ~stack(){}; virtual bool empty() const = 0; virtual int size() const = 0; virtual T& top() = 0; virtual void pop() = 0; virtual void push(const T& theElement) = 0;};
0 0
- 数据结构之栈篇
- 数据结构学习之栈篇
- C语言数据结构之栈篇
- java数据结构之栈
- 数据结构之顺序栈
- 数据结构之链栈
- 数据结构之栈
- 数据结构复习之【栈】
- 数据结构之栈
- 数据结构之栈
- 数据结构之栈实现
- 数据结构学习之栈
- 数据结构之栈
- 数据结构之栈
- 复习数据结构之栈
- 数据结构之栈
- 数据结构之栈
- 大话数据结构之栈
- MNIST tensorflow官方卷积网络示例
- ocupload影响的按钮无效的原因
- 网络请求框架封装(NetWorkUtils)
- 推荐几个常用的GIT托管平台
- Android进程间通讯
- 数据结构之栈篇
- Android StatFs类
- 设计模式-单列模式
- 软件架构设计 首发于 软件架构设计 写文章 大型软件架构设计
- 软件设计师考试:设计模式
- 让vs2010支持html5
- JDBC操作数据库的步骤
- myEclipse 导入 jquery包报错
- jquery下拉模糊搜索