320 likes | 464 Views
The Queue. Queue. a list (initially empty) of items (of some type) to which items may be added at one end (called the rear ) and from which items may be removed at the other end (called the front ) examples waiting lines print queues behaviour FIFO ordering error conditions: underflow
E N D
Queue • a list (initially empty) of items (of some type) to which items may be added at one end (called the rear) and from which items may be removed at the other end (called the front) • examples • waiting lines • print queues • behaviour • FIFO ordering • error conditions: • underflow • overflow
Queue Interface • generic • E – items to be stored • operations: • enter (enqueue, add, insert) • leave (dequeue, remove, delete) • front (head, first) • length (count, size) • empty • exceptions • NoItemException • NoSpaceException
Queue ADTContiguous Implementation • based on variable-sized array • two indices: front & rear • add at rear, remove at front • queue moves towards rear • repositioning on delete: O(n) • circular array • at end of array reuse front • index modulo array size
implementation • instance variables • count • constructors • empty state • methods • enter • overflow • increment • leave, front • underflow • length, empty • compute? • empty vs full
Queue ADTLinked Implementation • sequentially-linked structure of items • deletion from front • insertion at end • keep pointer to rear O(1) • length? • keep count else O(n) • comparison with contiguous • all operations O(1) • space tradeoffs
Java Collections Framework • Queue interface • generic in element type (E) • some duplication of methods (Collection) • standard queue methods • add = enter • remove = leave • element = front • size = length • isEmpty = empty • LinkedList class • generic in element type (E) • implements Queue as symmetrically-linked structure • also implements List
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 • operations • insertion • deletion • access • length • traversal • search • off end? • exceptions • NoSpaceException • NoItemException
Bounded Type Parameter • List is generic in E • bounded type parameter <E extends Keyed> • E has additional properties beyond Object • as defined by Keyed interface • within class or interface E is treated as Keyed • actual type parameter must be subtype of Keyed • unbound • <E> means <E extends Object> • Keyed interface • getKey
Iterators • a device that allows traversal through a collection ADT • ADT extends Iterable<E> (from java.lang) • 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 • iterator • needs pointer to current node in iteration • comparison with contiguous • insert/remove O(1) vs O(n)
Java Collections Framework • List interface • generic in element type (E) • some duplication of methods (Collection) • defines an indexed list • like an array • list methods • add • remove • get • set • indexOf • LinkedList class • implements List as symmetrically-linked structure • ArrayList class • implements List as a resizeable array