简体   繁体   English

使用循环数组实现队列

[英]Queue implementation with circular array

I am reading about implementation of DynaArrayQueue (queue that doubles in size when there are no enough elements) 我正在阅读关于DynaArrayQueue的实现(当没有足够的元素时,队列的大小加倍)

I have certain questions regarding two methods in it. 我对其中的两种方法有一些疑问。

Let capacity be the capacity of the queue. 容量是队列的容量。

getQueueSize() Method getQueueSize()方法

public int getQueueSize()
 {
if(front == -1) return 0;

//Here why can't the size by  simply [(rear -front +1) %capacity ]
int size = (capacity - front + rear +1) % capacity;

if(size == 0) return capacity;
else return size;
 }

When calculating the size why are we using 在计算尺寸时我们使用的原因

size = (capacity - front + rear +1 ) % capacity , instead of simply (rear - front +1) % capacity. size = (容量 - 前+后+ 1) %容量,而不是简单(后 - 前+1) %容量。 ?

Question 2: 问题2:

resizeQueue() resizeQueue()

This is the method that resizes the queue 这是调整队列大小的方法

  private void resizeQueue()
 {
int initCapacity = capacity;
capacity *=2;

int[] oldArray = array;

array = new init[this.capacity];

//this is fine
for(int i=0; i<oldArray.length;i++)
{
    array[i] =oldArray[i];
}

//why do we need this ?
if(rear < front)
{
    for(int i =0 ; i<front ; i++)
    {
        array[i+initCapacity] = this.array[i];
        array[i]= null;

    }

    rear = rear + initCapacity;

}


  }

The first for loop in the method is straight forward, but why do we need the second if loop for ? 方法中的第一个for循环是直接的,但为什么我们需要第二个if循环呢? . What conditions it is taking care of ? 这是什么条件照顾?

1. The reason capacity is there is because 容量的原因是因为

(rear - front) 

can be negative, negative modulus vary by language implementations. 可以是负的,负模数因语言实现而异。

2. The reason you have the 2nd loop is because 你有第二个循环的原因是因为

In this situation 在这种情况下

[#####000000####]
     ^      ^
    rear   front

# is array item
0 is empty space

When you copy to a larger array, you get a disjointed array 复制到更大的数组时,会得到一个脱节的数组

[#####000000####000000000000000]

The second loop moves 第二个循环移动

[#####000000####000000000000000]
 ^---^

to

[00000000000#########0000000000]
                ^---^

The second loop is making sure that items are stored in the proper order in the new array. 第二个循环是确保项目以新数组中的正确顺序存储。 Imagine a circular buffer where front (the place you're taking from) is greater than rear (where you're adding things). 想象一个循环缓冲区,前面(你要取的地方)大于后面(你要添加的东西)。 So capacity is 50, front is 40, and rear is 10. Items will be removed from the queue in the order 40, 41, 42, 43...0, 1, 2, ... 9. 因此容量为50,前面为40,后面为10.项目将按顺序40,41,42,43 ... 0,1,2,... 9从队列中删除。

Now, when you resize the queue, items are copied to the new array in the same order. 现在,当您调整队列大小时,项目将以相同的顺序复制到新阵列。 But removing items from the queue, which now has capacity of 100, will be 40, 41, 42, ... 49, 50, 51. But there's nothing at position 50! 但是从队列中删除现在容量为100的项目将是40,41,42,... 49,50,51。但是在50位没有任何东西!

So those 10 items are moved from positions 0 through 9 to positions 50 through 59. That's what the second loop does. 所以这10个项目从0到9位置移动到50到59位置。这就是第二个循环的作用。

Not familiar with this class, but I guess if the queue has wrapped around, so that new items are being written at the start of the array, then "rear" could be less than "front". 不熟悉这个类,但我想如果队列已经缠绕,那么在数组的开头写入新项目,那么“后”可能小于“前”。 This kind of answers both of your questions, because in this case, (rear - front +1) % capacity would be negative, which is not what you want (Q1), and also the new part of the queue would have to be transplanted to the end, which the capacity increases (Q2). 这样可以解答你的两个问题,因为在这种情况下, (rear - front +1) % capacity将是负数,这不是你想要的(Q1),并且队列的新部分也必须被移植到最后,容量增加(Q2)。

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

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