Question: public class SinglyLinkedList implements Cloneable { private static class Node { private E element; // reference to the element stored at this node private Node
public class SinglyLinkedList
private E element; // reference to the element stored at this node
private Node
public Node(E e, Node
public E getElement() { return element; }
public Node
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
public int size() { return size; }
public boolean isEmpty() { return size == 0; }
public E first() { // returns (but does not remove) the first element if (isEmpty()) return null; return head.getElement(); }
public E last() { // returns (but does not remove) the last element if (isEmpty()) return null; return tail.getElement(); }
// update methods 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++; }
public void addLast(E e) { // adds element e to the end of the list Node
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 walkA = head; // traverse the primary list Node walkB = other.head; // traverse the secondary list while (walkA != null) { if (!walkA.getElement().equals(walkB.getElement())) return false; //mismatch walkA = walkA.getNext(); walkB = walkB.getNext(); } return true; // if we reach this, everything matched successfully }
@SuppressWarnings({"unchecked"}) public SinglyLinkedList
public int hashCode() { int h = 0; for (Node walk=head; walk != null; walk = walk.getNext()) { h ^= walk.getElement().hashCode(); // bitwise exclusive-or with element's code h = (h << 5) | (h >>> 27); // 5-bit cyclic shift of composite code } return h; }
public String toString() { StringBuilder sb = new StringBuilder("("); Node
In this exercise, you will add a method swapNodes to SinglyLinkedList class from above examples. This method should swap two nodes node1 and node2 (and not just their contents) given references only to node1 and node2. The new method should check if node1 and node2 are the same node, etc. Write the main method to test the swapNodes method. Hint: You may need to traverse the list.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
