stack 结构的特点:
stack 是一种先进先出(First In Last Out) 的数据结构,它只有一个出口,stack 允许新增元素,移除元素,取的顶端的元素。
stack 以底部容器完成其所有的工作,而具有这种 “修改某物接口,形成另一种风貌”之性质者,称为 adapter (配接器)。所以 stack 往往不被称为 container ,而往往被称为 container adapter 。
#include <iostream>
#include<list>
using namespace std;
template<class T,class Sequence = list<T> >
class Stack
{
typedef typename Sequence :: value_type value_type;
typedef typename Sequence :: reference reference;
protected:
Sequence c;
public:
bool empty(){return c.empty();}
size_t size() {return c.size();}
reference top() {return *(--(c.end()));}
void push(const T& x) {return c.push_back(x);}
void pop(){return c.pop_back();}
public:
bool operator==(const Stack<T,list<T>> & x) {return c==x.c ;}
bool operator!=(const Stack<T,list<T>> & x) {return c!=x.c ;}
};
int main()
{
Stack<int > x;
x.push(3);
x.push(4);
x.push(5);
x.push(6);
x.push(7);
x.push(8);
cout<<x.top()<<endl;
x.pop();
cout<<x.top()<<endl;
x.pop();
cout<<x.top()<<endl;
x.pop();
cout<<x.top()<<endl;
}