public class LinkedList { Item head; Item tail; public LinkedList() { head = null; tail = null; } // Add an item to the end of the list public void add(Item x) { if (tail == null) { tail = x; head = x; } else { tail.next = x; x.previous = tail; tail = x; x.next = null; } } // Remove the given item from the list public void remove(Item x) { if (x == head) { if (x == tail) { head = tail = null; } else { head = x.next; head.previous = null; } } else { if (x == tail) { tail = x.previous; tail.next = null; } else { x.previous.next = x.next; x.next.previous = x.previous; } } } // Return a string representation of the list public String toString() { if (head == null) return "[EMPTY]"; String s = "[H:"; Item currentItem = head; while (currentItem != null) { s += currentItem.data; if (currentItem != tail) s += "]<==>["; currentItem = currentItem.next; } return s + ":T]"; } // add up the total data in the list public int totalData() { if (head == null) return 0; int total = 0; Item currentItem = head; while (currentItem != null) { total += currentItem.data; currentItem = currentItem.next; } return total; } }