Title: Queue ADT
1Queue ADT
2Queue ADT
- Queue might bring to mind something like this
What can we do to a Queue like this?
3Queue ADT Assumptions
- Given the operations (behaviors) what are the
assumptions/restrictions on the data? - All data is of same type
- Data is in some specific order
- First added is at front, last at back
- What assumptions have we made about data storage?
- Relative order of data is maintained
4Queue ADT
- We can think of our list like this
a line where the first one in is the first one
out
5Queue ADT Operations
- Things we can do with a queue
- Add entries to back
- Remove from front
- Display entry at front
- Display ALL entries
- Get size of Queue
- Find out if Queue is empty
6Specifications (modifiers)
- Enqueue (add)
- Adds element to back of Q unless Q is full
- Returns nothing
- Dequeue (remove)
- Removes element from front of Q unless Q is empty
- Returns element from front
7Specifications (accessers)
- front
- Returns element from front of Q
- getSize
- Returns number of elements in Q
- isEmpty
- Returns true if Q is empty
8Adding Entries
- We always add (enqueue) to the back
Back
Front
9Removing Entries
- We always remove (dequeue) from the front
Back
Front
10Different Situations
- We must consider different possible situations
- For Example
- Is enqueueing onto an empty queue different than
a populated queue? - What about dequeueing?
11Problem Situations
- We must address potential problem situations
- Dequeueing from an empty queue is meaningless
- Displaying the front of an empty queue is
meaningless - What if queue has a fixed size and is full?
12Interface
- Defines methods a class must implement
interface QueueltEgt bool enqueue(E entry) E
dequeue() E front() int getSize() bool
isEmpty()
13Implementation
- Must implement all methods in interface
- Need underlying data structure Something to
hold the data - Can use array or list
14Array Implementation
- We may have problem called right drift
- If we do not shift elements in queue
Front
Back
15Right Drift
- We can have an empty array with no place to put a
new element - Head has drifted to the right
- Solutions?
- Shift elements
- Virtually shift elements
16Circular Array
17How
- Wrap the index around
- front (front1)MAX_SIZE
- rear (rear1)MAX_SIZE
18Array-based Queue
- Use an array of size N in a circular fashion
- Two variables keep track of the front and rear
- f index of the front element
- r index immediately past the rear element
- Array location r is kept empty
normal configuration
wrapped-around configuration
19Queue Operations
- We use the modulo operator (remainder of division)
Algorithm size() return (N - f r) mod
N Algorithm isEmpty() return (f r)
20Queue Operations (cont.)
Algorithm enqueue(o) if size() N ? 1
then throw FullQueueException else Qr ?
o r ? (r 1) mod N
- Operation enqueue throws an exception if the
array is full - This exception is implementation-dependent
21Queue Operations (cont.)
Algorithm dequeue() if isEmpty() then throw
EmptyQueueException else o ? Qf f ? (f
1) mod N return o
- Operation dequeue throws an exception if the
queue is empty - This exception is specified in the queue ADT
22Implementation
- Javas Queue Interface
- Javas AbstractQueue
- Must implement
- offer(E)
- peek()
- poll()
- size()
- iterator()
23Uses
- Keeping track of jobs
- To-do lists
- Printers