Data Structure Notes - Queue

definition

Queue: A linear list that only allows insertion at one end and deletion at the other .

A queue is a first-in, first- out linear list. The end that allows insertion is called the tail of the queue, and the end that allows deletion is called the head of the queue.

As shown below:

write picture description here

circular queue

The linear table has sequential storage and chain storage, and the stack is a linear table, so there are two storage methods. Similarly, the queue, as a special linear table, also exists in these two storage methods.

Insufficient queue order storage

Assuming that there is a queue with n elements, an array larger than n needs to be established for the sequential storage queue, and all elements of the queue are stored in the first n elements of the array, and the end of the array subscript 0 is the head of the queue. The so-called queue operation is actually adding an element to the end of the queue without moving any elements, so the complexity of the family is O(1).

write picture description here

Unlike the stack, the queue elements are dequeued at the head of the queue, that is, the position with the subscript 0, which means that all elements in the queue have to move forward, that is, the position with the subscript 0 is not Empty, the time complexity is O(n).

write picture description here

Their specific implementation is the same as the sequential storage structure of the linear table, and is no longer cumbersome.

but

If you do not limit the condition that the elements of the queue must be stored in the first n elements of the array, the performance of dequeuing will be greatly increased, as shown in the figure

write picture description here

In order to avoid that when there is only one element, the overlapping of the queue head and the queue tail makes processing troublesome, two pointers are introduced, front points to the head element of the queue, and rear points to the next position of the rear element, so that when front is equal to rear, it means empty. queue.

In this way, another problem arises. If there is an element at the end of the queue, where does rear point to when a new element is inserted? Moreover, when there is only an element at the end of the queue, adding it backwards will cause an array out-of-bounds error, but in fact the previous position may be empty. We call this phenomenon false overflow .

Circular queue definition

The way to solve the false overflow is to start from the beginning when the back is full, that is, a loop that is connected end to end. We call this head-to- tail sequential storage structure of the queue a circular queue.

Subscripts 2, 3, and 4 already have elements, and when a new element is inserted, the position with subscript 0 is stored. As shown in the figure:

write picture description here

But it was previously agreed that when front==rear, it means that the queue is empty. In the above method, when front==rear also means that the queue is full, how to deal with it?

We require that when the queue is full, there is still a free unit in the array, as shown in the figure:

write picture description here

This results in the following formula:

  • Queue full condition (rear+1) %QueueSize==front

  • Queue length formula: (rear-front+QueueSize)%QueueSize

Chained storage of queues

The chained storage structure of the queue is actually a singly linked list of a linear table, but it can only go in and out of the head. We call it a chain queue for short.

write picture description here

When the queue is empty, both front and rear point to the head node:

write picture description here

Enqueue operation

write picture description here

  1. Follow the last element to point to the new element;
  2. Point the tail pointer to the new element.

Dequeue operation

write picture description here

  1. Change the successor of the head node to the next node;
  2. If there is only one element left in addition to the head node, point rear to the head node.

In general, when you can determine the maximum queue length, it is recommended to use a circular queue, and if you cannot estimate the length of the queue, use a chain queue.

For more exciting content, please pay attention to my WeChat public account - Android Motor Vehicles
write picture description here

Guess you like

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