template<class Elem>
class AStack:public Stack<Elem> //数组实现的栈类
{

private:
int size;
int top;
Elem *listArray;
public:
AStack(int sz=DefaultListSize)
{
size=sz;top=0;listArray=new Elem[sz];
}
~AStack()
{
delete [] listArray;
}
void clear()
{
top=0;
}
bool push(const Elem& item)
{
if(top==size) return false;
else
{
listArray[top++]=item;
}
}
bool pop(Elem & it)
{
if(top==0) return false;
else
{
it=listArray[--top];
return true;
}
}
bool topValue(Elem & it) const
{
if(top==0) return false;
else
{
it=listArray[top-1];
return true;
}
}
int length() const
{
return top;
}
};



#include "LList.h"
template<class Elem>
//用链表实现的栈
class LStack:public Stack<Elem>

{
private:
Link<Elem> * top;
int size;
public:
LStack(int sz=DefaultListSize)
{
top=NULL;size=0;
}
~LStack()
{
clear();
}
void clear()
{
while(top!=NULL)
{
Link<Elem> *temp=top;
top=top->next;
size=0;
delete temp;
}
}
bool push(const Elem& item)
{
top=new Link<Elem>(item,top);
size++;
return true;
}
bool pop(Elem & it)
{
if(size==0) return false;
it=top->element;
Link<Elem> * ltemp=top->next;
delete top;
top=ltemp;
size--;
return true;
}
bool topValue(Elem &it) const
{
if(size==0) return false;
it=top->element;
return true;
}
int length() const
{
return size;
}
};
Live together,or Die alone!
原文地址:https://www.cnblogs.com/hzhida/p/2354739.html