这是一个普通的链表式栈模板。
注:1、编译测试环境(vs2005+boost);
2、其中可以稍作修改(注释掉部分异常代码),使之不依赖于boost。
==================================================================
#ifndef _STACK_H_
#define _STACK_H_
#include <boost/throw_exception.hpp>
#define EN_TEST_STACK
#ifdef EN_TEST_STACK
#include <iostream>
#include <string>
#endif
namespace redwolf
{
template <typename T> class stack;
template<typename T>
class stack_node
{
public:
protected:
private:
stack_node(const T& item=T()):element(item), _n(NULL){}
T element;
stack_node* _n;
friend class stack<T>;
};
template <typename T>
class stack
{
public:
//构造0
stack():_t(NULL){}
//构造0
stack(const T& item):_t(new stack_node<T>(item)){}//no throw
//拷贝构造
stack(const stack<T>& other):_t(NULL){
copy(other._t, _t);
}
//析构
virtual ~stack(){
this->destroy();
}
//重载=
virtual stack& operator= (const stack<T>& rhs){
if(this!=&rhs)
{
this->destroy();
copy(rhs._t, _t);
}
return *this;
}
//压栈
virtual void push(const T& item){
stack_node<T>* _temp=new stack_node<T>(item);
if(_temp==NULL) boost::throw_exception("memory unenough.");
_temp->_n=_t;
_t=_temp;
}
//出栈
virtual T pop(){
stack_node<T>* _p=_t;
_t=_t->_n;
T item=_p->element;
delete _p;
_p=NULL;
return item;
}
//获得栈顶的元素引用
virtual T& top() const {
return _t->element;
}
//获取栈的大小
virtual int size() const {
stack_node<T>* _p=_t;
int i=0;
while(_p!=NULL)
{
i++;
_p=_p->_n;
}
return i;
}
//是否是空栈
virtual bool empty() const{
return (_t==NULL);
}
//合并两个同类型的栈,a.uinte(b),把a接在b的栈顶上
virtual void uinte(const stack<T>& other){
stack_node<T>* _bottom=_t, *_p=_t;
while(_p!=NULL)
{
_bottom=_p;
_p=_p->_n;
}
if(_bottom==NULL)//空栈
copy(other._t, _bottom);
else
copy(other._t, _bottom->_n);
}
//销毁栈
virtual void destroy(){
destroy(_t);
}
protected:
//销毁一个栈,可以用递归,但考虑到效率,尽量回避
virtual void destroy(stack_node<T>*& top){
stack_node<T>* _temp=top;
while(top!=NULL)
{
_temp=top;
top=top->_n;
delete _temp;
}
}
//copy一个栈,如何消除这个递归呢?
virtual void copy(const stack_node<T>* _top, stack_node<T>*& top){
if(top!=NULL)
boost::throw_exception("unempty destin stack");
if(_top->_n!=NULL)
{
top=new stack_node<T>(_top->element);
if(_top==NULL) boost::throw_exception("memory unenough.");
copy(_top->_n, top->_n);
}
else
{
top=new stack_node<T>(_top->element);
if(_top==NULL) boost::throw_exception("memory unenough.");
}
}
private:
stack_node<T>* _t;//top
};
#ifdef EN_TEST_STACK
void stacktest0()
{
using namespace std;
stack<string> s0,s1,s2,s3,s4,s5;
stack<string> s6("redwolf");
s0=s1=s2=s3=s4=s5=s6;
cout<<s0.pop()<<endl;
cout<<s0.empty()<<endl;
cout<<s1.top()<<endl;
cout<<s1.empty()<<endl;
s0.push("http");
s0.push("ftp");
s0.push("rtsp");
cout<<s0.size()<<endl;
int cnt=s0.size();
/*for(int i=0; i<cnt; i++)
{
cout<<"top: "<<s0.top()<<endl;
cout<<"pop: "<<s0.pop()<<endl;
}*/
s1.push("yuxiongjian");
s1.push("study");
s1.push("std...boost");
s0.uinte(s1);
cout<<s0.size()<<endl;
cnt=s0.size();
/*for(int i=0; i<cnt; i++)
{
cout<<"top: "<<s0.top()<<endl;
cout<<"pop: "<<s0.pop()<<endl;
}*/
s1.uinte(s0);
stack<string> s7(s1);
cout<<s1.size()<<endl;
cnt=s7.size();
stack<string> s8=s7;
for(int i=0; i<cnt; i++)
{
cout<<"top: "<<s7.top()<<endl;
cout<<"pop: "<<s7.pop()<<endl;
}
s2.size();
s2=s1;
s2.size();
s8.size();
cnt=s8.size();
for(int i=0; i<cnt; i++)
{
cout<<"top: "<<s8.top()<<endl;
cout<<"pop: "<<s8.pop()<<endl;
}
}
#endif
};
#endif
#define _STACK_H_
#include <boost/throw_exception.hpp>
#define EN_TEST_STACK
#ifdef EN_TEST_STACK
#include <iostream>
#include <string>
#endif
namespace redwolf
{
template <typename T> class stack;
template<typename T>
class stack_node
{
public:
protected:
private:
stack_node(const T& item=T()):element(item), _n(NULL){}
T element;
stack_node* _n;
friend class stack<T>;
};
template <typename T>
class stack
{
public:
//构造0
stack():_t(NULL){}
//构造0
stack(const T& item):_t(new stack_node<T>(item)){}//no throw
//拷贝构造
stack(const stack<T>& other):_t(NULL){
copy(other._t, _t);
}
//析构
virtual ~stack(){
this->destroy();
}
//重载=
virtual stack& operator= (const stack<T>& rhs){
if(this!=&rhs)
{
this->destroy();
copy(rhs._t, _t);
}
return *this;
}
//压栈
virtual void push(const T& item){
stack_node<T>* _temp=new stack_node<T>(item);
if(_temp==NULL) boost::throw_exception("memory unenough.");
_temp->_n=_t;
_t=_temp;
}
//出栈
virtual T pop(){
stack_node<T>* _p=_t;
_t=_t->_n;
T item=_p->element;
delete _p;
_p=NULL;
return item;
}
//获得栈顶的元素引用
virtual T& top() const {
return _t->element;
}
//获取栈的大小
virtual int size() const {
stack_node<T>* _p=_t;
int i=0;
while(_p!=NULL)
{
i++;
_p=_p->_n;
}
return i;
}
//是否是空栈
virtual bool empty() const{
return (_t==NULL);
}
//合并两个同类型的栈,a.uinte(b),把a接在b的栈顶上
virtual void uinte(const stack<T>& other){
stack_node<T>* _bottom=_t, *_p=_t;
while(_p!=NULL)
{
_bottom=_p;
_p=_p->_n;
}
if(_bottom==NULL)//空栈
copy(other._t, _bottom);
else
copy(other._t, _bottom->_n);
}
//销毁栈
virtual void destroy(){
destroy(_t);
}
protected:
//销毁一个栈,可以用递归,但考虑到效率,尽量回避
virtual void destroy(stack_node<T>*& top){
stack_node<T>* _temp=top;
while(top!=NULL)
{
_temp=top;
top=top->_n;
delete _temp;
}
}
//copy一个栈,如何消除这个递归呢?
virtual void copy(const stack_node<T>* _top, stack_node<T>*& top){
if(top!=NULL)
boost::throw_exception("unempty destin stack");
if(_top->_n!=NULL)
{
top=new stack_node<T>(_top->element);
if(_top==NULL) boost::throw_exception("memory unenough.");
copy(_top->_n, top->_n);
}
else
{
top=new stack_node<T>(_top->element);
if(_top==NULL) boost::throw_exception("memory unenough.");
}
}
private:
stack_node<T>* _t;//top
};
#ifdef EN_TEST_STACK
void stacktest0()
{
using namespace std;
stack<string> s0,s1,s2,s3,s4,s5;
stack<string> s6("redwolf");
s0=s1=s2=s3=s4=s5=s6;
cout<<s0.pop()<<endl;
cout<<s0.empty()<<endl;
cout<<s1.top()<<endl;
cout<<s1.empty()<<endl;
s0.push("http");
s0.push("ftp");
s0.push("rtsp");
cout<<s0.size()<<endl;
int cnt=s0.size();
/*for(int i=0; i<cnt; i++)
{
cout<<"top: "<<s0.top()<<endl;
cout<<"pop: "<<s0.pop()<<endl;
}*/
s1.push("yuxiongjian");
s1.push("study");
s1.push("std...boost");
s0.uinte(s1);
cout<<s0.size()<<endl;
cnt=s0.size();
/*for(int i=0; i<cnt; i++)
{
cout<<"top: "<<s0.top()<<endl;
cout<<"pop: "<<s0.pop()<<endl;
}*/
s1.uinte(s0);
stack<string> s7(s1);
cout<<s1.size()<<endl;
cnt=s7.size();
stack<string> s8=s7;
for(int i=0; i<cnt; i++)
{
cout<<"top: "<<s7.top()<<endl;
cout<<"pop: "<<s7.pop()<<endl;
}
s2.size();
s2=s1;
s2.size();
s8.size();
cnt=s8.size();
for(int i=0; i<cnt; i++)
{
cout<<"top: "<<s8.top()<<endl;
cout<<"pop: "<<s8.pop()<<endl;
}
}
#endif
};
#endif
==================================================================
转载于:https://blog.51cto.com/redwolf/88184