import java.util.NoSuchElementException; /** * A generic LinkedList implementation. * * @author gtowell Created: Sep 2019 Modified: Jan 2020 Modified: Nov 2020 */ public class LinkedList implements LinkedListInterface { /** * The node class. Each element in the linked list is an instance of Node */ private class Node { /** The data item in the node. */ public V data; /** The next item in the linked list */ public Node next; /** * Node constructor. Takes a data item and another node */ public Node(V data, Node next) { this.data = data; this.next = next; } } /** The start, first element, of the linked list */ private Node head = null; /** THe last element in the linked list */ private Node tail = null; /** The number of items in the linked list */ private int size = 0; /** * The number of items in the linked list * * @return the number of items in the linked list */ @Override public int size() { return size; } /** * Returns true iff the linked list has no elements * * @return true iff the linked list has no elements */ @Override public boolean isEmpty() { return size == 0; } /** * Get the first element in the linked list * * @return the first element in the linked list * @throws NoSuchElementException is the list is empty */ @Override public T first() throws NoSuchElementException { if (head == null) throw new NoSuchElementException("The list is empty"); return head.data; } /** * The last element in the linked list * * @return the last elment * @throws NoSuchElementException if the linked list is empty */ @Override public T last() throws NoSuchElementException { if (head == null) throw new NoSuchElementException("The list is empty"); return tail.data; } /** * Add an element to the end of the list * * @param c the element to be added */ @Override public void addLast(T c) { Node newest = new Node<>(c, null); if (isEmpty()) { head = newest; } else { tail.next = newest; } tail = newest; size++; } /** * Add an element at the front of the list * * @param c the element to be added */ @Override public void addFirst(T c) { Node newest = new Node<>(c, null); if (isEmpty()) { head = newest; tail = newest; } else { newest.next = head; } head = newest; size++; } /** * Remove the first element from the list * * @return the first element, or null if the list is empty */ @Override public T removeFirst() { if (head == null) return null; T rtn = head.data; head = head.next; if (head == null) tail = null; return rtn; } /** * Remove the last from the list * * @return the last element, or null if the list is empty */ @Override public T removeLast() { if (head == null) return null; if (head == tail) { head = null; T rtn = tail.data; size = 0; tail = null; return rtn; } // start at front and get to the node prior to the last Node curr = head; while (curr.next != tail) { curr = curr.next; } T rtn = tail.data; tail = curr; tail.next = null; size--; return rtn; } /** * Remove the specified element from the list * * @param r the element to be removed * @return the removed element, or null is the element was not in the list */ @Override public boolean remove(T r) { return false; } public String toString() { StringBuffer s = new StringBuffer(); for (Node n = head; n != null; n = n.next) { s.append(n.data.toString()); if (n != tail) { s.append("\n"); } } return s.toString(); } public static void main(String[] args) { } }