简体   繁体   中英

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. The swap function is supposed to take two int inputs and swap their values. I was attempting to change the nextNodeLink pointers to do so, but clearly am failing. 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.:

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.

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.

  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;

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM