#include<stdio.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(S.top == -1)
return false;
x = S.data[S.top--];
return true;
}
//读栈顶元素
bool GetTop(SqStack S,ElemType &x)
{
if(S.top == -1)
return false;
x = S.data[S.top];
return true;
}
void main()
{
ElemType topVal;
SqStack S;
InitStack(S);
Push(S,1);
Push(S,2);
Push(S,3);
GetTop(S,topVal);
for(int i=0;i<3;++i)
{
Pop(S,topVal);
printf("%d ",topVal);
}
printf("\n");
}
06-29
3090