線性表數據結構解讀(四)隊列結構Queue

    在上一篇文章中,我們詳細介紹了棧結構,並結合Stack源碼進行了分析,相關文章大家可以點擊這裏回看我的博客:線性表數據結構解讀(三)棧結構Stack

隊列的定義

    隊列是一種插入和刪除分別在兩端進行操作的線性表,一端進行插入操作,一端進行刪除操作。

這裏寫圖片描述

隊列的特點

    我們把進入隊列端稱爲隊列的對尾,用rear表示;離開隊列的一端成爲隊列的頭,用front表示,即在隊列的頭進行刪除操作。

滿隊列

    當一個隊列rear指向最後一個位置時,不能夠再進行插入操作,成爲滿隊列狀態。

空隊列

    當front的位置在rear後面時,表示隊列中沒有元素可以離開,說明隊列是空狀態。

這裏寫圖片描述

循環隊列

    隊列的頭尾詳解的順訊存儲結構稱爲循環隊列

這裏寫圖片描述

隊列的缺點

    隊列空和滿都可能出現假空和假滿的狀態

棧的存儲結構

● 順序隊列
    隊列在順序存儲結構下所得到的結構,成爲順序隊列。順序棧類類似於數組,因此可以使用數組實現順序棧的相關運算。

● 鏈式隊列
    隊列在鏈式存儲結構下所得到的結構,稱爲鏈隊。鏈式隊列類似於指針,在java中可以通過類的對象引用實現指針運算。

這裏寫圖片描述

在Android中,我們常見具有代表性的隊列結構爲Queue,但是Queue確是一個接口,具體源碼如下。

public interface Queue<E> extends Collection<E> {
    /**
     * 添加方法
     * Inserts the specified element into this queue if it is possible to do so
     * immediately without violating capacity restrictions, returning
     * <tt>true</tt> upon success and throwing an <tt>IllegalStateException</tt>
     * if no space is currently available.
     * @param e the element to add
     * @return <tt>true</tt> (as specified by {@link Collection#add})
     * @throws IllegalStateException if the element cannot be added at this
     *         time due to capacity restrictions
     * @throws ClassCastException if the class of the specified element
     *         prevents it from being added to this queue
     * @throws NullPointerException if the specified element is null and
     *         this queue does not permit null elements
     * @throws IllegalArgumentException if some property of this element
     *         prevents it from being added to this queue
     */
    boolean add(E e);

    /**
     * 入隊方法
     * Inserts the specified element into this queue if it is possible to do
     * so immediately without violating capacity restrictions.
     * When using a capacity-restricted queue, this method is generally
     * preferable to {@link #add}, which can fail to insert an element only
     * by throwing an exception.
     * @param e the element to add
     * @return <tt>true</tt> if the element was added to this queue, else
     *         <tt>false</tt>
     * @throws ClassCastException if the class of the specified element
     *         prevents it from being added to this queue
     * @throws NullPointerException if the specified element is null and
     *         this queue does not permit null elements
     * @throws IllegalArgumentException if some property of this element
     *         prevents it from being added to this queue
     */
    boolean offer(E e);

    /**
     * 移除元素方法
     * Retrieves and removes the head of this queue.  This method differs
     * from {@link #poll poll} only in that it throws an exception if this
     * queue is empty.
     * @return the head of this queue
     * @throws NoSuchElementException if this queue is empty
     */
    E remove();

    /**
     * 出隊方法
     * Retrieves and removes the head of this queue,
     * or returns <tt>null</tt> if this queue is empty.
     * @return the head of this queue, or <tt>null</tt> if this queue is empty
     */
    E poll();

    /**
     * 獲取某一個元素方法
     * Retrieves, but does not remove, the head of this queue.  This method
     * differs from {@link #peek peek} only in that it throws an exception
     * if this queue is empty.
     * @return the head of this queue
     * @throws NoSuchElementException if this queue is empty
     */
    E element();

    /**
     * 出隊並刪除掉
     * Retrieves, but does not remove, the head of this queue,
     * or returns <tt>null</tt> if this queue is empty.
     * @return the head of this queue, or <tt>null</tt> if this queue is empty
     */
    E peek();
}

既然是接口,那麼就有實現類,我之前給大家分析的LinkedList源碼正是實現了該接口,具體可以查閱我之前的博客線性表數據結構解讀(二)鏈式存儲結構LinkedList

發佈了79 篇原創文章 · 獲贊 89 · 訪問量 91萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章