import java.util.*; class LinkedList { protected LinkedListEntry start = null; protected LinkedListEntry end = null; protected int numElements; public void addElement(Object obj) { // Make sure the object is valid if (obj == null) throw new NullPointerException(); // Create the new entry LinkedListEntry newElement = new LinkedListEntry(obj); numElements++; // See if the new element is the start of the list if (start == null) { start = newElement; end = newElement; } else { end.next = newElement; newElement.prev = end; end = newElement; } } public void insertElementAt(Object obj, Object pos) { // Make sure the objects are valid if (obj == null || pos == null) throw new NullPointerException(); // Make sure the position object is in the list LinkedListEntry posEntry = find(pos); if (posEntry == null) throw new NullPointerException(); // Create the new entry LinkedListEntry newElement = new LinkedListEntry(obj); numElements++; // Link in the new entry newElement.next = posEntry; newElement.prev = posEntry.prev; if (posEntry == start) start = newElement; else posEntry.prev.next = newElement; posEntry.prev = newElement; } public boolean removeElement(Object obj) { // Make sure the object is valid if (obj == null) throw new NullPointerException(); // Make sure the object is in the list LinkedListEntry delEntry = find(obj); if (delEntry == null) return false; // Unlink the entry numElements--; if (delEntry == start) start = delEntry.next; else delEntry.prev.next = delEntry.next; if (delEntry == end) end = delEntry.prev; else delEntry.next.prev = delEntry.prev; return true; } private LinkedListEntry find(Object obj) { // Make sure the list isn't empty and the object is valid if (isEmpty() || obj == null) return null; // Search the list for the object LinkedListEntry tmp = start; while (tmp != null) { if (tmp.val == obj) return tmp; tmp = tmp.next; } return null; } public boolean contains(Object obj) { return (find(obj) != null); } public boolean isEmpty() { return (start == null); } public Enumeration elements() { return new LinkedListEnumerator(this); } }