/** * A generic Doubly Linked List implementation. * * @author gtowell Created: Jan 2020 Modified: Nov 2020 */ public class DoubleLinkedList> implements LinkedListInterface { /** * The node class. Each element in the linked list is an instance of Node */ protected class Node> { /** The data item in the node. An instance of the data */ public Comparable data; /** The next item in the linked list */ public Node next; /** The previous item in the linked list */ public Node prev; /** * Node constructor. Takes a data item and the preceding and trailing nodes. */ public Node(Comparable data, Node prev, Node next) { this.data = data; this.next = next; this.prev = prev; } } /** The start, first element, of the linked list */ protected Node head = null; /** THe last element in the linked list */ protected Node tail = null; /** The number of items in the linked list */ protected 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 or null if list is empty */ @Override public Comparable first() { if (head == null) return null; return head.data; } /** * The last element in the linked list * * @return the last elment or null if list is empty */ @Override public Comparable last() { if (head == null) return null; return tail.data; } /** * Remove the first item from the list * * @return the first item, or null if the list is empty */ @Override public Comparable removeFirst() { if (head == null) return null; Comparable rtn = head.data; head = head.next; if (head == null) tail = null; else head.prev = null; size--; return rtn; } /** * Remove the last item from the list * * @return the last item, or null if the list is empty */ @Override @SuppressWarnings("unchecked") public Comparable removeLast() { if (head == null) return null; Comparable rtn = tail.data; if (head == tail) { head = null; size = 0; tail = null; return (T) rtn; } tail = tail.prev; tail.next = null; size--; return rtn; } /** * Remove the specified rabbit from the list * * @param r the item to be removed * @return the removed item, or null is the item was not in the list */ @Override @SuppressWarnings("unchecked") public Comparable remove(Comparable r) { // Do something much like find, but need to trak the previous node Node curr = head; while (curr != null) { if (0 == curr.data.compareTo((T) r)) { break; } curr = curr.next; } if (curr == null) { // 1. the rabbit was not found return null; } size--; if (curr.prev != null) curr.prev.next = curr.next; if (curr.next != null) curr.next.prev = curr.prev; if (curr == tail) tail = curr.prev; return r; } /** * Find a rabbit in the list by its iD * * @param iD * @return the found rabbit, or null if a match could not be found */ @Override @SuppressWarnings("unchecked") public Comparable find(Comparable rr) { Node curr = head; while (curr != null) { if (0 == curr.data.compareTo((T) rr)) { return (T) curr.data; } curr = curr.next; } return null; } public String toString() { StringBuffer s = new StringBuffer(); for (Node n = head; n != null; n = n.next) { s.append(n.data.toString()); s.append("\n"); } return s.toString(); } /** * Example usage with Rabbits * * @param args */ public static void main(String[] args) { DoubleLinkedList llr = new DoubleLinkedList<>(); Rabbit ar = new Rabbit(BreedEnum.Angora, "A1", 2); llr.addFirst(ar); llr.addFirst(new Rabbit(BreedEnum.DwarfDutch, "DD1", 3)); llr.addLast(new Rabbit(BreedEnum.FrenchLop, "FL1", 4)); System.out.println(llr); // llr.removeFirst(); // llr.removeLast(); llr.remove(ar); System.out.println(); System.out.println(llr); } /** * Add an element at the end of the list * * @param c the element to be added */ @Override public void addLast(Comparable c) { Node newNode = new Node<>(c, tail, null); if (head == null) { head = newNode; tail = newNode; size = 1; return; } tail.next = newNode; tail = newNode; size++; } /** * Add a element at the front of the list * * @param c the Element to be added */ @Override public void addFirst(Comparable c) { Node newNode = new Node<>(c, null, head); if (head == null) { head = newNode; tail = newNode; size = 1; return; } head.prev = newNode; head = newNode; size++; } }