堆栈实现(C++)

用顺序结构(数组)与模板技术实现Stack如下:

View Code
const int MAXSTACK = 10;



template<class T>

class ZtkStack{

public:

    ZtkStack();

    bool empty() const;

    ErrorCode pop();

    ErrorCode top(T &item) const;

    ErrorCode push(const T &item);

private:

    int count;

    T entry[MAXSTACK];

};



template<class T>

ZtkStack<T>::ZtkStack():count(0)

{

}



template<class T>

bool ZtkStack<T>::empty() const

{

    bool outcome = true;

    if(count > 0)

        outcome = false;

    return outcome;

}



template<class T>

ErrorCode ZtkStack<T>::pop()

{

    ErrorCode outcome = success;

    if(count>0)

        --count;

    else

        outcome = underflow;

    return outcome;

}



template<class T>

ErrorCode ZtkStack<T>::push(const T &item)

{

    ErrorCode outcome = success;

    if(count < MAXSTACK)

        entry[count++] = item;

    else

        outcome = overflow;

    return outcome;

}



template<class T>

ErrorCode ZtkStack<T>::top(T &item) const

{

    ErrorCode outcome = success;

    if(count > 0)

        item = entry[count - 1];

    else

        outcome = underflow;

    return outcome;

}

 

资料:C++编译器不能支持对模板的分离式编译, http://blog.csdn.net/pongba/article/details/19130

你可能感兴趣的:(C++)