background image

class sqstack:public stack<elemtype>

{
private:

elemtype elem[STACK_SIZE];

//存取栈元素值

int top;

//栈顶

 

public:

sqstack(){top=-1;}

//构造函数

~sqstack(){}

//析构函数

bool push(const elemtype &e)

//入栈

{

if(top==STACK_SIZE-1) return false; //栈满,入栈失败
else

{

//栈未满

 elem[++top]=e;

//栈顶指针加 1 后将 e 入栈

 return true;

//入栈成功

}

}

bool pop(elemtype &e)

//出栈

{

if(top==-1) return false;

//栈空,出栈失败

else
{

//栈不空

e=elem[top--];

//用 e 返回栈顶元素后将栈顶指针

减 1

return true;

//出栈成功

}

}

bool gettop(elemtype &e)const

//取栈顶元素

{

if(top==-1) return false;

//栈空,出栈失败

else
{

//栈不空

e=elem[top];

//用 e 返回栈顶元素

return true;

//出栈成功

}

}