#include<iostream>
using namespace std;
template<class T>
class Stack
{
private:
int top;
int MaxTop;
T *stack;
public:
Stack(int MaxStackSize);
~Stack(){delete[] stack;}
int IsEmpty()const {return (top==-1)?1:0;}
int IsFull()const {return (top==MaxTop)?0:1;}
T Top();
Stack<T>&Add(const T&x);
Stack<T>&Delete(T&x);
};
template<class T>
Stack<T>::Stack(int MaxStackSize)
{
MaxTop=MaxStackSize-1;
stack=new T [MaxStackSize];
for(int i=0;i<=MaxTop;i++)
{
stack[i]= 0;
}
top=-1;
}
template<class T>
T Stack<T>::Top()
{
if(IsEmpty())
{
cout<<"empty";
exit(1);
}
else
{
return stack[top--];
}
}
template<class T>
Stack<T>&Stack<T>::Add(const T &x)
{
if(IsFull)
{
cout<<"Full";
exit(1);
}
Stack[++top]=x;
return *this;
}
template<class T>
Stack<T>&Stack<T>::Delete(T &x)
{
if(IsEmpty)
{
cout<<"empty";
exit(1);
}
x=stack[top--];
return *this;
}
int main()
{
Stack <int> S(10);
int i=S.Top();
cout<<i<<endl;
return 1;
}
怎么才能push一个数啊 ,我用S.Add(1);不行啊