#include"stdafx.h"
#include<iostream>
using namespace std;
constexpr auto M = 5;//最多存放
int Stack[M];//顺序栈
int top;//栈顶
void InitiaStack(int &top) {//初始化
top =-1;
cout<<"初始化了一个空顺序栈"<<endl;
}
int Empty(int top) {//检查栈是否为空
return top == -1;
}
int Full(int top) {//检查栈满
return top == M - 1;
}
int GetTop(int Stack[],int &top) {//获取栈顶
int item;
if (Empty(top))
{
cout << "空栈" << endl;
return 0;
}
else
{
item = Stack[top];
cout << "当前栈顶元素为" << item<<endl;
return 1;
}
}
int Push(int Stack[], int &top, int item) {
if (Full(top))
{
cout << "当前栈空间不足,请结束进栈操作" << endl;
return 0;
}
else
{
Stack[++top] = item;//进栈,修改栈顶
cout << "当前进栈元素为:" << item << endl;
return 1;
}
}
int Pop(int Stack[], int &top) {
int item;
if (Empty(top))
{
return 0;
}
else
{
item = Stack[top--];//出栈,修改栈顶
cout << item << "出栈成功" << endl;
return 1;
}
}
int main() {
InitiaStack(top);
int item;
cout << "请输入进栈元素,以0结束" << endl;
while (true)
{
cin >> item;
if (item)
{
Push(Stack, top, item);
}
else
{
break;
}
}
Pop(Stack, top);
GetTop(Stack, top);
return 0;
}