#ifndef _SQSTACK_H_
#define _SQSTACK_H_
#include <iostream>
using namespace std;
enum StatusCode{SUCCESS,RANGE_ERROR,OVER_FLOW,UNDER_FLOW};
const int DEFAULT_SIZE=10;
template<class ElemType>
class SqStack
{
protected:
int count;
int maxSize;
ElemType *elems;
void Init(int size);
bool Full()const;
public:
SqStack(int size=DEFAULT_SIZE);
virtual ~SqStack();//注意虚构函数要是虚函数
bool Empty()const;
void Clear();
int Length()const;
void Traverse(void(* visit)(ElemType &e))const;
StatusCode Push(const ElemType &e);
StatusCode Top(ElemType &e)const;
StatusCode Pop(ElemType &e);
SqStack(const SqStack<ElemType> ©);
SqStack<ElemType>& operator = (const SqStack<ElemType> ©);
};
template<class ElemType>
SqStack<ElemType>::SqStack(int size)
{
elems=NULL;
Init(size);
}
template<class ElemType>
SqStack<ElemType>::~SqStack()
{
delete []elems;
}
template<class ElemType>
void SqStack<ElemType>::Clear()
{
count=0;
}
template<class ElemType>
void SqStack<ElemType>::Init(int size)
{
maxSize=size;
count=0;
if(elems!=NULL)
delete []elems;
elems=new ElemType[maxSize];
}
template<class ElemType>
bool SqStack<ElemType>::Full()const
{
return count==maxSize;
}
template<class ElemType>
bool SqStack<ElemType>::Empty()const
{
return count==0;
}
template<class ElemType>
int SqStack<ElemType>::Length()const
{
return count;
}
template<class ElemType>
StatusCode SqStack<ElemType>::Pop(ElemType &e)
{
if(Empty())
return UNDER_FLOW;
else
{
e=elems[count-1];
--count;
return SUCCESS;
}
}
template<class ElemType>
StatusCode SqStack<ElemType>::Push(const ElemType &e)
{
if(Full())
return OVER_FLOW;
else
{
elems[count++]=e;
return SUCCESS;
}
}
template<class ElemType>
StatusCode SqStack<ElemType>::Top(ElemType &e)const
{
if(Empty())
return UNDER_FLOW;
else
{
e=elems[count-1];
return SUCCESS;
}
}
template<class ElemType>
SqStack<ElemType>::SqStack(const SqStack<ElemType> ©)
{
elems=NULL;
Init(copy.maxSize);
for(int i=0;i<copy.count;i++)
elems[i]=copy.elems[i];
}
template<class ElemType>
SqStack<ElemType>& SqStack<ElemType>::operator = (const SqStack<ElemType> ©)
{
if(this=©)
{
elems=NULL;
Init(copy.maxSize);
for(int i=0;i<copy.count;i++)
elems[i]=copy.elems[i];
}
return *this;
}
#endif
#include "SqStack.h"
#include <iostream>
using namespace std;
template<class ElemType>
void Reserve()
{
int n,e;
SqStack<int> tmps;
cout<<"input an integer:";
cin>>n;
while(n<=0)
{
cout<<"the integer cann't be negative,please input n again:";
cin>>n;
}
cout<<"please input n integers:";
for(int i=0;i<n;i++)
{
cin>>e;
tmps.Push(e);
}
cout<<"output on the opposite direction:";
while(!tmps.Empty())
{
tmps.Pop(e);
cout<<e<<" ";
}
}
void main()
{
Reserve<int>();
}