栈的创建
Status InitStack(SqStack &S)
{
if(!S.base = (SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType)))
exit(OVERFLOW);
S.top = S.base;
S.stacksize = STACK_INIT_SIZE;
return OK;
}
栈的销毁
Status DestroyStack(SqStack &S)
{
free(S.base);
S.base = NULL;
S.top = NULL;
S.stacksize = 0;
return OK;
}
栈的清空
Status ClearStack(SqStack &S)
{
S.top = S.base;
return OK;
}
判断是否为空栈
Status StackEmpty(SqStack S)
{
if(S.top != S.base)
return FALSE;
else
return TRUE;
}
计算栈的长度
Status StackLength(SqStack S)
{
return S.top - S.base;
}
得到栈顶元素
Status GetTop(SqStack S,SElemType &e)
{
if(S.top > S.base)
{
e = *(S.top -1);
return OK;
}
else
return ERROR;
}
进栈
Status Push(SqStack &S,SElemType e)
{
if(S.top-S.base >= S.stacksize)
{
S.base = (SElemType *)relloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType)));
if(!S.base)
exit(OVERFLOW);
S.top = S.base + S.stacksize;
S.stacksize += STACKINCREMENT;
}
*(S.top)++ = e; //S.top为空值,当赋值e的值,top往上移一位
return OK;
}
出栈
Status Pop(SqStack &S,SElemType &e)
{
if(S.top == S.base)
return ERROR;
e = *--S.top; //S.top为空值,top下一个指针才有值
return OK;
}