zoukankan      html  css  js  c++  java
  • 3.1.1 栈的实现

    ①C++实现顺序栈

    #include <iostream>
    #include <fstream>
    #include <cassert>
    using namespace std;
    ///顺序栈
    const int defaultSize=200;
    
    template<class T>
    class Stack
    {
    private:
        T* data;
        int maxSize;
        int top;
        void overProcess();
    
    public:
        Stack(int sz=defaultSize);
        Stack(const Stack<T>& s);
        Stack<T>& operator=(const Stack<T>& s);
        ~Stack();
    
        void Push(const T &x);
        bool Pop(T& x);
        bool GetTop(T &x)const;
        bool IsEmpty()const;
        bool IsFull()const;
    
        int GetSize()const;
        void MakeEmpty();
    
        friend ostream& operator<<(ostream& out, const Stack<T>& s)
        {
            for(int i=0;i<=s.top;i++)
                cout<< s.data[i]<<' ';
            cout<<endl;
            return out;
        }
    };
    
    template<class T>
    Stack<T>::Stack(int sz)
    {
        top = -1;
        maxSize = sz;
        data = new T[maxSize];
    }
    
    template<class T>
    Stack<T>::Stack(const Stack<T>& s)
    {
        maxSize = s.GetSize();
         s.GetTop(top);
       // cout<<top;
        data = new T[maxSize];
        for(int i=0; i<=top; i++)
        {
            data[i] = s.data[i];
        }
    
    }
    
    template<class T>
    Stack<T>& Stack<T>::operator=(const Stack<T>& s)
    {
        maxSize = s.GetSize();
        s.GetTop(top);
        data = new T[maxSize];
       // cout<<top;
        for(int i=0; i<=top; i++)
        {
            data[i] = s.data[i];
        }
    }
    
    template<class T>
    Stack<T>::~Stack()
    {
        delete []data;
    }
    
    template<class T>
    void Stack<T>::overProcess()
    {
        T* newdt = new T[maxSize+defaultSize];
        for(int i=0; i<=top; i++)
        {
            newdt[i] = data[i];
        }
        maxSize += defaultSize;
        delete []data;
        data = newdt;
    }
    
    template<class T>
    void Stack<T>::Push(const T &x)
    {
        if(IsFull() == true)
            overProcess();
        top++;
        data[top] = x;
    }
    
    template<class T>
    bool Stack<T>::Pop(T& x)
    {
        if(IsEmpty() == true)
            return false;
        x = data[top];
        top--;
        return true;
    }
    
    template<class T>
    bool Stack<T>::GetTop(T &x)const
    {
        if(top == -1)
            return false;
        x = top;
        return true;
    }
    
    template<class T>
    bool Stack<T>::IsEmpty()const
    {
        if(top == -1)
            return true;
        return false;
    }
    
    template<class T>
    bool Stack<T>::IsFull()const
    {
        if(top == maxSize-1)
            return true;
        return false;
    }
    
    template<class T>
    int Stack<T>::GetSize()const
    {
        return top+1;
    }
    
    template<class T>
    void Stack<T>::MakeEmpty()
    {
        top = -1;
    }
    
    int main()
    {
        Stack<int> sta;
        ifstream fin("data.txt");
        assert(fin);
        int data;
        while (!fin.eof())
        {
            assert(fin >> data);
            sta.Push(data);
        }
    
        cout << "The initial Stack in the file is:
    " << sta;
        cout << "The current size of the Stack is: " << sta.GetSize() << endl;
        sta.GetTop(data);
        cout << "The current Top of the Stack is : " << data << endl;
        cout << endl;
        Stack<int> s=sta;
        cout<<"运算符=重载:"<<s<<endl;
        int x;
        sta.Pop(x);
        cout << "
    Do a Pop operation, then the stack is:
    " << sta << endl;
        cout << "the pop number is:" << x << endl;
    
        sta.GetTop(data);
        cout << "The current Top of the Stack is : " << data << endl;
    
        cout << "
    Test the state of the stack:
    ";
        if (sta.IsEmpty())
            cout << "The stack is empty now!
    ";
        else if (sta.IsFull())
            cout << "The stack is full now!
    ";
        else
            cout << "The stack is not empty and not full now!
    ";
        cout << "Now make the stack empty, then the state of the stack is:
    ";
    
    
    
        sta.MakeEmpty();
        if (sta.IsEmpty())
            cout << "The stack is empty now!
    ";
        else if (sta.IsFull())
            cout << "The stack is full now!
    ";
        else
            cout << "The stack is not empty and not full now!
    ";
    
    
    
        return 0;
    }

    #运行结果#

     ##include<assert.h>

    用法:assert(data != NULL);

    使用了一种断言机制:若()里的满足条件,则继续执行后续的语句;否则出错处理,终止程序运行

    优点:语句简洁,逻辑清晰

    ②C++实现链式栈

    #include <iostream>
    #include <fstream>
    #include <cassert>
    
    using namespace std;
    
    template <typename T>
    struct StackNode
    {
        T data;
        StackNode<T> *link;
        StackNode(T d = 0, StackNode<T> *next = NULL):link(next),data(d) {}
    };
    
    template <typename T>
    class LinkedStack
    {
    private:
        StackNode<T> *top;
    public:
        LinkedStack():top(NULL){}
        ~LinkedStack();
        void Push(const T &x);
        bool Pop(T &x);
        bool GetTop(T &x)const;
        int GetSize()const;
        bool IsEmpty()const;
        bool IsFull()const;
        void MakeEmpty();
        friend ostream& operator << (ostream &os, const LinkedStack<T> &s)
        {
            StackNode<T>* t = s.top;
            while(t != NULL)
            {
                cout<<t->data<<' ' ;
                t=t->link;
            }
            cout<<endl;
        }
    };
    
    template <typename T>
    LinkedStack<T>::~LinkedStack()
    {
        MakeEmpty();
    }
    
    template <typename T>
    void LinkedStack<T>::Push(const T &x)
    {
        top = new StackNode<T>(x,top);
    }
    
    template <typename T>
    bool LinkedStack<T>::Pop(T& x)
    {
        if(IsEmpty() == true)
            return false;
        StackNode<T>* t = top;
        top = top->link;
        x = t->data;
        delete t;
        return true;
    }
    
    template <typename T>
    bool LinkedStack<T>::GetTop(T &x)const
    {
        if(IsEmpty() == true)
            return false;
        x = top->data;
        return true;
    }
    
    template <typename T>
    int LinkedStack<T>::GetSize()const
    {
        StackNode<T>* t = top;
        int s=0;
        while(t!=NULL)
        {
            t = t->link;
            s++;
        }
        return s;
    }
    
    template <typename T>
    bool LinkedStack<T>::IsEmpty()const
    {
        if(top == NULL)
            return true;
        return false;
    }
    
    template <typename T>
    bool LinkedStack<T>::IsFull()const
    {
        return false;
    }
    
    template <typename T>
    void LinkedStack<T>::MakeEmpty()
    {
        StackNode<T>* t;
        while(top!=NULL)
        {
            t = top;
            top = top->link;
            delete t;
        }
    }
    
    
    int main()
    {
        LinkedStack<int> sta;
        ifstream fin("data.txt");
        assert(fin);
        int data;
    
        while (!fin.eof())
        {
            assert(fin >> data);
            sta.Push(data);
        }
    
        cout << "The initial Stack in the file is:
    " << sta;
        cout << "The current size of the Stack is: " << sta.GetSize() << endl;
    
        sta.GetTop(data);
        cout << "The current Top of the Stack is : " << data << endl;
    
        int x;
        sta.Pop(x);
        cout << "
    Do a Pop operation, then the stack is:
    " << sta << endl;
        cout << "The data popped is: " << x << endl;
        sta.GetTop(data);
        cout << "The current Top of the Stack is : " << data << endl;
    
        cout << "
    Test the state of the stack:
    ";
        if (sta.IsEmpty())
            cout << "The stack is empty now!
    ";
        else if (sta.IsFull())
            cout << "The stack is full now!
    ";
        else
            cout << "The stack is not empty and not full now!
    ";
    
        sta.MakeEmpty();
        cout << "Now make the stack empty, then the state of the stack is:
    ";
        if (sta.IsEmpty())
            cout << "The stack is empty now!
    ";
        else if (sta.IsFull())
            cout << "The stack is full now!
    ";
        else
            cout << "The stack is not empty and not full now!
    ";
        return 0;
    }

    #运行结果#

  • 相关阅读:
    I NEED A OFFER!
    水题 Codeforces Round #303 (Div. 2) A. Toy Cars
    模拟 HDOJ 5099 Comparison of Android versions
    模拟 HDOJ 5095 Linearization of the kernel functions in SVM
    贪心 HDOJ 5090 Game with Pearls
    Kruskal HDOJ 1863 畅通工程
    Kruskal HDOJ 1233 还是畅通工程
    并查集 HDOJ 1232 畅通工程
    DFS/并查集 Codeforces Round #286 (Div. 2) B
    水题 Codeforces Round #286 (Div. 2) A Mr. Kitayuta's Gift
  • 原文地址:https://www.cnblogs.com/syzyaa/p/13763650.html
Copyright © 2011-2022 走看看