栈(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;
};

猜你喜欢

转载自blog.csdn.net/Dashi_Lu/article/details/88664015