基于王道数据结构:
#include <stdio.h>
#include <stdlib.h>
#define MaxSize 50
typedef int ElemType;
typedef struct{
ElemType data[MaxSize];//数组
int top;
}SqStack;
void InitStack(SqStack &S)
{
S.top=-1;//代表栈为空
}
bool StackEmpty(SqStack &S)
{
if(S.top==-1)
return true;
else
return false;
}
//入栈
bool Push(SqStack &S,ElemType x)
{
if(S.top==MaxSize-1)//数组的大小不能改变,避免访问越界
{
return false;
}
S.data[++S.top]=x;
return true;
}
//出栈
bool Pop(SqStack &S,ElemType &x)
{
if(-1==S.top)
return false;
x=S.data[S.top--];//后减减,x=S.data[S.top];S.top=S.top-1;
retur