顺序栈的c++实现

来源:互联网 发布:用什么软件做内帐 编辑:程序博客网 时间:2024/06/06 01:19

顺序栈的实现
完整的代码和测试:https://github.com/yqtaowhu

const int stackSize = 100;template<typename T>class Stack{public:    Stack() { top = -1; }    bool Empty() { return top == -1; }    void Push(T x);    void Pop();    T GetTop();    int Size();private:    T data[stackSize];    int top;};template<typename T>void Stack<T>::Push(T x){    if (top >= stackSize - 1)        throw "out of range";    top++;    data[top] = x;}template<typename T>void Stack<T>::Pop() {    T temp = data[top];    top--;}template<typename T>T Stack<T>::GetTop() {    if (top <= -1)        throw "the stack is empty";    return data[top];}template<typename T>int Stack<T>::Size() {    return top+1;}
0 0
原创粉丝点击