我已经白板了,似乎无法理解为什么我会出现内存不足的错误。该项目是从头开始创建链接列表及其一些方法。我的其他功能很好,但这个交换功能给我带来了很多麻烦。
当我运行调试器时,程序在pj.nextNodeLink = p.nextNodeLink上崩溃。 swap函数应该接受两个int输入并交换它们的值。我试图改变nextNodeLink指针这样做,但显然失败了。任何帮助将非常感激!
public void swapByIndex(int firstIndexValue, int secondIndexValue){
if(firstIndexValue<0 || secondIndexValue<0 || firstIndexValue>size-1 || secondIndexValue>size-1) {
throw new ArrayIndexOutOfBoundsException();
}
else if(head == tail){ // Case one - only one element in list
System.out.println("The list only has one element. Nothing to swap. ");
}
else{ // Case Two - two or more elements
//keep a pointer to the next element of head
Node firstPointer = head;
Node firstSwapElement = firstPointer;
for(int k=0; k<firstIndexValue; k++){
firstSwapElement = firstPointer; // save the node P is on into 'previ' node
firstPointer = firstPointer.nextNodeLink; // P iterates to next node
}
Node secondPointer = head;
Node secondSwapElement = secondPointer;
for(int k=0; k<secondIndexValue; k++){
secondSwapElement = secondPointer;
secondPointer = secondPointer.nextNodeLink;
}
Node secondNodeSave = secondPointer; // save this so we have the correct next node link for second swap
secondPointer.nextNodeLink = firstPointer.nextNodeLink;
firstSwapElement.nextNodeLink = secondSwapElement;
firstPointer.nextNodeLink = secondNodeSave.nextNodeLink;
secondSwapElement.nextNodeLink = secondPointer;
}
}
答案 0 :(得分:0)
您的代码如何工作 - 您要交换的A和B节点。:
Say initially you had, A - a - a2.... B - b- b2
b.next = a.next; // b - a2
A.next = B; // A - B
a.next = secondNodeSave.next; // a - b2
B.next = b; // B - b
结合所有它看起来像:
A - B - b - a2 - ?
a - b2 - ?
虽然预期是:
B - a - a2.... A - b- b2
您的逻辑和变量的位置完全被拧紧用于交换操作。您不需要关心a2
或b2
,它们与上下文无关。
正确的方法:
aPre - A - aNext ..... bPre - B - bNext
aPre->next = B
B->next = aNext
bPre->next = A
A->next = bNext
答案 1 :(得分:0)
我刚刚编辑了你的代码。只是运行它,它将解决您的问题。您在交换节点时遇到了错误。
public void swapByIndex(int firstIndexValue, int secondIndexValue){
if(firstIndexValue<0 || secondIndexValue<0 || firstIndexValue>size-1 || secondIndexValue>size-1) {
throw new ArrayIndexOutOfBoundsException();
}
else if(head == tail){ // Case one - only one element in list
System.out.println("The list only has one element. Nothing to swap. ");
}else
if(firstIndexValue==secondIndexValue)
{
System.out.print("Both are pointing to same index, no need to swap");
}
else{ // Case Two - two or more elements
//keep a pointer to the next element of head
Node firstPointer = head;
Node firstSwapElement = firstPointer;
for(int k=0; k<firstIndexValue; k++){
firstSwapElement = firstPointer; // save the node P is on into 'previ' node
firstPointer = firstPointer.nextNodeLink; // P iterates to next node
}
Node secondPointer = head;
Node secondSwapElement = secondPointer;
for(int k=0; k<secondIndexValue; k++){
secondSwapElement = secondPointer;
secondPointer = secondPointer.nextNodeLink;
}
Node secondNodeSave = secondPointer; // save this so we have the correct next node link for second swap
secondPointer.nextNodeLink = firstPointer.nextNodeLink;
firstSwapElement.nextNodeLink = secondPointer;
firstPointer.nextNodeLink = secondNodeSave.nextNodeLink;
secondSwapElement.nextNodeLink = firstPointer;
}
}
答案 2 :(得分:0)
使用不同的策略结束 - 而不是通过重新排列下一个指针实际尝试交换节点本身,我只是使用临时变量交换值。
public void swapByIndex(int firstIndexValue, int secondIndexValue){
if(firstIndexValue<0 || secondIndexValue<0 || firstIndexValue>size-1 || secondIndexValue>size-1) {
throw new ArrayIndexOutOfBoundsException();
}
else if(head == tail){ // Case one - only one element in list
System.out.println("The list only has one element. Nothing to swap. ");
}
else{ // Case Two - two or more elements
//keep a pointer to the next element of head
Node firstSwapElement = head; //THIS IS THE FIRST ELEMENT!
Node previousFirstSwapElement = firstSwapElement;
for(int k=0; k<firstIndexValue; k++){
previousFirstSwapElement = firstSwapElement; // save the node P is on into 'previ' node
firstSwapElement = firstSwapElement.nextNodeLink; // P iterates to next node
}
Node secondSwapElement = head;
Node previousSecondSwapElement = secondSwapElement;
for(int k=0; k<secondIndexValue; k++){
previousSecondSwapElement = secondSwapElement;
secondSwapElement = secondSwapElement.nextNodeLink;
}
Integer temp = (Integer)secondSwapElement.data;
secondSwapElement.data = firstSwapElement.data;
firstSwapElement.data = temp;
}
}