算法导论—栈和队列

来源:互联网 发布:速卖通数据分析工具 编辑:程序博客网 时间:2024/06/11 06:22
#include <vector>#include <iostream>class UnderflowException { };using namespace std;template<typename Object>class stack{public:stack(int capacity=100):array(capacity),top(-1){ }stack(vector<Object> &vec):capacity(100),top(vec.size()-1){for(int i=0;i<vec.size();++i)array.push_back(vec[i]);}bool isEmpty() const {return -1==top;}void push(Object x){array[++top]=x;}Object pop(){if(isEmpty())throw UnderflowException{ };else{Object old=array[top--];return old;}}int size(){return top+1;}private:vector<Object> array;int capacity;int top;};int main(){vector<int> vec{1,2,3,4,5};stack<int> mystack{vec};mystack.push(6);mystack.push(10);while(mystack.size())cout<<mystack.pop()<<" ";cout<<endl;}

0 0
原创粉丝点击