#include <iostream>
using namespace std;
#define max 5
template<typename T>
class stack
{
public:
stack();
bool pop(T &m); //将出栈的元素给m
bool push(T n);//将元素n进栈
void print();
private:
T a[max];
int top;
};
template<typename T>
stack<T>::stack()
{
top=-1;
}
template<typename T>
bool stack<T>::push(T n)
{
if(top==max-1)
{
cout<<"栈已满,操作失败!"<<endl;
return false;
}
else
{
++top;
a[top]=n;
return true;
}
}
template <typename T>
bool stack<T>::pop(T &m)
{
if(top==-1)
{
cout<<"栈已空,无法再出栈"<<endl;
return false;
}
else
{
m=a[top];
--top;
return true;
}
}
template <typename T>
void stack<T>::print()
{
for(int i=top;i>=0;i--)
cout<<a[i]<<endl;
}
void main()
{
int e=0;
stack<int> test;
for(int i=0;i<max;i++)
test.push(i);
for(int i=0;i<max;i++)
test.pop(e);
test.push(10);
test.print();
getchar();
}