#include<iostream>
#define MaxSize 50
//顺序栈的基本操作
using namespace std;
typedef int Elemtype;
typedef struct {
Elemtype data[MaxSize];
int top;
}SqStack;
void InitStack(SqStack &S);
bool StackEmpty(SqStack &S);
bool Push(SqStack &S, int x);
bool Pop(SqStack &S, Elemtype &x);
bool GetTop(SqStack S, Elemtype &x);
int main() {
SqStack S;
InitStack(S);
Push(S, 1);
Push(S, 2);
Push(S, 3);
int x=0;
//不加&是拷贝传递,只是值相同,地址不同。加上&后是引用传递,传递的是地址,函数操作后入参的值跟着改变,地址指向没变。
Pop(S, x);
cout << "出栈元素为:" << x << endl;
GetTop(S, x);
cout << "栈顶元素为:" << x << endl;
}
//初始化
void InitStack(SqStack &S) {
S.top = -1;
}
//判断栈是否为空
bool StackEmpty(SqStack &S) {
if (S.top == -1) {
return true;
}
return false;
}
//进栈
bool Push(SqStack &S,int x) {
if (S.top == MaxSize - 1) {
return false;
}
S.top++;
S.data[S.top] = x;
return true;
}
//出栈
bool Pop(SqStack &S,Elemtype &x) {
if (S.top == -1) {
return false;
}
x = S.data[S.top];
S.top--;
return true;
}
//读栈顶元素
bool GetTop(SqStack S, Elemtype &x) {
if (S.top == -1) {
return false;
}
x = S.data[S.top];
return true;
}