The storage space of cyclic queue is array data[21

dyfdyfdyf 注册会员
2023-02-28 14:05

Why 16?
The head points to 8 and the tail points to 3. What elements are not in the queue? 4 to 7, obviously, because 3 and 8 are also queue elements. There are 4 to 7, so the queue length is 21 minus 4=17

xianyuxuyang 注册会员
2023-02-28 14:05

This answer quotes ChatGPT

According to the definition of a circular queue, the queue length should be the "tail pointer" position minus the "head pointer" position. However, it should be noted that if the tail pointer is in front of the head pointer, it means that the queue is divided into two segments. In this case, the queue length is the tail segment length + the head segment length. Since the length of the tail segment is the array length minus the length of the header segment, we can calculate the tail segment length first and then subtract the array length from


The length of the tail segment gets the length of the head segment.

According to the head pointer and tail pointer given in the title, it can be found that the tail pointer is in front of the head pointer, so it is necessary to calculate the length of the head segment and the tail segment respectively:

Tail length = array length - Head pointer + tail pointer = 21-8 + 3 = 16
Head length = array length - Tail length = 21-16 = 5

Therefore, the queue length is tail segment length + head segment length = 16 + 5 = 21. So, the correct answer is 21, not 17.