#include<iostream>
#include<stdlib.h>
using namespace std;
const int maxn = 2;
template <typename T>
class Stack
{
private:
int size_;//大小
T *base;//只想栈底
T *top;//top指针指向的是栈顶元素的下一个位置
public:
void Init()
{
base=new T[maxn];
top=base;
size_=maxn;
return;
}//初始化
int Empty()
{
if(base==top)
return 1;
return 0;
}//判断栈是否为空
void Destroy()
{
delete[]base;
top=base;
size_=0;
}
T Top()
{
return *(top-1);
}//返回栈顶元素
void Pop(){
top--;
}//将栈顶元素弹出
void Push(T num)
{
if(top-base>=size_)
{
base=(T*)realloc(base,(size_+maxn)*sizeof(T));//栈的大小需要增加时,使用realloc
top=base+size_;
size_+=maxn;
}
*(top++)=num;
return;
}//新元素进栈
void Clear()
{
base=NULL;
top=base;
}
int Length()
{
return top-base;
}//返回栈的大小
};
int main()
{
Stack<int> a;
a.Init();
for(int i=0;i<5;i++)
{
int num;
cin>>num;
a.Push(num);
cout<<a.Top()<<endl;
}
cout<<a.Length()<<endl;
for(int i=0;i<5;i++)
{
if(!a.Empty())
{
cout<<a.Top();
a.Pop();
}
else
cout<<"hehehe"<<endl;
}
}