CSC153, Class 43: List ADTs Overview: * Review: + Types of lists + List operations so far + Where is the "cursor" in a list? * More general list operations * More ordered list operations * More sorted list operations Notes: * Talk today at 4:15. Extra credit for attending. * Notes on extra credit available. * I'll be absent next Monday. In the long tradition of absent instructors, I will show a film. Please attend. * Questions on exam 3? A list is a collection of elements that you can iterate That is, you can look at the elements one by one Three kinds of lists * Simple Lists: client has no control over order of iteration * Ordered Lists: client precisely specifies order of iteration (add this element to the list *at this place*) * Sorted Lists: iteration is based on "sort ordering" (typically smallest to largest) Simple Lists * add(Object val) Add somewhere in the list? * Cursor getFront() * Cursor getEnd() * int getSize() * boolean emptyP() * remove(Object val) Remove all copies of value * removeAt(Cursor p) Ordered Lists * addAtEnd(Object val) * addAtBeginning(Object val) * addBefore(edu.grinnell.cs.cs153.2003S.section1.Cursor p, Object val) * addAfter(Cursor p, Object val) * add(Object val) Meaning changed to addAtEnd Cursors * advanceToNextElement * retreatToPreviousElement * getValue * changeValue * boolean atEndP * boolean atBeginningP /** * Advance to the next place in the list. * * @exception InvalidCursorException * when you try to advance beyond the end of the * list. */ public void next(); Question: What is a Cursor? * Option 1: The location of an element in the list. + Provide addBefore and addAfter + Less confusing to certain members of the class * Option 2: Between the locations of two elements in the list (or before first or after last) + Provide addAt + May be harder to implement + The inherited add makes more sense Question: Should lists have a "current" Cursor? * Option 1: Yes * Option 2: No. Cursors are values you can extract from lists and then operate on. Semi-Consensus: Cursors are "on" nodes. + May be easier to implement Side note: What happens in deletion if we have two Cursors that refer to the same point in the list E.g., Cursor arjun = students.getFirst(); Cursor victim = students.getFirst(); students.deleteAt(victim); /** * Delete a value referred to by a cursor. * @post: * The location is no longer specified. * the list. * Any other locations that were "the same" are no * longer specified. * Don't expect any existing interations to * be valid. */ Sorted Lists