Queue queue

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:

write picture description here

Each time an element is added, rear is shifted by one. After adding four elements, rear is at index 4:

write picture description here

At this time, a1 and a2 are dequeued, and the front moves to 2:

write picture description here

At this time, I want to add two more elements, but after shifting rear two places, it will be out of bounds:

write picture description here

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 :

write picture description here

When you continue to add in this way, you can also add several elements:

write picture description here

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:

  1. Add a flag flag, which is initially false, and when it is full, it is set to true;
  2. 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?

write picture description here

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.

write picture description here

Then we introduce the queue Queue in the Java collection framework

write picture description here

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.

write picture description here

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:

write picture description here

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();

element() throws an exception when the queue is empty; peek() doesn't crash, it just returns null.

other

1. Although LinkedList does not prohibit adding null, the implementation class of Queue generally does not allow adding null elements. Why? Because the poll() and peek() methods will return null when abnormal, after you add null, it is difficult to tell whether the return is correct or not when you get it.

2. Queue is generally FIFO, but there are exceptions, such as priority queue (its order is based on natural sorting or custom comparator); another example is LIFO queue (same as stack, those who enter later go out first).

No matter what the order of entry and exit is, the remove() and poll() methods are used to operate the elements in the head; and the insertion position is not necessarily at the end of the queue, different queues will have different insertions logic.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326044067&siteId=291194637