简体   繁体   English

循环队列和循环链接列表

[英]Circular Queue and Circular Linked List

I want to have a clear difference between circular queue and circular single linked list ?? 我想在循环队列和循环单链表之间有一个明显的区别? Although on the outset,both look nearly same.... 尽管从一开始,两者看上去都差不多。

Circular queue or Circular Buffer: is a way of implementing a queue. 循环队列或循环缓冲区:是一种实现队列的方法。 For example, suppose you want to implement a queue using an array. 例如,假设您要使用数组实现队列。 You'd have your enqueue() and dequeue() methods. 您将拥有enqueue()dequeue()方法。

Suppose the underlying array is of length 7, and the user enqueues five values, so the values in the underlying array look like this: 假设基础数组的长度为7,并且用户排队了五个值,因此基础数组中的值如下所示:

            head                   tail
position:  | 0 | 1  | 2 | 3 | 4  |   5  |   6  |
value:     | 3 | 12 | 5 | 4 | 71 | free | free |

Now the user wants to dequeue an element, removing value 3 from position 0 . 现在,用户想要使元素出队,从位置0删除值3 As the queue implementer, you'd have to figure out how to handle this. 作为队列实现者,您必须弄清楚如何处理此问题。 A basic solution would be to move all values down by one position so the underlying array now looks like this: 一个基本的解决方案是将所有值向下移动一个位置,因此基础数组现在看起来像这样:

            head               tail
position:  | 0  | 1 | 2 | 3  | 4    |   5  |   6  |
value:     | 12 | 5 | 4 | 71 | free | free | free |

but that may require unnecessarily copying a lot of values every time you dequeue anything! 但这可能需要在每次出队时不必要地复制很多值! One way to avoid that is to say that your head is now at position 1 instead of 0, 一种避免这种情况的方法是说您的头部现在位于位置1而不是0,

                   head               tail
position:  |   0  | 1  | 2 | 3 | 4  |   5  |   6  |
value:     | free | 12 | 5 | 4 | 71 | free | free | 

so now every time you add a new element you'll just add it to the tail (and increment the tail position), and if you remove an element, you'll just move the head . 因此,现在每次添加一个新元素时,您只需将其添加到tail (并增加tail位置),如果删除某个元素,则只需移动head This way you don't have to do any unnecessary copying. 这样,您不必进行任何不必要的复制。

Once the tail reaches the end of the array it'll start wrapping over to the beginning of the array -- ie the queue will move in "circle" over the underlying array. 一旦tail到达数组的末尾,它将开始环绕到数组的开头-即队列将在基础数组上“圈”移动。 For example, after a few more enqueues and dequeues, the underlying array would look like this: 例如,经过更多的入队和出队后,底层数组将如下所示:

                  tail                head
position:  | 0  |   1  |   2  |   3  | 4  | 5  | 6 |
value:     | 91 | free | free | free | 71 | 22 | 8 | 

The tail now wrapped around to the beginning of the array. 尾巴现在缠绕到数组的开头。

Circular linked list : a linked list where the head points to the tail. 循环链表 :头指向尾的链表。 It's a general-purpose circular structure. 这是一种通用的圆形结构。 It can be used to implement a circular queue/buffer, or it may be used for something else. 它可以用于实现循环队列/缓冲区,也可以用于其他用途。

检查一下: http : //www.vias.org/cppcourse/chap20_05.html您将注意到,循环队列在标准定义中作为数组实现。

The major difference between circular linked list and circular queue/circular buffer/ring buffer is that: 循环链表和循环队列/循环缓冲区/环形缓冲区之间的主要区别在于:

  • In a circular linked list the next pointer of the last node points to the head (of the linked list). 在循环链表中,最后一个节点的下一个指针指向链表的头。 While in a circular buffer, we simply maintain two indices front and rear which point to the beginning and end of the buffer. 在循环缓冲区中时,我们只需要在缓冲区的开头和结尾保持两个索引,它们分别指向缓冲区的开头和结尾。

  • Unless otherwise specified (the position of insertion or deletion), it takes place at the end/tail. 除非另有说明(插入或删除的位置),否则发生在末尾。 In case of Circular Buffer deletion happens at the front index and addition at the tail; 如果使用循环缓冲区,则会在前索引处删除,并在末尾添加; ie, consumer consumes from the front of the buffer and producer appends to the end. 也就是说,消费者从缓冲区的前端进行消费,而生产者则从缓冲区的末尾追加。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM