结构体
typedef struct
{
Elemtype data[MAXSIZE];
int top;
}SqStack;
top初始为-1
初始化
void InitStack(SqStack &S)
{
S.top = -1;
}
入栈
bool Push(SqStack &S, Elemtype e)
{
if(S.top == MAXSIZE - 1)
return false;
S.data[++S.top] = e;
return true;
}
出栈
bool Pop(SqStack &S, Elemtype &e)
{
if(S.top == -1)
return false;
e = S.data[S.top--];
return true;
}
获得栈顶元素
bool GetTop(SqStack &S, Elemtype &e)
{
if(S.top == -1)
return false;
e = S.data[S.top];
return true;
}
top初始为0
初始化
void InitStack(SqStack &S)
{
S.top = 0;
}
入栈
bool Push(SqStack &S, Elemtype e)
{
if(S.top == MAXSIZE)
return false;
S.data[S.top++] = e;
return true;
}
出栈
bool Pop(SqStack &S, Elemtype &e)
{
if(S.top == 0)
return false;
e = S.data[--S.top];
return true;
}
获取栈顶元素
bool GetTop(SqStack &S, Elemtype &e)
{
if(S.top == 0)
return false;
e = S.data[S.top - 1];
return true;
}