1. 程式人生 > >數據結構(07)_隊列

數據結構(07)_隊列

MF city 技術分享 HR vpd oss RoCE 常用操作 cit

1. .隊列的概念和實現

1.1.隊列的概念

隊列是一種特殊的線性表,僅能在線性表的兩端進行操作。

  • -隊頭(front)取出數據元素的一端;
  • -隊尾(rear)插入數據元素的一端。
    隊列的特性:
    技術分享圖片
    隊列的常用操作:
    創建和銷毀;出隊和入隊;清空隊列;獲取隊首元素;獲取隊列長度
    技術分享圖片
    隊列聲明(接口):
    template < typename T >
    class Queue
    {
    public:
    virtual void enqueue(const T& e) = 0;
    virtual void dequeue() = 0;
    virtual T front() const = 0;
    virtual void clear() = 0;
    virtual int length() const = 0;
    };

    1.2.StaticQueu

    順序隊列的實現:
    技術分享圖片
    設計要點:
    類模板,使用原生數組作為隊列 存儲空間,使用模板參數決定隊列的最大容量;

    template < typename T, int N >
    class StaticQueue : public Queue<T>
    {
    protected:
    T m_space[N];
    int m_front;
    int m_rear;
    int m_length;
    public:
    StaticQueue()
    void enqueue(const T& e)
    void dequeue()
    T front() const
    void clear()
    int length() const
    int capacity() const
    };

    註意事項:
    StaticQueue實現要點:(循環計數法) 提高隊列操作的效率(本質上時循環隊列)
    關鍵操作:

  • 入隊:m_rear = (m_rear + 1) % N;
  • 出隊:m_front = (m_front + 1) % N;
    隊列狀態:
  • 隊空:(m_length == 0) && (m_front == m_rear)
  • 隊滿:(m_length == N) && (m_front == m_rear)
    StaticQueue最終實現:

    template < typename T, int N >
    class StaticQueue : public Queue<T>
    {
    protected:
    T m_space[N];
    int m_front;
    int m_rear;
    int m_length;
    public:
    StaticQueue()   //O(1)
    {
        m_front = 0;
        m_rear = 0;
        m_length = 0;
    }
    
    void enqueue(const T& e)   //O(1)
    {
        if(m_length < N)
        {
            m_space[m_rear] = e;
            m_rear = (m_rear + 1) % N;
            m_length++;
        }
        else
        {
            THROW_EXCEPTION(InvalidOperationException, "no space in current staticqueue...");
        }
    }
    
    void dequeue()   //O(1)
    {
        if(m_length > 0)
        {
            m_front = (m_front + 1) % N;
            m_length--;
        }
        else
        {
            THROW_EXCEPTION(InvalidOperationException, "no element in current staticqueue...");
        }
    }
    
    T front() const   //O(1)
    {
        if(m_length > 0)
        {
            return m_space[m_front];
        }
        else
        {
            THROW_EXCEPTION(InvalidOperationException, "no element in current staticqueue...");
        }
    }
    
    void clear()   //O(1)
    {
        m_front = 0;
        m_rear = 0;
        m_length = 0;
    }
    
    int length() const   //O(1)
    {
        return  m_length;
    }
    
    int capacity() const   //O(1)
    {
        return N;
    }
    
    bool is_empty()   //O(1)
    {
        return (m_length == 0) && (m_front == m_rear);
    }
    
    bool is_full()   //O(1)
    {
        return (m_length == N) && (m_front == m_rear);
    }
    };

    2.鏈式隊列

    2.1.LinkQueue

    順序隊列的缺陷:當數據為類類型時,StaticQueue的對象在創建時,會多次調用元素類型的構造函數,影響效率。所以我們采用鏈式存儲結構來實現隊列。
    技術分享圖片
    設計要點:
    1.類模板,繼承自抽象父類Queue;
    2.在內部使用鏈式結構實現元素的存儲
    3.只在鏈表的頭部和尾部進行操作。
    技術分享圖片
    LinkQueue聲明:

    template <typename T>
    class LinkQueue : public Queue<T>
    {
    protected:
    LinkList<T> m_list;
    public:
    LinkQueue(){}
    void enqueue(const T& e)    //O(n)
    void dequeue()      //O(1)
    T front() const //O(1)
    void clear()        //O(n)
    int length() const  //O(1)
    };

    LinkQueue最終實現:

    template <typename T>
    class LinkQueue : public Queue<T>
    {
    protected:
    LinkList<T> m_list;
    public:
    LinkQueue(){}
    
    void enqueue(const T& e)    //O(n)
    {
        m_list.insert(e);
    }
    
    void dequeue()      //O(1)
    {
        if(m_list.length() > 0)
        {
            m_list.remove(0);
        }
        else
        {
            THROW_EXCEPTION(InvalidOperationException, "no elemet in current LinkQueue...");
        }
    }
    
    T front() const //O(1)
    {
        if(m_list.length() > 0)
        {
            return m_list.get(0);
        }
        else
        {
            THROW_EXCEPTION(InvalidOperationException, "no elemet in current LinkQueue...");
        }
    }
    
    void clear()        //O(n)
    {
        while (m_list.length() > 0)
        {
            m_list.remove(0);
        }
    }
    
    int length() const  //O(1)
    {
        return m_list.length();
    }
    };

    LinkQueue中,入隊操作由於只能操作隊列的尾部(鏈表的最後位置),要進行遍歷操作,所以時間復雜度為O(n),可以使用雙向循環鏈表代替單鏈表來解決這個問題。

數據結構(07)_隊列