[英]Queue implementation with Array- Java
I am trying to implement a Queue with an array. 我正在尝试使用数组实现队列。 (Not using the built in Java Queue functions).
(不使用内置的Java Queue函数)。 However, when tested, the array will only print out when size ==maxSize (as in the size of the array reached the maxSize/capacity).
但是,在测试时,只有在size == maxSize时(如数组的大小达到maxSize / capacity),才会打印出数组。 Other than not printing when the size is less than maxSize, the test cases pass.
当大小小于maxSize时,除了不打印以外,测试用例都通过了。 (It is a double ended Queue, so elements can be added to the front and back).
(这是一个双端队列,因此可以将元素添加到前面和后面)。 Any advice?
有什么建议吗?
package vipQueueArray;
import java.util.NoSuchElementException;
public class vipQueue {
private Object[] array;
private int size = 0;
private int head = 0; // index of the current front item, if one exists
private int tail = 0; // index of next item to be added
private int maxSize;
public vipQueue(int capacity) {
array = new Object[capacity];
maxSize = capacity;
size = 0;
tail = maxSize - 1;
}
public Object Dequeue() {
if (size == 0) {
throw new NoSuchElementException("Cant remove: Empty");
}
Object item = array[head];
array[head] = null;
head = (head + 1) % array.length;
size--;
return item;
}
public void EnqueueVip(Object x) {
if (size == maxSize) {
throw new IllegalStateException("Cant add: Full");
}
array[tail] = x;
tail = (tail - 1) % array.length;
size++;
}
public void Enqueue(Object y) {
if (size == maxSize) {
throw new IllegalStateException("Cant add: Full");
}
array[head] = y;
head = (head + 1) % array.length;
size++;
}
public boolean isFull() {
return (size == maxSize);
}
public boolean isEmpty() {
return (size == 0);
}
} }
public class Test{
public static void main(String[] args){
vipQueue Q = new vipQueue(2);
Q.Enqueue(4);
System.out.printf("->%d", Q.Dequeue());
} }
In dequeue you're doing: 在出队时,您正在执行以下操作:
head = (head + 1) % array.length;
you should (according to your impl.) change it to: 您应该(根据您的暗示)将其更改为:
head = (head - 1) % array.length;
Addition : 加法 :
This is not an implementation of a Queue: a queue works in FIFO and what you implemented works LIFO which is actually more like a...stack. 这不是队列的实现:队列在FIFO中工作,而您实现的在LIFO中工作,实际上更像是...堆栈。
In order to implement a Queue you should start with both head & tail pointing to array[0]. 为了实现队列,您应该从头和尾都指向array [0]开始。 Then every insert is
addAtTail()
which means that the item will be entered at array[tail] : 然后,每个插入都是
addAtTail()
,这意味着该项目将在array [tail]中输入:
array[tail] = item;
tail++;
size++;
You stop inserting when tail == array.length (throw exception). 当tail == array.length(抛出异常)时,您将停止插入。
Dequeue means removing the item at array[tail-1]
and then doing: 出队意味着删除
array[tail-1]
处的项目,然后执行以下操作:
tail--;
size--;
When you enqueue, you're setting head = head + 1. When you dequeue, you're return array[head] 入队时,设置head = head +1。出队时,返回array [head]
Ergo, after the Enqueue, head = 1, but the item you added is in slot 0. go,入队后,head = 1,但是您添加的项目在插槽0中。
Additionally, having tail = capacity-1, when there's nothing in the queue is bound to cause trouble. 此外,当tail = Capacity-1时,如果队列中没有任何内容,势必会引起麻烦。 En
恩
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.