I am making a project right now to practice double linked lists and I have been working on a method in my double linked lists class which switches node locations. It works fine when trying to switch non adjacent nodes but when trying to switch adjacent nodes the error occurs for example.
If the starting list is:
- Carrot
- Candy
- Watermelon
- Bread
The output will be
- Carrot
- Watermelon
- Watermelon
- Bread
Im trying to figure out the error but my knowledge is lacking there, Any help would be great I appreciate it!
I tried creating a temp node to hold the original prev and next for the nodes, but that didnt work. I tried if statements which checked if the prev and next were equal and that didnt work.
Here is my code
public void switchNodes(int firstSpace, int secondSpace){
//If statement which checks isEmpty(), and if firstSpace and secondSpace is less than 0.
if(isEmpty()|| firstSpace < 0 || secondSpace < 0){
System.out.println("List is empty or one of spaces is entered does not have anything in it.");//If any of the previous if scenarios is met, prints a message telling the user that.
}
else{
Node<E> firstN = current(firstSpace); //Creates a node firstN by calling the current method and using the imported firstSpace int.
Node<E> secondN = current(secondSpace); //Creates a node secondN by calling the current method and using the imported secondSpace int.
switchSpace(firstN, secondN);//Calls the switchSpace method with nodes firstN and secondN.
}
}
/**
*Private method which takes in two nodes and switches their positions on the list.
* Done by creating two nodes with the information imported from the switchNodes method.
* The created nodes contain the same element and the swapped prev and next.
* The nodes around the swapped nodes are then accessed and replaced to the swapped nodes address.
*/
private void switchSpace(Node<E> firstSwitch, Node<E> secondSwitch){
//Creates switchedFirst node to contain the element of the imported firstSwitch node and the addresses of the secondSwitch node.
Node<E> switchedFirst = new Node<E>(firstSwitch.getElement(), secondSwitch.getPrev(), secondSwitch.getNext());
//Creates switchedSecond node to contain the element of the imported secondSwitch node and the addresses of the firstSwitch node.
Node<E> switchedSecond = new Node<E>(secondSwitch.getElement(), firstSwitch.getPrev(), firstSwitch.getNext());
switchedFirst.getNext().setPrev(switchedFirst);//The node after the switchedFirst nodes new position is accessed and the prev address is set to switchedFirst.
switchedFirst.getPrev().setNext(switchedFirst);//The node before the switchedFirst nodes new position is accessed and the next address is set to switchedFirst.
switchedSecond.getNext().setPrev(switchedSecond);//The node after the switchedSecond nodes new position is accessed and the prev address is set to switchedSecond.
switchedSecond.getPrev().setNext(switchedSecond);//The node before the switchedSecond nodes new position is accessed and the next address is set to switchedSecond.
}