#include <iostream>
using namespace std;
class SqStack
{
private:
enum { MaxSize = 200 };
int data[MaxSize];
int top;
public:
SqStack(){top = 0;}
~SqStack(){};
bool isEmpty(); //判断栈是否已空
void pushInt(int x);//入栈
int popInt(); //退栈
int getTop(); //得到栈顶元素
void display(); //打印栈中元素
};
bool SqStack::isEmpty() //判断栈为空
{
return (top == 0);
}
void SqStack::pushInt(int x) //元素进栈
{
if(top == MaxSize - 1)
{
cout << "栈上溢出!" << endl;
}
else
{
data[top] = x;
++top;
}
}
int SqStack::popInt() //退栈
{
int tmp = 0;
if(top == 0)
{
cout << "栈已空!" << endl;
}
else
{
tmp = data[top];
top--;
}
return tmp;
}
int SqStack::getTop() //获得栈顶元素
{
int tmp=0;
if(top == 0)
{
cout << "栈空!" << endl;
}
else
{
tmp = data[top - 1];
}
return tmp;
}
void SqStack::display() //打印栈里元素
{
cout << "栈中元素:" << endl;
for(int number = 0; number < top; number++)
{
cout << data[number] << endl;
}
}
int main()
{
SqStack st;
cout << "开始栈空:" << st.isEmpty() << endl;
cout << "入栈" << endl;
for(int i = 1; i < 50; i++)
st.pushInt(i);
st.display();
cout << "退栈:" << endl;
cout << "栈顶元素: " << st.getTop() << endl;
st.popInt();
for(i = 1; i < 50; i++)
cout << st.popInt() << endl;
return 0;
}