顺序栈的表示和实现
----------顺序栈的存储结构----------
#define MAXSIZE 100
typedef struct
{
SElemType *top; //栈顶指针
SElemType *base; //栈底指针
int stscksize; //栈可用的最大容量
}SqStack;
top指针始终指向栈顶元素的上一个位置
base指针始终指向栈底元素
①栈的初始化
Status InitStack(SqStack &S)
{
S.base=new SElemType[MAXSIZE];
if(!S.base) exit(ERROR);
S.top=S.base;
S.stacksize=MAXSIZE;
return OK;
}
②入栈
Status Push(SqStack &S,SElemType &e)
{
if(S.top-S.base==S.stacksize) return ERROR;
*S.top=e;
S.top++;
return OK;
}
③出栈
Status Pop(SqStack &S)
{
if(S.top==S.base) return ERROR;
S.top--; //栈顶元素减一,删除的实质是存储空间的覆盖
e=*S.top;
return OK;
}
④取栈顶元素
Status GetTop(SqStack &S)
{
if(S.top!=S.base)
return *(S.top-1); //返回栈顶元素,栈顶指针的值不变
}