顺序栈的c++实现

顺序栈的实现

#pragma once
const int stackSize = 1024;
template<typename T>
class seqStack
{
public:
    seqStack() { top = -1; }
    bool Empty() { return top == -1; }
    void Push(T x);//压栈
    T Pop();//删除顶层元素
    T GetTop();//获得顶层元素
private:
    T data[stackSize];
    int top;//栈顶指针
};
template<typename T>    
void seqStack<T>::Push(T x)
{
    if (top >= stackSize-1)
        throw "上溢";
    top++;
    data[top] = x;
}
template<typename T>
T seqStack<T>::Pop()
{
    T temp = data[top];
    top--;
    return temp;
}
template<typename T>
T seqStack<T>::GetTop()
{
    if (top<=-1)
        throw "栈为空";
    return data[top];
}

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