};
template<class elemtype>
class linkstack:public stack<elemtype>
{
private:
node<elemtype>*top;
//栈顶指针
public:
linkstack(){top=NULL;}
//构造函数
~linkstack()
//析构函数
{
elemtype e;
//临时变量
while(top!=NULL) pop(e);
//出栈,直到栈为空
}
bool push(const elemtype &e)
//入栈
{
top=new node<elemtype>(e,top);
//以 e 为数据值,top 指向下一
结点构造新节点
return true;
//入栈成功
}
bool pop(elemtype &e)
//出栈
{
if(top==NULL) return false;
//栈空,出栈失败
else
{
//栈不空
e=top->data;
//用 e 返回栈顶元素
node<elemtype>*p=top;
//暂存栈顶
top=top->next;
//top 指向下一结点
delete p;
//释放原栈顶
return true;
//出栈成功
}
}
bool gettop(elemtype &e)const
//取栈顶元素
{
if(top==NULL) return false;
//栈空,出栈失败
else
{
//栈不空
e=top->data;
//用 e 返回栈顶元素