Data structure study notes ------ queue

Question: If an array of size 6 is used to implement a circular queue, and the current values ​​of rear and front are 0 and 3 respectively, when an element is deleted from the queue and two elements are added, the values ​​of rear and front are respectively ( ).
A. 1 and 5          B.  2 and 4          C.  4 and 2        D. 5 and 1

The circular queue Q is implemented using an array Q of size 6. When the team is empty, Q.fornt=Q.rear. If the queue is not empty, front points to the current head element, and rear points to the next position at the end of the current queue. When the queue head pointer points to 3 and the queue tail pointer points to 0, the positions of the queue head and queue tail pointers are shown in the figure. The positions occupied by the queue are positions 3~5, and the unoccupied positions are 0~2.

Delete an element from the queue, Q.front=(Q.front+1)%MaxSize=(3+1)%6=4, the front pointer points to the position where the array subscript is 4. At this time, the positions occupied by the queue are 4~5, and the unoccupied positions are 0~3, as shown in Figure (2). When two elements enter the queue, execute Q.rear=(Q.rear+1)% Maxsize twice in sequence, where Maxsize=6, and the found positions are 1 and 2 in turn. After these two elements enter the queue, the changes in the queue are shown in the figure below.

As can be seen from the right figure above, the value of the front pointer is 4, and the value of the rear pointer is 2. So, choose B.

Guess you like

Origin blog.csdn.net/qiaoqi17/article/details/108166801