import ciips.animation.*; import java.awt.*; public class ListLinked implements DrawingObj { private int x, y; private AlgAnimFrame frame; /** * Sentinel link reference before the start of the list */ private Link before; /** * Sentinel link reference after the end of the list */ private Link after; /** * Initialise a new linked list * The list is initially empty * The before link references the after link. */ public ListLinked() { after = new Link(null,null); before = new Link(null, after); } /** * Test to determine if the linked list is an empty list * @return true if the list is empty */ public boolean isEmpty() { return before.successor == after; } /** * Test to determine if the window is at the before first position * @param w is the reference to the window w * @return true if the window is at the before first position */ public boolean isBeforeFirst(WindowLinked w) { return w.link == before; } /** * Test to determine if the window is at the after last position * @param w is the reference to the window w * @return true if the window is at the after last position */ public boolean isAfterLast(WindowLinked w) { return w.link == after; } /** * Sets the window to be at the before first position * @param w is the reference to the window w */ public void beforeFirst(WindowLinked w) { w.link = before; } /** * Sets the window to be at the after last position * @param w is the reference to the window w */ public void afterLast(WindowLinked w) { w.link = after; } /** * Moves the window to the immediate right of its current position on the list * @param w is the reference to the window * @exception OutOfBoundsException if the window w is at the afterLast link position */ public void next(WindowLinked w) throws OutOfBoundsException { if(!isAfterLast(w)) w.link = w.link.successor; else throw new OutOfBoundsException("Calling next after end of list."); } /** * Moves the window w to the immediate left of its current position on the list * @param w is the reference to the window * @exception OutOfBoundsException if the window w is at the beforeFirst link position */ public void previous(WindowLinked w) throws OutOfBoundsException { if(!isBeforeFirst(w)) { Link current = before.successor; Link previous = before; while(current != w.link) { previous = current; current = current.successor; } w.link = previous; } else throw new OutOfBoundsException("Calling previous when window is at the before first position"); } /** * Insert an Object to the immediate right of the current window position * @param e is the Object to insert. * @param w is the reference to the window * @exception OutOfBoundsException if window w is at the beforeFirst or afterLast link position */ public void insertAfter(Object e, WindowLinked w) throws OutOfBoundsException { if(!isAfterLast(w)) { Link toLink = w.link.successor; w.link.successor = new Link(e, toLink); } else if (isAfterLast(w)){ previous(w); w.link.successor = new Link(e, after); } else throw new OutOfBoundsException("Attempt to insert after the end of the list"); } /** * Insert an Object to the immediate left of the current window position * @param e is the Object to insert. * @param w is the reference to the window * @exception OutOfBoundsException if window w is at the beforeFirst or afterLast link position */ /*-------------------------------------------------------------------------*/ public void insertBefore(Object e, WindowLinked w) throws OutOfBoundsException { /*-*/ frame.Highlight(0); if(!isBeforeFirst(w)) { /*-*/ frame.Highlight(1); w.link.successor = new Link(w.link.item, w.link.successor); if(isAfterLast(w)) after = w.link.successor; w.link.item = e; w.link = w.link.successor; /*-*/ frame.waitStep(); } else throw new OutOfBoundsException("Inserting b4 the start of the list."); } /** * Examines the Object that the current window is referencing * @param w is the reference to the window * @return the Object that the current window is referencing * @exception OutOfBoundsException if window w is at the beforeFirst or afterLast link position * @exception UnderFlow if the list is empty */ public Object examine(WindowLinked w) throws OutOfBoundsException, UnderflowException { if(!isEmpty()) { if(!isBeforeFirst(w) && !isAfterLast(w)) return w.link.item; else throw new OutOfBoundsException("Window is over a non-data node."); } else throw new UnderflowException("Attempt to examine a list that is empty."); } /** * Replace the Object in the list which the current window is referencing * @param e is the new Object that will replace the current Object. * @param w is the reference to the window * @exception OutOfBoundsException if window w is at the beforeFirst or afterLast link position * @exception UnderFlow if the list is empty */ public void replace(Object e, WindowLinked w) throws OutOfBoundsException, UnderflowException { if(!isEmpty()) { if(!isBeforeFirst(w) && !isAfterLast(w)) w.link.item = e; else throw new OutOfBoundsException("Attempt to examine an item that is out ofbounds."); } else throw new UnderflowException("Attempt to replace in an empty list"); } /** * Deletes the first link in the list * @exception UnderFlow if the list is empty */ public void deleteFromFront() throws UnderflowException { if(!isEmpty()) before.successor = before.successor.successor; else throw new UnderflowException("Attempt to delete from an empty list."); } /** * Deletes the last link in the list * @exception UnderFlow if the list is empty */ public void deleteFromBack() throws UnderflowException { if(!isEmpty()) { Link follower = before; Link current = before.successor; while(current.successor!= after) { follower = current; current = current.successor; } follower.successor = after; } else throw new UnderflowException("Attempt to delete from an empty list."); } /** * Prints all the Objects in the list. * @exception UnderFlow if the list is empty */ public void printList() throws UnderflowException { if(!isEmpty()) { Link current = before.successor; while(current != after) { System.out.println(current.item); current = current.successor; } } else throw new UnderflowException("Attempt to print an empty queue"); } public void draw( Graphics g ) { Link h = before; while ( h != null ) { h.draw( g ); h = h.successor; } } public void move( int x, int y ) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } public void initBeforeOffsc( AlgAnimFrame frame, Object t ) { this.frame = frame; } } // end of class definition.