线性表数据结构解读(四)队列结构Queue

    在上一篇文章中,咱们详细介绍了栈结构,并结合Stack源码进行了分析,相关文章你们能够点击这里回看个人博客:线性表数据结构解读(三)栈结构Stackjava

队列的定义

    队列是一种插入和删除分别在两端进行操做的线性表,一端进行插入操做,一端进行删除操做。数组

这里写图片描述

队列的特色

    咱们把进入队列端称为队列的对尾,用rear表示;离开队列的一端成为队列的头,用front表示,即在队列的头进行删除操做。markdown

满队列

    当一个队列rear指向最后一个位置时,不可以再进行插入操做,成为满队列状态。数据结构

空队列

    当front的位置在rear后面时,表示队列中没有元素能够离开,说明队列是空状态。app

这里写图片描述

循环队列

    队列的头尾详解的顺讯存储结构称为循环队列this

这里写图片描述

队列的缺点

    队列空和满均可能出现假空和假满的状态spa

栈的存储结构

● 顺序队列
    队列在顺序存储结构下所获得的结构,成为顺序队列。顺序栈类相似于数组,所以可使用数组实现顺序栈的相关运算。.net

● 链式队列
    队列在链式存储结构下所获得的结构,称为链队。链式队列相似于指针,在java中能够经过类的对象引用实现指针运算。指针

这里写图片描述

在Android中,咱们常见具备表明性的队列结构为Queue,可是Queue确是一个接口,具体源码以下。rest

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

相关文章
相关标签/搜索