• C++ 顺序栈基本算法实现


    C++ 顺序栈基本算法

    #ifndef SeqStack_h
    #define SeqStack_h
    #include <iostream>
    using namespace std;
    const int StackSize = 1024;
    template <class T>
    class SeqStack{
    public:
        SeqStack(){top = -1;}
        SeqStack(T a[], int n);
        void Push(T  x);
        T Pop();
        T GetTop();
        bool Empty();
        int GetLength();
        void PrintSeqStack();
    private:
        T data[StackSize];
        int top;
    };
    template<class T>
    SeqStack<T>::SeqStack(T a[], int n){
        top = -1;
        if(n>StackSize) throw "上溢";
        for(int i = 0;i< n;i++){
            data[i] = a[i];
            top++;
        }
    }
    template <class T>
    bool SeqStack<T>::Empty(){
        if(top <0) return true;
        else  return false;
    }
    template <class T>
    void SeqStack<T>::Push(T x){
        if(top >= StackSize-1) throw "上溢 ";
        top ++;
        data[top] = x;
    }
    template <class T>
    T SeqStack<T>::Pop(){
        if(Empty()) throw "下溢";
        top--;
        return data[top+1];
    }
    template <class T>
    T SeqStack<T>::GetTop(){
        if(Empty()) throw "下溢";
        return data[top];
    }
    template <class T>
    int SeqStack<T>::GetLength(){
        return top+1;
    }
    
    template <class T>
    void SeqStack<T>::PrintSeqStack(){
        cout<<"入栈元素顺序依次是:"<<endl;
        for(int i = 0;i<=top;i++){
            cout<< data[i]<< " ";
        }
        cout << endl;
    }
    
    #endif /* SeqStack_h */
  • 相关阅读:
    [转]老男孩读pcie
    nand
    面试题目汇总
    redis的tcp-backlog配置
    Redis 3.0.4 sentinels
    Redis 3.0.4 客户端
    Redis 3.0.4 事件驱动
    Redis 3.0.4 AOF持久化
    Redis 3.0.4 数据库
    Redis 3.0.4 RDB持久化
  • 原文地址:https://www.cnblogs.com/ycbeginner/p/10006387.html
Copyright © 2020-2023  润新知