1 / 31

The Queue

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

sheena
Download Presentation

The Queue

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Queue

  2. 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

  3. 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

  4. 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

  5. implementation • instance variables • count • constructors • empty state • methods • enter • overflow • increment • leave, front • underflow • length, empty • compute? • empty vs full

  6. 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

  7. 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

  8. The List

  9. 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

  10. 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

  11. 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

  12. List Interface • operations • insertion • deletion • access • length • traversal • search • off end? • exceptions • NoSpaceException • NoItemException

  13. 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

  14. 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

  15. 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 RL • cursor

  16. deletion • fill gap • shift items LR • 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

  17. 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)

  18. 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?

  19. 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)

  20. 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

More Related