Question: Java Programming: I am currently stuck on a question and I know I am over thinking it. Any help is appreciated. Questions: b.) Write the
Java Programming:
I am currently stuck on a question and I know I am over thinking it. Any help is appreciated.
Questions:
b.) Write the following remove() method for the class KWSingleLinkedList without using helper methods including add(), addFirst(), addAfter(), remove(), removeFirst(), removeAfter(). You can use this.head to get head of a linked list, you can also use methods getNode(index), getHead() and setHead() if you need them.
For reference, here 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) { return -1; } /** Return the size of the linked list. */ public int getSize() { return size; }