栈(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;
};
天上我才必有用,千金散尽还复来!