[英]Trouble with Swap method from a 'from scratch' Java Linked List
我已经对此进行了白板处理,似乎无法理解为什么出现内存不足错误。 该项目是从头开始创建一个链表及其一些方法。 我的其他功能很好,但是此交换功能给我带来了很多麻烦。
当我运行调试器时,程序在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;
}
}
代码的工作方式-您要交换的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
我刚刚编辑了您的代码。 只需运行它,它将解决您的问题。 您在交换节点时做错了。
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;
}
}
最终使用了另一种策略-我没有实际尝试通过重新排列下一个指针来交换节点本身,而是简单地使用temp变量交换了值。
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.