继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

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

猴子搬来的救兵Castiel
关注TA
已关注
手记 77
粉丝 40
获赞 184

 在上一篇文章中,我们详细介绍了栈结构,并结合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

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP