Queues - PowerPoint PPT Presentation

1 / 42
About This Presentation
Title:

Queues

Description:

compare queue implementations. 1-3. Queues ... the cars at a stop light. an assembly line. 1-4. Conceptual View of a Queue. 1-5 ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 43
Provided by: jasonm6
Category:
Tags: queues

less

Transcript and Presenter's Notes

Title: Queues


1
Topic 6
  • Queues
  • (dont need all examples)

2
Chapter Objectives
  • examine queue processing
  • define a queue abstract data type
  • demonstrate how a queue can be used to solve
    problems
  • examine various queue implementations
  • compare queue implementations

3
Queues
  • queue a collection whose elements are added at
    one end (the rear or tail of the queue) and
    removed from the other end (the front or head of
    the queue)
  • a queue is a FIFO (first in, first out) data
    structure
  • examples any waiting line is a queue
  • the check out line at a grocery store
  • the cars at a stop light
  • an assembly line

4
Conceptual View of a Queue
5
Computer System Examples
  • printer queue
  • keyboard input buffer
  • GUI event queue

6
Queue Operations
  • enqueue add an element to the tail of a queue
  • dequeue remove an element from the head of a
    queue
  • peek examine the element at the head of the
    queue
  • it is not legal to access the elements in the
    middle of the queue

7
Operations on a Queue
  • see QueueADT.java

8
The QueueADT Interface in UML
9
Interface to a Queue in Java
  • See QueueADT.java

10
Using Queues
  • queues are useful for any kind of problem
    involving FIFO data
  • example simulation problems
  • exampleencoding messages
  • example ticket counter simulation
  • example radix sort?

11
Example Coded Messages
  • a Ceasar cipher encodes a message by shifting
    each letter in a message by a constant amount k
  • if k is 5, a becomes f, b becomes g, etc
  • this code is fairly easy to break!
  • an improvement change how much a letter is
    shifted depending on where the letter is in the
    message

12
Coded Messages (contd)
  • a repeating key is a series of integers that
    determine how much each character is shifted
  • for example, consider the repeating key
  • 3 1 7 4 2 5
  • the first character in the message is shifted 3,
    the next 1, the next 7, and so on
  • when the key is exhausted, start over at the
    beginning of the key

13
An Encoded Message Using a Repeating Key
14
Coded Messages (contd)
  • use a queue to store the values of the key
  • dequeue a key value when needed
  • after using it, enqueue it back onto the end of
    the queue
  • so, the queue represents the constantly cycling
    values in the key
  • see codes.java

15
UML Description of Codes Program
16
Ticket Counter Simulation
  • simulate the waiting line at a movie theatre
  • determine how many cashiers are needed to keep
    the customer wait time under 7 minutes
  • assume
  • customers arrive on average every 15 seconds
  • processing a request takes two minutes once a
    customer reaches a cashier
  • see customer.java
  • see TicketCounter.java

17
UML Description of TicketCounter Program
18
Results of Ticket Counter Simulation
19
Radix Sort
  • a radix sort uses queues to order a set of values
  • a queue is created for each possible value in the
    sort key
  • example if the sort key was a lowercase
    alphabetic string, there would be 26 queues
  • example if the sort key was a decimal integer,
    there would be 10 queues corresponding to the
    digits 0 through 9

20
Radix Sort (contd)
  • each pass through the sort examines a particular
    position in the sort value
  • the element is put on the queue corresponding to
    that position's value
  • processing starts with the least significant
    position (1s) to the most significant position
  • the following example uses integers with only the
    digits 0 through 5

21
A Radix Sort of Ten Three-digit Numbers
22
Implementation of Radix Sort
  • we maintain an array of 10 queues, one for each
    digit (0-9)
  • see RadixSort.java

23
UML description of RadixSort program
24
Queue Implementation using a Linked List
  • internally, the queue is represented as a linked
    list of nodes, with
  • each node containing a data element
  • two references
  • a pointer to the beginning of the list
  • a pointer to the end of the list
  • a count of the number of nodes in the queue

25
Linked Implementation of a Queue
26
Queue After Adding Element E
27
Queue After a dequeue Operation
28
Java Implementation
  • we will implement a general queue of Objects
  • the queue is represented as a linked list of
    nodes
  • we will again use the LinearNode class
  • front is a reference to the head of the queue
    (beginning of the list)
  • rear is a reference to the tail of the queue (end
    of the list)
  • the integer count is the number of nodes in the
    queue

29
The enqueue Operation
//------------------------------------------------
----------------- // Adds the specified element
to the rear of the queue. //----------------------
------------------------------------------- public
void enqueue (Object element) LinearNode
temp new LinearNode (element) if
(isEmpty()) front node else
rear.setNext (node) rear node
count
30
The dequeue Operation
//------------------------------------------------
----------------- // Removes the element at the
front of the queue and returns a // reference to
it. Throws an EmptyCollectionException if the //
queue is empty. //--------------------------------
--------------------------------- public Object
dequeue () throws EmptyCollectionException
if (isEmpty()) throw new EmptyCollectionExce
ption ("queue") Object result
front.getElement() front front.getNext()
count-- if (isEmpty()) rear null
return result
31
Queue Implementation using an Array
  • First Approach
  • use an array in which index 0 represents one end
    of the queue
  • integer value rear represents the next open slot
    in the array (and also the number of elements
    currently in the queue)
  • Discussion What is the challenge with this
    approach?

32
An Array Implementation of a Queue
33
Queue After Adding Element E
34
Queue After Removing First Element
35
The enqueue Operation
//------------------------------------------------
----------------- // Adds the specified element
to the rear of the queue, expanding // the
capacity of the queue array if necessary. //------
--------------------------------------------------
--------- public void enqueue (Object element)
if (size() queue.length)
expandCapacity() queuerear element
rear
36
The dequeue Operation
//------------------------------------------------
----------------- // Removes the element at the
front of the queue and returns a // reference to
it. Throws an EmptyCollectionException if the //
queue is empty. //--------------------------------
--------------------------------- public Object
dequeue () throws EmptyCollectionException
if (isEmpty()) throw new EmptyCollectionExce
ption ("queue") Object result queue0
// shift the elements for (int scan0 scan lt
rear scan) queuescan
queuescan1 rear-- queuerear
null return result
37
Second Approach Queue as a Circular Array
  • if we don't fix one end of the queue at index 0,
    we won't have to shift the elements!
  • circular array is an array that conceptually
    loops around on itself
  • the last index is thought to precede index 0
  • need to keep track of where the front and rear of
    the queue are at any given time

38
Circular Array Implementation of a Queue
39
A Queue Straddling the End of a Circular Array
40
Changes in a Queue
41
Circular Array (contd)
  • when an element is enqueued, the value of rear is
    incremented
  • but it must take into account the need to loop
    back to 0
  • rear (rear1) queue.length
  • Can this array implementation also reach
    capacity?

42
Analysis of Queue Operations
  • the linked implementation of a queue does not
    suffer from the need to operate on both ends of
    the queue
  • enqueue operation
  • O(1) for all implementations
  • dequeue operation
  • O(1) for linked implementation
  • O(1) for circular array implementation
  • O(n) for noncircular array implementation
Write a Comment
User Comments (0)
About PowerShow.com