📄 sqstack.cpp
字号:
#include <stdlib.h>
#include "ds.h"
#include "SqStack.h"
Status InitStack(SqStack &S)
{
S.base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!S.base) 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)
{
return (S.top==S.base);
}
///////////////////////////////////////////////////////////////
int StackLength(SqStack S)
{
return S.top-S.base;
}
///////////////////////////////////////////////////////////////
Status GetTop(SqStack S,SElemType &e)
{
if(S.top==S.base) return ERROR;
e=*(S.top-1);
return OK;
}
///////////////////////////////////////////////////////////////
Status Push(SqStack &S,SElemType e)
{
if(S.top-S.base>=S.stacksize)
{
S.base=(SElemType *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!S.base) exit(OVERFLOW);
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top++=e;
return OK;
}
///////////////////////////////////////////////////////////////
Status Pop(SqStack &S,SElemType &e)
{
if(S.top==S.base) return ERROR;
e=*--S.top;
return OK;
}
///////////////////////////////////////////////////////////////
Status StackTraverse(SqStack S,Status (*visit)(SElemType))
{
SElemType *bp;
bp=S.base;
while(bp!=S.top)
visit(*bp++);
return OK;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -