Definition of queue:
A storage structure that can realize "first in, first out", similar to queuing to buy tickets, first in line; it can be operated at both ends, but it can only be fixed at one end and fixed at one end to delete; and the stack can only be Insert and delete operations on one end
Classification of queues:
Chained queue [implemented with linked list]-very simple, there is no difference from before, we will not talk about it
Static queue [implemented with an array]-static queues usually have to be circular queues, which is a bit difficult
Circular queue explanation:
(1) Why the static queue must be a circular queue
If it is a traditional array, if the queue goes out from the Front, it can only be added, and the queue needs to be entered from Rear, and it can only be added. Then the space below you is wasted, so here in the circular queue, the circular queue It means: if the Rear is added to the head, then let Rear can loop to the beginning of the array, the same is true for Front
For example:
① At the beginning, from front to rear, there is only one data c
② At this time we want to put the element "in" into it, then move the rear down and set it as a circular queue. At this time, there are two data, namely c and medium
③ At this time, we want to delete another element, then the front can only be added, and the c element can only be deleted. There is another element in the queue, which is middle
(2) The circular queue needs several parameters to determine
Requires Front and Rear parameters
(3) The meaning of these two parameters of the circular queue
① When the queue is initialized: Front and Rear are both zero
② When the queue is not empty: Front refers to the front element of the queue , Rear refers to the position behind the last element of the queue
③ When the queue is empty: Front and Rear are equal, but not necessarily zero
(4) Explanation of the pseudo-algorithm of circular queue entry
Joining the team is from the tail, a total of two steps are required to complete
① Put the elements in the team first in the rear position
② Then rear = (rear + 1)% array length [because the circular queue has special circumstances], don’t just rear = rear + 1
(5) Explanation of the pseudo-algorithm for dequeuing from the circular queue
Dequeue is dequeued from the head, it only takes one step to complete, or if you want to save the element of the dequeue, add a step
front = (front + 1)% array length
(6) How to judge whether the circular queue is empty
If front and rear are equal, then this circular queue is empty
(7) How to judge whether the circular queue is full
When drawing a sketch simulation, use the method of drawing circles to make it easier to understand
The first thing to be clear is that there is no absolute relationship between the size of front and rear in the circular queue.
If you want to be full, you need to meet: rear = front [This is consistent with the determination of the circular queue to be empty! How to do? 】
① You can define one more parameter for the number of queue elements, and you can judge by this parameter
② The queue can put 6 values, but it is stipulated to only put 5, this problem is solved [commonly used] -front = (rear + 1)% array length
Some name issues need attention:
For the stack, we usually use Top and Bottom. The pointer points from Top to Bottom. Pushing and popping are all performed at Top.
For queues, we usually use Front and Rear. The pointers point from Front to Rear, enqueue in Rear, and dequeue in Front [essentially or linked list], F points to the first element, and R points to the next element of the last element. element