[英]Trouble with Swap method from a 'from scratch' Java Linked List
I've whiteboarded this out, and cannot seem to understand why I am getting an out of memory error. 我已经对此进行了白板处理,似乎无法理解为什么出现内存不足错误。 The project is to create a linked list and some of its methods from scratch.
该项目是从头开始创建一个链表及其一些方法。 My other functions are good to go, but this swap function is giving me a lot of trouble.
我的其他功能很好,但是此交换功能给我带来了很多麻烦。
When I run the debugger, the program crashes on the pj.nextNodeLink = p.nextNodeLink. 当我运行调试器时,程序在pj.nextNodeLink = p.nextNodeLink上崩溃。 The swap function is supposed to take two int inputs and swap their values.
swap函数应该接受两个int输入并交换它们的值。 I was attempting to change the nextNodeLink pointers to do so, but clearly am failing.
我试图更改nextNodeLink指针来这样做,但显然失败了。 Any help would be much appreciated!
任何帮助将非常感激!
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;
}
}
How your code works - A and B nodes you want to swap.: 代码的工作方式-您要交换的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
Combining all it looks like : 结合所有看起来像:
A - B - b - a2 - ?
a - b2 - ?
while expected is : 而预期的是:
B - a - a2.... A - b- b2
You logic and placements of variables are totally screwed for the swapping operation. 您的逻辑和变量放置完全为交换操作所困扰。 You don't need to care about
a2
or b2
, they are irrelevant in the context. 您无需关心
a2
或b2
,它们在上下文中无关紧要。
Correct approach: 正确的做法:
aPre - A - aNext ..... bPre - B - bNext
aPre->next = B
B->next = aNext
bPre->next = A
A->next = bNext
I have just edit your code. 我刚刚编辑了您的代码。 just run it, it will solve your problem.
只需运行它,它将解决您的问题。 You were doing mistake in swapping of nodes.
您在交换节点时做错了。
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;
}
}
Ended up using a different tactic - instead of actually trying to swap the nodes themselves by rearranging the next pointers, I simply swapped the values using a temp variable. 最终使用了另一种策略-我没有实际尝试通过重新排列下一个指针来交换节点本身,而是简单地使用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.