#include<iostream>
using namespace std;
#define size 100
typedef struct stack
{
int data[size];
int top;
}stack;
//初始化顺序栈
void CreatStack(stack &s)
{
s.top = 0;
}
//判断栈是否为空
bool IsEmpty(stack s)
{
if (s.top < 0)
return 1;
else
return 0;
}
//判断栈是否已满
bool IsFull(stack s)
{
if (s.top == size)
return 1;
else
return 0;
}
//入栈
void PushStack(stack &s,int e)
{
if (s.top == size)
cout << "栈已满,无法插入" << endl;
else
s.data[s.top] = e;
s.top++;
}
//出栈
int PopStack(stack &s)
{
if (s.top < 0)
return 0;
else
return s.data[s.top];
s.top--;
}
int main()
{
//初始化栈
stack s;
CreatStack(s);
//判断栈状态
cout << "判断栈为满或为空:";
if (IsEmpty(s))
cout << "为空,"<<' ';
else
cout << "不为空,"<<' ';
if (IsFull(s))
cout << "为满," <<' ';
else
cout << "不为满," <<endl;
//元素入栈
cout << "输入元素个数:";
int n;
cin >> n;
cout << "输入元素:";
while (n--)
{
int e;
cin >> e;
PushStack(s, e);
}
//元素出栈
cout << "出栈顺序是:";
while (s.top--)
{
int e1 = PopStack(s);
cout << e1 << ' ';
}
cout << endl;
//出栈后判断是否为空
cout << "是否为空:";
if (IsEmpty(s))
cout << "为空," << ' ';
else
cout << "不为空," << ' ';
cout << endl;
return 0;
}