简体   繁体   English

使用循环数组实现队列

[英]implementation of a queue using a circular array

I have found these algorithms in the internet but I can not understand that why in the enqueue method we compare size with N-1??? 我已经在互联网上找到了这些算法,但我不明白为什么在入队方法中我们将大小与N-1进行了比较??? please help me thanks!! 请帮我谢谢!!

Algorithm size():
return (N-f+r)mod N



Algorithm enqueue(e):
if size()=N-1 then
   throw a FullQueueException
Q[r]<---e
r<----(r+1)mod N

The reason that the queue is full when the size is N-1 is that in this simple implementation, 'r' represents the index of the next free element and 'f' represents the next element to retrieve. 当大小为N-1时,队列已满的原因是,在此简单实现中,“ r”代表下一个空闲元素的索引,而“ f”代表要检索的下一个元素。 If 'f' and 'r' are equal, the queue is empty, so the queue is full if incrementing 'r' would result in it being equal to 'f'. 如果“ f”和“ r”相等,则队列为空,因此如果递增“ r”将导致其等于“ f”,则队列已满。

In this implementation, at least one element is always empty. 在该实现中,至少一个元素总是空的。 This usually is more efficient than adding more logic to differentiate the case where the 'f' and 'r' is equal and the queue is full vs the case where it is empty. 这通常比添加更多逻辑来区分“ f”和“ r”相等且队列已满与空的情况相比更为有效。

BTW, in most processors the mod function is a lot more expensive than using logic like this: 顺便说一句,在大多数处理器中,mod函数比使用如下逻辑要昂贵得多:

Algorithm enqueue(e):
rNext<---r + 1
if rNext = N
    rNext<---0
if rNext = r then
    throw a FullQueueException
r<---rNext
Q[r]<---e

You provided a very broken (and incorrect) implementation. 您提供了一个非常糟糕(且不正确)的实现。

That being said, a circular queue in an array usually starts at a given index and ends at another given index (thus the f and r). 话虽如此,数组中的循环队列通常始于给定索引,并终止于另一个给定索引(因此f和r)。 However, no matter what you do, you can't have more items in the queue than you do in the underlying array. 但是,无论您做什么,队列中的项目都不能超过基础数组中的项目。

The size function here is supposed to calculate the number of elements in the queue. 这里的size函数应该用于计算队列中的元素数。 If the number becomes dangerously close to the total array size, then the queue is full. 如果该数字危险地接近总阵列大小,则说明队列已满。

I agree with @Matthew Flaschen's comment, but I will take a guess. 我同意@Matthew Flaschen的评论,但我会猜测。 I suspect that the queue is N long, and one element is reserved for a sentinel for searching. 我怀疑队列的长度为N,并且为一个前哨保留了一个元素进行搜索。 It is not how I would do it, though. 不过,这不是我会做的。

Given an implementation of circular queues wherein the begin and end are kept as indicies modulo the size of the allocated underlying array, say N, it is necessary that the actual capacity of the queue (not the array) be less than N, elsewise the begin and end indicies will be equal and there would be ambiguity between empty and full. 给定一个循环队列的实现,其中开始和结束都保持为已分配基础数组的大小(例如N)的模数,则队列(而非数组)的实际容量必须小于N,否则开始最终指标将是相等的,并且在空和满之间会有歧义。

Thus, when the allocated underlying array is of size N the true capacity of the queue is N-1. 因此,当分配的基础数组的大小为N时,队列的真实容量为N-1。 That is the why of the test. 这就是测试的原因。

There are ways around this that actually allow use of all N slots AND eliminate the division implicit in taking the index modulo n. 围绕这种情况,实际上有一些方法可以使用所有N个时隙消除将索引取n为模的隐式除法。

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

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