Queue ADT - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Queue ADT

Description:

Given the operations (behaviors) what are the assumptions ... Java's Queue Interface. Java's AbstractQueue. Must implement. offer(E) peek() poll() size ... – PowerPoint PPT presentation

Number of Views:75
Avg rating:3.0/5.0
Slides: 24
Provided by: bal1
Category:
Tags: adt | javas | meaningless | queue

less

Transcript and Presenter's Notes

Title: Queue ADT


1
Queue ADT
  • IC312
  • A Linear Data Type

2
Queue ADT
  • Queue might bring to mind something like this

What can we do to a Queue like this?
3
Queue 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

4
Queue ADT
  • We can think of our list like this

a line where the first one in is the first one
out
5
Queue 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

6
Specifications (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

7
Specifications (accessers)
  • front
  • Returns element from front of Q
  • getSize
  • Returns number of elements in Q
  • isEmpty
  • Returns true if Q is empty

8
Adding Entries
  • We always add (enqueue) to the back

Back
Front
9
Removing Entries
  • We always remove (dequeue) from the front

Back
Front
10
Different Situations
  • We must consider different possible situations
  • For Example
  • Is enqueueing onto an empty queue different than
    a populated queue?
  • What about dequeueing?

11
Problem 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?

12
Interface
  • Defines methods a class must implement

interface QueueltEgt bool enqueue(E entry) E
dequeue() E front() int getSize() bool
isEmpty()
13
Implementation
  • Must implement all methods in interface
  • Need underlying data structure Something to
    hold the data
  • Can use array or list

14
Array Implementation
  • We may have problem called right drift
  • If we do not shift elements in queue

Front
Back
15
Right 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

16
Circular Array
  • Turn this
  • into this

17
How
  • Wrap the index around
  • front (front1)MAX_SIZE
  • rear (rear1)MAX_SIZE

18
Array-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
19
Queue Operations
  • We use the modulo operator (remainder of division)

Algorithm size() return (N - f r) mod
N Algorithm isEmpty() return (f r)
20
Queue 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

21
Queue 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

22
Implementation
  • Javas Queue Interface
  • Javas AbstractQueue
  • Must implement
  • offer(E)
  • peek()
  • poll()
  • size()
  • iterator()

23
Uses
  • Keeping track of jobs
  • To-do lists
  • Printers
Write a Comment
User Comments (0)
About PowerShow.com