what is a queue
Queue is an important type of data structure. It supports FIFO, tail addition and head deletion (elements of advanced queues are first out of the queue), which is similar to queuing in our lives.
There are two types of queues:
- single queue
- circular queue
A single queue is a common queue. Every time an element is added, it is added to the end of the queue:
Taking the queue implemented by an array as an example, the initial queue length is fixed to 4, and both font and rear are 0:
Each time an element is added, rear is shifted by one. After adding four elements, rear is at index 4:
At this time, a1 and a2 are dequeued, and the front moves to 2:
At this time, I want to add two more elements, but after shifting rear two places, it will be out of bounds:
There are three vacancies, but only one can be put in! This is the " false overflow " situation for a single queue .
(The above reference is borrowed from http://www.nowamagic.net/librarys/veda/detail/2350 )
In response to this situation, the solution is to start from the beginning when the back is full, that is, a loop that is connected end to end. This is the concept of a "circular queue" .
Circular queue:
In a circular queue,
rear = (rear - size) % size
Following the above example, when rear is greater than the queue length, rear = ( 5 - 5) % 5 = 0 :
When you continue to add in this way, you can also add several elements:
So how to judge whether the queue is full of elements? Using front == rear alone cannot judge whether it is empty or full.
Two methods:
- Add a flag flag, which is initially false, and when it is full, it is set to true;
- Instead of front = rear as the full sign, change to (rear - front) % size = 1.
When the formula of method 2 is filled with elements, there is a space left . What does this formula mean?
Following the above situation, when rear adds elements from the back to the front 0, another element a6 is added, and rear is moved one bit back to 1. At this time, front = 2, (1 - 2) % 5 = 1, which is full condition.
Therefore, when rear > font, the number of elements in the queue = rear - font;
When rear < font, the elements in the queue are divided into two parts: size - font and rear, that is, rear + size - font. Taking the above picture as an example, the number of elements in the queue = 1 + 5 - 2 = 4.
Then we introduce the queue Queue in the Java collection framework
The Queue in the Java collection inherits from the Collection interface , which is implemented by Deque, LinkedList, PriorityQueue, BlockingQueue and other classes.
Queue is used to store a collection of elements waiting to be processed. This scenario is generally used for buffering and concurrent access.
In addition to inheriting some methods of the Collection interface, Queue also adds additional add, delete, and query operations.
Add, delete, query these operations provide two forms, one of which directly throws an exception when the operation fails, and the other returns a special value:
Queue method introduction:
1.add(E), offer(E) add at the end:
boolean add(E e);
boolean offer(E e);
What they have in common is that it is recommended that the implementation class prohibit adding null elements, otherwise a NullPointerException will be reported;
The difference is that the add() method will report some runtime errors when the addition fails (such as when the queue is full); and the offer() method will not crash even if the addition fails, but will only return false.
2016.11.21 Added
Notice
Queue is an interface. The original purpose of the add and offer methods it provides is to hope that subclasses can prohibit adding elements to be null, so as to avoid returning null when querying whether it is correct or incorrect.
In fact, most Queue implementation classes do respond to the provisions of the Queue interface, such as ArrayBlockingQueue, PriorityBlockingQueue and so on.
But there are still some implementation classes that do not have this requirement, such as LinkedList.
Thanks to sumsear for pointing that out.
2.remove(), poll() delete and return the head:
E remove();
E poll();
When the queue is empty, the remove() method will report a NoSuchElementException error; while poll() will not crash, it will only return null.
3.element(), peek() get but not delete:
E element();
E peek();