import java.util.NoSuchElementException; /** * A LinkedList implementation. Tied to the Rabbit class rather than being * generic */ public class LinkedListOfRabbits 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. An instance of rabbit */ public Rabbit data; /** The next item in the linked list */ public Node next; /** * Node constructor. Takes a rannit and another node */ public Node(Rabbit 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 Rabbit first() throws NoSuchElementException { if (head == null) throw new NoSuchElementException("There are no rabbits"); return head.data; } /** * The last element in the linked list * * @return the last elment * @throws NoSuchElementException if the linked list is empty */ @Override public Rabbit last() throws NoSuchElementException { if (head == null) throw new NoSuchElementException("There are no rabbits"); return tail.data; } /** * Add a rabbit to the end of the list * * @param c the rabbit to be added */ @Override public void addLast(Rabbit c) { Node newest = new Node(c, null); if (isEmpty()) { head = newest; } else { tail.next = newest; } tail = newest; size++; } /** * Add a rabbit at the front of the list * * @param c the rabbit to be added */ @Override public void addFirst(Rabbit c) { Node newest = new Node(c, null); if (isEmpty()) { head = newest; tail = newest; } else { newest.next = head; } head = newest; size++; } /** * Remove the last rabbit from the list * * @return the last rabbit, or null if the list is empty */ @Override public Rabbit removeFirst() { if (head == null) return null; Rabbit rtn = head.data; head = head.next; if (head == null) tail = null; return rtn; } /** * Remove the last rabbit from the list * * @return the last rabbit, or null if the list is empty */ @Override public Rabbit removeLast() { if (head == null) return null; if (head == tail) { head = null; Rabbit 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; } Rabbit rtn = tail.data; tail = curr; tail.next = null; size--; return rtn; } /** * Remove the specified rabbit from the list * * @param r the rabbit to be removed * @return the removed rabbit, or null is the rabbit was not in the list */ @Override public Rabbit remove(Rabbit r) { // Do something much like find, but need to trak the previous node Node prev = null; Node curr = head; while (curr != null) { if (curr.data.equals(r)) { break; } prev = curr; curr = curr.next; } // three cases: if (curr == null) { // 1. the rabbit was not found return null; } if (prev == null) { // 2. the rabbit was the first item in the list size--; head = head.next; return r; } // 3. Rabbit is somewhere it list // special -- rabbit is at end of list size--; prev.next = curr.next; if (curr == tail) tail = prev; return null; } /** * 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 public Rabbit find(String iD) { Node curr = head; while (curr != null) { if (curr.data.getId().equals(iD)) { return 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()); if (n != tail) { s.append("\n"); } } return s.toString(); } public static void main(String[] args) { LinkedListOfRabbits llr = new LinkedListOfRabbits(); Rabbit ar = new Rabbit(BreedEnum.Angora, "A1"); llr.addFirst(ar); llr.addFirst(new Rabbit(BreedEnum.DwarfDutch, "DD1")); llr.addLast(new Rabbit(BreedEnum.FrenchLop, "FL1")); System.out.println(llr); // llr.removeFirst(); // llr.removeLast(); llr.remove(ar); System.out.println(); System.out.println(llr); } }