现在的位置: 首页 > 综合 > 正文

栈的实现

2014年02月28日 ⁄ 综合 ⁄ 共 621字 ⁄ 字号 评论关闭

1 栈的向量实现(利用vector向量)

#ifndef STACK

#define STACK

 

#include <vector>

 

template<class T, int caacity>

class stack

{

public:

stack()

{

vector1.reserve(capacity);

}

   void clear()

{

vector1.clear();

}

bool isempty()

{

return vector1.empty();

}

T &topel()

{

return vector1.back();//引用

}

T pop()

{

T ti=vector1.back();

vector1.pop_back();

return ti;

}

void push(const T &t1)

{

vector1.push_back(t1);

}

private:

vector vector1;

};

 

 

2 栈的链表实现

template<class T>

class lstack

{

public:

lstack()

{

 

}

void clear()

{

list1.clear()

}

bool isempty()

{

return list1.empty()

}

T &topel()

{

return list1.back()

}

T pop()

{

T el=list1.back();

list1.pop_back();

return el;

}

void push(T t1)

{

list1.push_back(t1);

}

 

private:

list<T>list1;

};

抱歉!评论已关闭.