简单数据结构的实现之顺序栈

#include <iostream>

using namespace std;

const int STACK_INIT_SIZE=100;

#ifndef SQSTACK_H_INCLUDED
#define SQSTACK_H_INCLUDED

template <class ElemType>
class SqStack
{
private:
    ElemType* base;
    ElemType* top;
public:
    SqStack();
    ~SqStack();
    void ClearStack();
    bool StackEmpty(){return top==base?true:false;}
    int StackLength(){return top-base;}
    ElemType* Base(){return base;}
    ElemType* Top(){return top;}
    bool GetTop(ElemType& e);
    bool Push(ElemType e);
    bool Pop(ElemType& e);
};
template <class ElemType>
SqStack<ElemType>::SqStack()
{
    base=new ElemType[STACK_INIT_SIZE];
    top=base;
}
template <class ElemType>
SqStack<ElemType>::~SqStack()
{
    delete []base;
    top=base=NULL;
}
template <class ElemType>
void SqStack<ElemType>::ClearStack()
{
    top=base;
}
template <class ElemType>
bool SqStack<ElemType>::GetTop(ElemType& e)
{
    if(top==base)
    {
        return false;
    }
    else
    {
        e=*(top-1);
        return true;
    }
}
template <class ElemType>
bool SqStack<ElemType>::Push(ElemType e)
{
    if(top-base>=STACK_INIT_SIZE)
    {
        return false;
    }
    else
    {
        *top=e;
        top++;
        return true;
    }
}
template <class ElemType>
bool SqStack<ElemType>::Pop(ElemType& e)
{
    if(top==base)
    {
        return false;
    }
    else
    {
        e=*(top-1);
        top--;
        return true;
    }
}

#endif // SQSTACK_H_INCLUDED

你可能感兴趣的:(数据结构,null,delete,Class,include)