Advanced programmers lessons - Architect of the road (5) - Queue

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/m0_37609579/article/details/99614679

First, the definition of the queue

Queue (Queue) is a special kind of linear form , is special in that it only allows deletion at the front end of the table (Front), while the rear end of insertion, the table (REAR), and stack as a queue table restricted linear modes of operation. Referred to as the tail end of the end, delete operation will be referred to as head-of-insertion operation. When there is no queue element, called an empty queue.

Also known as data elements of the queue queue element. Insert a queue element called into the team in the queue, the queue is deleted from a queue element called a team. Because the queue is inserted only at one end, the other end of the deletion, so only the first element of the queue to enter the first removed from the queue, it is also known as FIFO queue (FIFO-first in first out) linear form.

Second, a schematic diagram of the queue

Third, the implementation of the queue

Queue is a very common data structure type, the Java inside Queue is an interface, it's just a basic definition of what should Queue functional specification. A plurality of Queue actually implemented, using some linear form implemented, some based on the linked list implementation. There are suitable for multi-threaded environment. Queue java class having several mainly functions as follows: AbstractQueue, ArrayBlockingQueue, ConcurrentLinkedQueue, LinkedBlockingQueue , DelayQueue, LinkedList, PriorityBlockingQueue, PriorityQueue and ArrayDeque.

  1.   Unidirectional queue (Queue): the data can only be inserted in one end, the other end of the deleted data.
  2.   Double-ended queue (Deque): the ends can be inserted into the queue data and delete data operations.

Several classes JDK relationship between common queues as follows:

Fourth, the common method queue

 
  1.  
    public interface Queue<E> extends Collection<E> {
  2.  
    boolean add(E e); // 添加元素到队列中,相当于进入队尾排队。超出队列界限时抛异常
  3.  
    boolean offer(E e); //添加元素到队列中,相当于进入队尾排队.超出队列界限时返回false
  4.  
    E remove(); //移除队头元素。超出队列界限时抛异常
  5.  
    E poll(); //移除队头元素.超出队列界限时返回false
  6.  
    E element(); //获取但不移除队列头的元素,如果为空抛NoSuchElementException异常
  7.  
    E peek(); //获取但不移除队列头的元素,队列为空返回null
  8.  
    }
 

Five, Deque so special

According to our general understanding, Deque is a double-ended queue, but this would mean that it is an enhancement to Queue interfaces.

If a careful analysis of the Deque interface code, we will find it inside the main function definition contains four sections.

  1. Method deque specific definitions.
  2.  Queue defined method.
  3.  Stack defined method.
  4.  Collection methods defined.

Methods Part 3, 4 tells us that concrete implementation of Deque class can treat them as ordinary Stack Collection to use.

Sixth, the nature of the queue

  1. a0 is the first element of the queue, only one successor
  2. an is the last element of the queue, only a precursor
  3. In addition to other elements ai a0 and an, both predecessor, successor have
  4. Queue access can be itemized and sequential access

Seven, you have to use the queue

  1. message queue
  2. Stop queue
  3. Redis queue implementation spike / rush

Eight, stacks and queues small summary

 By speaking in front of stacks and queues Benpian say these two data structures, to sum up:

  1. 栈、队列通常是用来简化某些程序操作的数据结构,而不是主要作为存储数据的;
  2. 在这些数据结构中,只有一个数据项可以被访问;
  3. 栈允许在栈顶压入(插入)数据,在栈顶弹出(移除)数据,但是只能访问最后一个插入的数据项,也就是栈顶元素;
  4. 队列(单向队列)只能在队尾插入数据,对头删除数据,并且只能访问对头的数据。而且队列还可以实现循环队列,它基于数组,数组下标可以从数组末端绕回到数组的开始位置;
  5. 这些数据结构都能由数组实现,但是可以用别的机制(后面讲的链表、堆等数据结构)实现。

队列的实现类很多,后面我们会专门看JDK的源码,通过源码来进一步加深对队列的理解和认识。


我的微信公众号:架构真经(id:gentoo666),分享Java干货,高并发编程,热门技术教程,微服务及分布式技术,架构设计,区块链技术,人工智能,大数据,Java面试题,以及前沿热门资讯等。每日更新哦!

参考文章:

  1. https://blog.csdn.net/f2006116/article/details/51375470
  2. https://www.cnblogs.com/ciyeer/p/9031480.html
  3. https://www.cnblogs.com/ysocean/p/7921930.html

Guess you like

Origin www.cnblogs.com/anymk/p/11470493.html