美文网首页
数据结构(c++实现)--stack

数据结构(c++实现)--stack

作者: ustclcl | 来源:发表于2018-10-23 18:51 被阅读0次

    在首先给出了线性表的类,stack继承线性表,并提供top(),add(), delete()操作。

    #include <iostream>
    using namespace std;
    
    template<class T>
    class LinearList{
        public:
            LinearList(int MaxListSize = 10);
            ~LinearList() {delete [] element;}
            bool IsEmpty() const {return length == 0;}
            int Length() const{return length;}
            bool Find(int k, T& x) const;
            int Search(const T& x) const;
            LinearList<T>& Delete(int k,T& x);  //delete element k and return it to x.
            LinearList<T>& Insert(int k,const T& x);
            void Output(ostream& out) const;
        private:
            int length;
            int MaxSize;
            T* element;
    };
    
    //No Memory
    class NoMem{
        public:
            NoMem(){}
    };
    void my_new_handler()
    {
        throw NoMem();
    }
    new_handler Old_Handler_ = set_new_handler(my_new_handler);
    
    class OutOfBounds{
        public:
            OutOfBounds(){ cout << "Out Of Bounds!" << endl; }
    };
    
    template<class T>
    LinearList<T>::LinearList(int MaxListSize)
    {
        MaxSize = MaxListSize;
        element = new T[MaxSize];
        length = 0;
    }
    template<class T>
    bool LinearList<T>::Find(int k,T& x) const
    {
        if(k<1 || k>length) return false;
        x = element[k-1];
        return true;
    }
    template<class T>
    int LinearList<T>::Search(const T& x) const
    {
        for(int i = 0;i < length; i++)
            if(element[i] == x) return ++i;
        return 0;
    }
    template<class T>
    LinearList<T>& LinearList<T>::Delete(int k,T& x)
    {
        if(Find(k,x)){
            for(int i =k;i<length;i++)
                element[i-1] = element[i];
            length--;
            return *this;
        }
        else throw OutOfBounds();
    }
    template<class T>
    LinearList<T>& LinearList<T>::Insert(int k,const T& x)
    {
        if(k<0||k>length) throw OutOfBounds();
        if(length == MaxSize) throw NoMem();
        for(int i=length;i>k;i--)
            element[i]=element[i-1];
        element[k] = x;
        length ++;
        return *this;
    }
    template<class T>
    void LinearList<T>::Output(ostream& out) const
    {
        for(int i=0;i<length;i++)
            out << element[i] << " ";
        out << endl;
    }
    template <class T>
    ostream & operator<<(ostream& out,const LinearList<T>& x)
    {x.Output(out);return out;}
    
    template<class T>
    class Stack :public LinearList <T>{
    public:
        Stack(int MaxStackSize=10):LinearList<T> (MaxStackSize){}
        int Length() const
        {
            return LinearList<T>::Length();
        }
        bool IsEmpty() const
        {
            return LinearList<T>::IsEmpty();
        }
        T Top() const
        {
            if(IsEmpty()) throw OutOfBounds();
            T x;
            Find(Length(),x) ;
            return x;
        }
        Stack<T>& Add(const T& x)
        {
           LinearList<T>::Insert(Length(),x);
            return *this;
        }
        Stack<T>& Delete(T& x)
        {
            LinearList<T>::Delete(Length(),x);
            return *this;
        }
    };
    
    int main()
    {
        try{
            int x;
            LinearList<int> L(5);
            cout << "Length = " << L.Length() << endl;
            cout << "IsEmpty = " << L.IsEmpty() << endl;
            L.Insert(0,2).Insert(1,6);
            cout << "List is " << L << endl;
            cout << "IsEmpty = " << L.IsEmpty() << endl;
          //  L.Insert(7,3);
            L.Delete(1,x);
            cout << "Dlete: " << x <<"List is: " << L << endl;
            Stack<int> st;
            st.Add(5);
            cout << st.Length();
    
        }
        catch(...){
            cerr << "An exception has occurred" << endl;
        }
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:数据结构(c++实现)--stack

          本文链接:https://www.haomeiwen.com/subject/gkoazftx.html