package rebelsky.list; /** * An implementation of lists using linked nodes. Based closely * on the * * @author CSC152 2004F * @version 1.0 of October 2004. */ public class SimpleLinkedList implements SimpleList { // +--------+-------------------------------------------------- // | Fields | // +--------+ /** The "first" thing. */ Node front; // +--------------+-------------------------------------------- // | Constructors | // +--------------+ public SimpleLinkedList() { this.front = null; } // LinkedList() // +---------+------------------------------------------------- // | Methods | // +---------+ public void add(Object addMe) { // Add it to the front of the list. this.front = new Node(addMe, this.front); } // put(Object) /** * Get a new cursor for the list. */ public SimpleCursor getSimpleCursor() { return new SimpleNodeCursor(this); } // getSimpleCursor() /** * Get a new cursor for the list. */ public Cursor getCursor() { // STUB return null; } // getCursor() /** * Remove the object last returned by cursor c. * * @pre * where is a cursor for this list. * where.next() has been called. */ public void remove(SimpleCursor where) { // Cast where to the right kind of simple cursor SimpleNodeCursor here = (SimpleNodeCursor) where; // If it's the first node, just remove the first one. if (here.pos == this.front) { System.err.println("DELETING THE FIRST ELEMENT"); this.front = this.front.next; return; } System.err.println("DELETING SOME OTHER ELEMENT"); // Find the previous item Node prev = this.front; while (prev.next != here.pos) prev = prev.next; // Make the previous item link to the next item prev.next = here.pos.next; } // remove(SimpleCursor) /** * Convert this list to a string for ease of printing. */ public String toString() { String result = "["; Node tmp = this.front; while (tmp != null) { result = result + tmp.data + " "; tmp = tmp.next; } // while result = result + "]"; return result; } // toString() } // class SimpleLinkedList