顺序栈类

来源:互联网 发布:c语言 if break 编辑:程序博客网 时间:2024/06/04 01:29
/*顺序栈*/#include <iostream>#include <cstdio>using namespace std;template <class T>class SqStack{    private:        int maxlen;        T *elem;        int top;    public:        SqStack() { top=-1; }        SqStack( T *a,int n,int maxsize ):maxlen(maxsize)        {            elem=new T[maxlen];            for( int i=0;i<n;i++ )  elem[i]=a[i];            top=n-1;        }        bool push( T el );        bool empty();        bool pop();        T gettop();};template <class T>bool SqStack<T>::push( T el ){    if( top>=maxlen )   return false;    elem[++top]=el;    return true;}template <class T>bool SqStack<T>::pop(){    if( top==-1 ) return false;    top--;    return true;}template <class T>bool SqStack<T>::empty(){    if( top==-1 )   return true;    else return false;}template <class T>T SqStack<T>::gettop(){    if( top==-1 )   return false;    else return elem[top];}int main(){    char a[]="abcde";    SqStack <char>test( a,5,100 );    while( !test.empty() )    {        cout<<test.gettop()<<" ";        test.pop();    }    return 0;}