Question: Question: SWAPPING NODES IN A SINGULARLY LINKED LIST: I am attempting to create a program that swaps 2 nodes (no matter where they are in
Question: SWAPPING NODES IN A SINGULARLY LINKED LIST: I am attempting to create a program that swaps 2 nodes (no matter where they are in the list) and am having some difficulty. I can't seem to figure out why this swap method is throwing an error.
Code: (SWAPPING METHOD AND TEST LINE BOLDED)
package linkedlists;
public class SinglyLinkedList
/** The element stored at this node */ private E element; // reference to the element stored at this node
/** A reference to the subsequent node in the list */ private Node
/** * Creates a node with the given element and next node. * * @param e the element to be stored * @param n reference to a node that should follow the new node */ public Node(E e, Node
// Accessor methods /** * Returns the element stored at the node. * @return the element stored at the node */ public E getElement() { return element; }
/** * Returns the node that follows this one (or null if no such node). * @return the following node */ public Node
// Modifier methods /** * Sets the node's next reference to point to Node n. * @param n the node that should follow this one */ public void setNext(Node
// instance variables of the SinglyLinkedList /** The head node of the list */ private Node
/** The last node of the list */ private Node
/** Number of nodes in the list */ private int size = 0; // number of nodes in the list
/** Constructs an initially empty list. */ public SinglyLinkedList() { } // constructs an initially empty list
// access methods /** * Returns the number of elements in the linked list. * @return number of elements in the linked list */ public int size() { return size; }
/** * Tests whether the linked list is empty. * @return true if the linked list is empty, false otherwise */ public boolean isEmpty() { return size == 0; }
/** * Returns (but does not remove) the first element of the list * @return element at the front of the list (or null if empty) */ public E first() { // returns (but does not remove) the first element if (isEmpty()) return null; return head.getElement(); }
/** * Returns (but does not remove) the last element of the list. * @return element at the end of the list (or null if empty) */ public E last() { // returns (but does not remove) the last element if (isEmpty()) return null; return tail.getElement(); }
// update methods /**swapNodes * @param positionOfNode1 * @param positionOfNode2 */
public void swapNodes(int positionOfNode1, int positionOfNode2){ if (positionOfNode1 >= size() || positionOfNode2 >= size()) { throw new IndexOutOfBoundsException(); } if(positionOfNode1==positionOfNode2)return;//Don't do anything if they're the same Node
//I have made a slight change here . i searched for the nodes in the list individually.The reason is that i could figure
//out that any of them is head or not. int position = 1; while(currentNode!=null && position!=positionOfNode1){ previousOfNode1=currentNode; currentNode=currentNode.getNext(); }
node1=currentNode; //Till this point i know which node is node1 and which node is previousOfNode1.
currentNode=null;
position=1;
while(currentNode!=null && position!=positionOfNode2){ previousOfNode2=currentNode; currentNode=currentNode.getNext(); } node2=currentNode; //The next two if will have there else part run only in case when one of them is head.
//If node 1 is head ,set the node 2 as head (done in else part),else follow the normal procedure of switiching the links of previous node of 1 . if(previousOfNode1!=null) { previousOfNode1.setNext(node2); } else { head=node2; }
//If node 1 is head ,set the node 2 as head (done in else part),else follow the normal procedure of switiching the links of previous node of 1 . if(previousOfNode2!=null) { previousOfNode2.setNext(node1); } else { head=node1; } Node
/** * Adds an element to the front of the list. * @param e the new element to add */ public void addFirst(E e) { // adds element e to the front of the list head = new Node<>(e, head); // create and link a new node if (size == 0) tail = head; // special case: new node becomes tail also size++; }
/** * Adds an element to the end of the list. * @param e the new element to add */ public void addLast(E e) { // adds element e to the end of the list Node
/** * Removes and returns the first element of the list. * @return the removed element (or null if empty) */ public E removeFirst() { // removes and returns the first element if (isEmpty()) return null; // nothing to remove E answer = head.getElement(); head = head.getNext(); // will become null if list had only one node size--; if (size == 0) tail = null; // special case as list is now empty return answer; }
@SuppressWarnings({"unchecked"}) public boolean equals(Object o) { if (o == null) return false; if (getClass() != o.getClass()) return false; SinglyLinkedList> other = (SinglyLinkedList>) o; // use nonparameterized type if (size != other.size) return false; Node
@SuppressWarnings({"unchecked"}) public SinglyLinkedList
public int hashCode() { int h = 0; for (Node
/** * Produces a string representation of the contents of the list. * This exists for debugging purposes only. */ public String toString() { StringBuilder sb = new StringBuilder("("); Node
// list.addFirst("LAX"); System.out.println(list); //swap nodes & print them list.swapNodes(2,4); System.out.println(list); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
