public class SafeLinkedList { /* The head node of the list; never contains a real data item. */ private Node head; /* * The size of the list; we use a variable so we don't have to iterate * through the list to get the size. Needs to be updated in add/remove. */ private int size; public SafeLinkedList() { head = new Node(null, null); size = 0; } /** * Adds an element to a certain index. The index is similar to array * indexing, i.e., index 0 is the first element, etc. * * @param index The index to add the element at * @param elem The element to add to the list */ public void add(int index, Object elem) { if (index > size || index < 0) { throw new Error("Can't insert an item past the ends of the list"); } } /** * Removes an object from the list. The list is unaffected if the element * does not exist. * * @param elem The element to remove from the list */ public void remove(Object elem) { } /** * Gets an object at a certain index. * * @param index The index to remove */ public Object get(int index) { if (index > size || index < 0) { throw new Error("Can't retrieve an item past the ends of the list"); } } /** * Appends an element to the list. The same as calling add(list.size, elem) */ public void append(Object elem) { add(size, elem); } /** * Returns the size of the list. */ public int size() { return size; } /** * Removes all items from a list. */ public void clear() { head.next = null; size = 0; } public String toString() { String ret = ""; Node iter = head; for (int i = 0; i < size; i++, iter = iter.next) { ret += i + ": " + iter.next.element + "\n"; } return ret; } class Node { public Object element; public Node< next; public Node(Object elem, Node next) { this.element = elem; this.next = next; } } }