Question: Java Programming: The following is my code: public class KWSingleLinkedList { public void setSize(int size) { this.size = size; } /** Reference to list head.
Java Programming:
The following is my code:
public class KWSingleLinkedList { public void setSize(int size) { this.size = size; }
/** Reference to list head. */ private Node head = null; /** The number of items in the list */ private int size = 0; /** Add an item to the front of the list. @param item The item to be added */ public void addFirst(E item) { setHead(new Node<>(item, getHead())); size++; } /** Add a node after a given node @param node The node preceding the new item @param item The item to insert */ private void addAfter(Node node, E item) { node.next = new Node<>(item, node.next); size++; } /** Remove the node after a given node @param node The node before the one to be removed @return The data from the removed node, or null if there is no node to remove */ @SuppressWarnings("unused") private E removeAfter(Node node) { Node temp = node.next; if (temp != null) { node.next = temp.next; size--; return temp.data; } else { return null; } } /** Remove the first node from the list @return The removed node's data or null if the list is empty */ @SuppressWarnings("unused") private E removeFirst() { Node temp = getHead(); if (getHead() != null) { setHead(getHead().next); } // Return data at old head or null if list is empty if (temp != null) { size--; return temp.data; } else { return null; } } /** Find the node at a specified position @param index The position of the node sought @return The node at index or null if it does not exist */ private Node getNode(int index) { Node node = getHead(); for (int i = 0; i < index && node != null; i++) { node = node.next; } return node; } /** Get the data at index @param index The position of the data to return @return The data at index @throws IndexOutOfBoundsException if index is out of range */ public E get(int index) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException(Integer.toString(index)); } Node node = getNode(index); return node.data; } /** Store a reference to anEntry in the element at position index. @param index The position of the item to change @param newValue The new data @return The data previously at index @throws IndexOutOfBoundsException if index is out of range */ public E set(int index, E newValue) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException(Integer.toString(index)); } Node node = getNode(index); E result = node.data; node.data = newValue; return result; } /** Insert the specified item at index @param index The position where item is to be inserted @param item The item to be inserted @throws IndexOutOfBoundsException if index is out of range */ public void add(int index, E item) { if (index < 0 || index > size) { throw new IndexOutOfBoundsException(Integer.toString(index)); } if (index == 0) { addFirst(item); } else { Node node = getNode(index-1); addAfter(node, item); } } /** Append item to the end of the list @param item The item to be appended @return true (as specified by the Collection interface) */ public boolean add(E item) { add(size, item); return true; } public Node getHead() { return head; }
public void setHead(Node head) { this.head = head; } public String toString() { @SuppressWarnings("unchecked") Node nodeRef = (Node) head; StringBuilder result = new StringBuilder(); while (nodeRef != null) { result.append(nodeRef.data); if (nodeRef.next != null) { result.append(" ==> "); } nodeRef = nodeRef.next; } return result.toString(); }
public int remove(E item) { if(head == null) { return -1; } if(head.data.equals(item)) { head = head.next; size--; return 0; } int index = 1; Node node = head; while(node.next != null) { if(node.next.data.equals(item)) { node.next = node.next.next; size--; return index; } } return -1; } /** Return the size of the linked list. */ public int getSize() { return size; }
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
I need help implementing the following method:
/** Insert a new item before the one at position index, starting at 0 for the list head. The new item is inserted between the one at position index-1 and the one formerly at position index. If the index is zero, add the new item to the first @param index The index where the new item is to be inserted @param item The item to be inserted @throws IndexOutOfBoundsException if the index is out of range */ // You should take a look at addAfter(Node node, E item) // You need to consider two situations: index == 0 and index > 0 // Hint: // 1. Check whether the index is valid // 2. if index == 0, create a new node with item and refer to this.head // and set the new node to this.head, using the constructor: Node(E dataItem, Node nodeRef): // this.head = new Node<>(item, this.head); // 3. Otherwise, Nodenode = getNode(index-1), and use item and node.next to create a new node // and give the new node to node.next. // 4. Don't forget to update the size public void addBefore(int index, E item) { // Add your code here }