120 likes | 249 Views
The List. If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping your mouth shut. Albert Einstein, Observer, Jan. 15, 1950. List. most general of list-oriented collections
E N D
The List If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping your mouth shut. Albert Einstein, Observer, Jan. 15, 1950
List • most general of list-oriented collections • an ordered collection (initially empty) of items (of some type) to which items may be added and removed. • cursored-list • cursor • operations relative to cursor • off list • errors • off list • list overflow
List Operations • insertion? • before or after cursor? • at front • at end? • access • at cursor • off list? • deletion • at cursor • off list? • cursor after deletion • inverse of addition
traversal • moving to front • advancing • end of list • search • key • from where • exhaustive search • list as stack • insert without advance • list as queue • insert with traverse to end • sequential order • insert with advance • sorted • traverse to find insertion point
List Interface • generic • E • getKey • Comparable from java.lang • operations • insertion • deletion • access • length • traversal • search • off end? • iterator • exceptions • NoSpaceException • NoItemException
Iterators • a device that allows traversal through a collection ADT • ADT extends Iterable<E> (from java.util) • iterator returns an Iterator over the ADT • interface Iterator<E> in java.util • methods • UnsupportedOperationException • extended for • implementation • must have intimate knowledge of ADT representation • place in same package & use package visibility in ADT
List ADTContiguous Representation • “variable-sized” array • contiguity • reorganization (O(n)) • cursor is an index • instance variables • constructors • empty list • operations • insertion • create opening • shift items RL • cursor
deletion • fill gap • shift items LR • cursor • removal of reference at length-1 • find • goal: move cursor • check each item in turn until found or off list • short circuit operator • negation using de Morgan’s law • iterator • create an iterator on this ADT • remaining operations
ConListIterator • generic in item type (same as ConList) • iteration involves sequencing through index positions from 0 to length-1 • instance variables • list it is iterating • has its own cursor • must keep track of current position • constructor • package visibility • only classes in package can create • initialize cursor • methods • access ConList instance variables directly (package visibility)
List ADTLinked Implementation • sequentially-linked structure • sequential processing natural • cursor? • cursor is Node reference • off list • insertion • in front of cursor • precursor • cursor pairs • header node • at end?
representation • sequentially linked structure with header and two cursors • empty list • off list? • length • keep count • comparison with contiguous • insert/remove O(1) vs O(n)