70 likes | 242 Views
The Queue. Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find out how to do it. Theodore Roosevelt. Queue.
E N D
The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find out how to do it. Theodore Roosevelt
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 • process queue • behaviour • FIFO ordering • error conditions: • underflow • overflow
Queue Interface • generic (Stores objects of type E) • no requirements • 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
Queue ADT. • 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