循环数组的队列实现
我正在阅读有关 DynaArrayQueue(当没有足够元素时大小加倍的队列)的实现的信息,
我对其中的两种方法有某些疑问。
令容量为队列的容量。
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;
}
在计算大小时,为什么我们使用
size = (capacity - front + back +1 ) %capacity ,而不是简单地 (rear - front +1)%容量。 ?
问题2:
resizeQueue()
这是调整队列大小的方法
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;
}
}
该方法中的第一个for循环很简单,但是为什么我们需要第二个if循环?。它照顾什么条件?
I am reading about implementation of DynaArrayQueue (queue that doubles in size when there are no enough elements)
I have certain questions regarding two methods in it.
Let capacity be the capacity of the queue.
getQueueSize() Method
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. ?
Question 2:
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 ?. What conditions it is taking care of ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
1. 容量存在的原因是因为
可以为负,负模数因语言实现而异。
2.你有第二个循环的原因是因为
在这种情况下
当你复制到一个更大的数组时,你会得到一个不相交的数组
第二个循环移动
到
1. The reason capacity is there is because
can be negative, negative modulus vary by language implementations.
2. The reason you have the 2nd loop is because
In this situation
When you copy to a larger array, you get a disjointed array
The second loop moves
to
第二个循环是确保项目以正确的顺序存储在新数组中。想象一个圆形缓冲区,其中前部(您从中获取的位置)大于后部(您添加内容的位置)。因此容量为 50,前端为 40,后端为 10。项目将从队列中按顺序删除:40, 41, 42, 43...0, 1, 2, ... 9。
现在,当您调整大小时队列中的项目以相同的顺序复制到新数组中。但是从现在容量为 100 的队列中删除项目将是 40, 41, 42, ... 49, 50, 51。但是位置 50 处没有任何内容!
因此,这 10 个项目从位置 0 到 9 移动到位置 50 到 59。这就是第二个循环的作用。
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.
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!
So those 10 items are moved from positions 0 through 9 to positions 50 through 59. That's what the second loop does.
不熟悉这个类,但我想如果队列已经绕回,以便在数组的开头写入新项目,那么“后”可能小于“前”。这回答了你的两个问题,因为在这种情况下,
(rear - front +1) %capacity
将为负数,这不是你想要的(Q1),也是队列必须移植到容量增加的末尾(Q2)。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).