栈(stack)的实现

栈的vector实现

#include<vector>
using namespace std;

template <class T,int capacity = 30>
class stack
{
public:
	stack()
	{
		pool.reserve(capacity);
	}
	void clear()
	{
		pool.clear();
	}
	bool isEmpty()const
	{
		return pool.empty();
	}
	T pop()
	{
		T el = pool.back();
		pool.pop_back();
		return el;
	}

	T& tpEL()
	{
		return pool.back();
	}
	void push(const T& el)
	{
		pool.push_back(el);
	}

private:
	vector<T> pool;
};

栈的链表实现

#include<list>
using namespace std;

template<class T>
class Lstack
{
public:
	Lstack(){
	}
	void clear()
	{
		lst.clear();
	}
	bool isEmpty() const
	{
		return lst.empty();
	}
	T& topEl()
	{
		return lst.back();
	}
	T pop()
	{
		T el = lst.back();
		lst.pop_back();
		return el;
	}
	void push(const T& el)
	{
		lst.push_back(el)
	}
private:
	list<T> lst;
};

 

posted @ 2019-03-19 15:51  鲁太师  阅读(76)  评论(0编辑  收藏  举报