顺序栈类的声明
const int MAX_SIZE=100;
template <class T>
class seqStack
{
public:
seqStack ( ) ;
~seqStack ( );
void Push ( T x );
T Pop ( );
T GetTop ( );
bool Empty ( );
private:
T data[MAX_SIZE];
int top;
}
顺序栈的实现——入栈
template <class T>
void seqStack<T>::Push ( T x)
{
if (top==MAX_SIZE-1) throw “溢出”;
top++;
data[top]=x;
}
取栈顶
template <class T>
T seqStack<T>::GetTop ( )
{
if (Empty()) throw ”空栈” ;
return data[top];
}
出栈
template <class T>
T seqStack<T>:: Pop ( )
{
if (top==-1) throw “溢出”;
x=data[top];
top--;
return x;
}
清空
template <class T>
bool seqStack<T>::Empty ()
{
if (top==-1)
return true;
return false;
}