JAVA如何在deque上实现wrapparound
我试图在我的双端队列上实现wrapparound,由于某种原因,我的insertRight()和removerlight()方法输出错误,而我的removereft()方法只是抛出了一个错误。我环顾四周,似乎找不到我的方法在这里不起作用的答案。它们是:JAVA如何在deque上实现wrapparound,java,queue,deque,Java,Queue,Deque,我试图在我的双端队列上实现wrapparound,由于某种原因,我的insertRight()和removerlight()方法输出错误,而我的removereft()方法只是抛出了一个错误。我环顾四周,似乎找不到我的方法在这里不起作用的答案。它们是: public void insertRight(int newItem) { if (isFull() == true) { System.out.print("Full"); } // end of if
public void insertRight(int newItem) {
if (isFull() == true) {
System.out.print("Full");
} // end of if
else {
if (right == capacity) {
right = 0;
} // end of nested if
else {
deque[right] = newItem;
nElems++;
right++;
} // end of nested else
} // end of else
}// end of insertRight
public void removeRight() {
if (isEmpty() == true) {
System.out.println("Empty");
} // end of if
if (isEmpty() == false) {
System.out.println(right);
if (right == capacity) {
right = 0;
} // end of nested if
int temp = deque[right];
right++;
nElems--;
} // end of if
}// end of removeRight
public void removeLeft() {
if (isEmpty() == true) {
System.out.println("Empty");
} // end of if
if (isEmpty() == false) {
if (left == capacity) {
left = -1;
} // end of nested if
else {
System.out.println(left);
int temp = deque[left];
left++;
nElems--;
} // end of else
} // end of if
}// end of removeLeft
关于
left
和right
的实际含义/值的更多信息会有所帮助
乍一看,似乎removeLeft()
失败了,因为left
的环绕点是0,而不是容量
,前提是我对代码的理解是正确的
也。您需要直接引用实际的最后一个索引
我真的建议您研究一下代码格式。你的缩进使你很难分辨一个块的结束和一个新块的开始。只需遵循一致的缩进模式即可保存显式注释:
public void insertRight(int newItem) {
if (isFull()) {
System.out.print("Full");
} else {
if (right == capacity) {
right = 0;
} else {
deque[right] = newItem;
nElems++;
right++;
}
}
}
public void removeRight() {
if (isEmpty()) {
System.out.println("Empty");
} else {
System.out.println(right);
if (right == capacity) {
right = 0;
}
int temp = deque[right];
right++;
nElems--;
}
}
public void removeLeft() {
if (isEmpty()) {
System.out.println("Empty");
} else {
// My assumption inserted here:
if (left == 0) {
left = capacity - 1;
} else {
System.out.println(left);
int temp = deque[left];
left++;
nElems--;
}
}
}
我在这里看到了几个问题:
if (right == capacity) {
right = 0;
} // end of nested if
else {
deque[right] = newItem;
nElems++;
right++;
} // end of nested else
如果right==capacity
则重置索引,但不向数组插入newItem
它应该是这样的(直接输入,不测试):
现在转到您的removlight
-方法:
System.out.println(right);
if (right == capacity) {
right = 0;
} // end of nested if
int temp = deque[right];
right++;
nElems--;
在这里,您使用相同的算法来检查边界,但它应该是您在insertRight
中使用的算法的“镜像”,因此类似于这样(直接键入,未测试):
最后,removeLeft
:
if (left == capacity) {
left = -1;
} // end of nested if
else {
System.out.println(left);
int temp = deque[left];
left++;
nElems--;
} // end of else
如果没有
insertLeft
方法,我只能猜测它也有类似的问题。我尝试了你的方法Lothar,但仍然存在输出错误的问题,当我在insertLeft()中插入1、2、3,并尝试使用removerlight()删除所有三个时,输出是3 2 3。
System.out.println(right);
if (right == 0) {
right = capacity;
} // end of nested if
int temp = deque[right - 1];
right--;
nElems--;
if (left == capacity) {
left = -1;
} // end of nested if
else {
System.out.println(left);
int temp = deque[left];
left++;
nElems--;
} // end of else