49
Chapter 7 Queues

Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Embed Size (px)

Citation preview

Page 1: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Chapter 7

Queues

Page 2: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-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

Page 3: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-3

Queues• A queue is a collection whose elements are

added on one end and removed from the other

• Therefore a queue is processed in a FIFO fashion: first in, first out

• Elements are removed in the same order they arrive

• Any waiting line is a queue:– the check out line at a grocery store

– the cars at a stop light

– an assembly line

Page 4: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-4

Queues

• A queue is usually depicted horizontally

• One end of the queue is the rear (or tail), where elements are added

• The other end is the front (or head), from which elements are removed

• Unlike a stack, which operates on one end of the collection, a queue operates on both ends

Page 5: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-5

FIGURE 7.1 A conceptual view of a queue

Page 6: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-6

Queue Operations

• The term enqueue is used to refer to the process of adding an element to a queue

• Likewise, dequeue is the process of removing an element

• Like a stack, a pure queue does not allow the user to access the elements in the middle of the queue

• We include a toString method for convenience

Page 7: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-7

FIGURE 7.2 The operations on a queue

Page 8: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-8

FIGURE 7.3 The QueueADT interface in UML

Page 9: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-9

Listing 7.1

Page 10: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-10

Coded Messages

• Let's use a queue to help us encode and decode 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.

• However, this is fairly easy to break

• An improvement can be made by changing how much a letter is shifted depending on where the letter is in the message

Page 11: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-11

Coded Messages

• 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, we just start over at the beginning of the key

Page 12: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-12

FIGURE 7.4 An encoded message using a repeating key

Page 13: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-13

Coded Messages

• We'll use a queue to store the values of the key

• We'll dequeue a value when needed

• After using a key value, we then enqueue it back onto the end of the queue

• That way the queue represents the constantly cycling values in the key

Page 14: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-14

Listing 7.2

Page 15: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-15

Listing 7.2 (cont.)

Page 16: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-16

FIGURE 7.5 UML description of the Codes program

Page 17: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-17

Ticket Counter Simulation

• Now let's use a queue to simulate the waiting line at a movie theatre

• The goal is to determine how many cashiers are needed to keep the wait time below 7 minutes

• We'll assume:– customers arrive on average every 15 seconds

– processing a request takes two minutes once a customer reaches a cashier

Page 18: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-18

Listing 7.3

Page 19: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-19

Listing 7.3 (cont.)

Page 20: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-20

Listing 7.4

Page 21: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-21

Listing 7.4 (cont.)

Page 22: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-22

Listing 7.4 (cont.)

Page 23: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-23

FIGURE 7.6 UML description of the TicketCounter program

Page 24: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-24

FIGURE 7.7 The results of the ticket counter simulation

Page 25: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-25

Radix Sort• Let's look at one more use of queues

• A radix sort uses queues to order a set of values

• A queue is created for each possible value of a position (or digit) in the sort key

• For example, if the sort key is a lowercase alphabetic string, there would be 26 queues

• If the sort key was a decimal integer, there would be 10 queues corresponding to the digits 0 through 9

Page 26: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-26

Radix Sort

• 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

Page 27: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-27

FIGURE 7.8 A radix sort of ten three-digit numbers

Page 28: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-28

Listing 7.5

Page 29: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-29

Listing 7.5 (cont.)

Page 30: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-30

Listing 7.5 (cont.)

Page 31: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-31

FIGURE 7.9 UML description of the RadixSort program

Page 32: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-32

The LinkedQueue Class

• Like a stack, a queue can be implemented using an underlying array or a linked list

• A linked version can use the LinearNode class yet again

• In addition to keeping a reference to the beginning of the list, we will keep a second reference to the end

• An integer count will keep track of the number of elements in the queue

Page 33: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-33

FIGURE 7.10 A linked implementation of a queue

Page 34: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-34

LinkedQueue - the enqueue Operation

Page 35: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-35

FIGURE 7.11 The queue after adding element E

Page 36: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-36

LinkedQueue - the dequeue Operation

Page 37: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-37

FIGURE 7.12 The queue after a dequeue operation

Page 38: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-38

The ArrayQueue Class

• A queue can be managed using an array in which index 0 represents one end

• An integer value rear represents the next open slot in the array and the number of elements currently in the queue

• The challenge with this approach is that a queue operates on both ends, so the elements in the array must be shifted to keep one end at index 0

Page 39: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-39

FIGURE 7.13 An array implementation of a queue

Page 40: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-40

ArrayQueue - the enqueue Operation

Page 41: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-41

FIGURE 7.14 The queue after adding element E

Page 42: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-42

ArrayQueue - the dequeue Operation

Page 43: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-43

FIGURE 7.15 The queue after removing the first element

Page 44: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-44

The CircularArrayQueue Class

• If we do not fix one end of the queue at index 0, we will not have to shift the elements

• A circular queue is an implementation of a queue using an array that conceptually loops around on itself

• That is, the last index is thought to precede index 0

• We keep track of integers that indicate where the front and rear of the queue are at any given time

Page 45: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-45

FIGURE 7.16 A circular array implementation of a queue

Page 46: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-46

FIGURE 7.17 A queue straddling the end of a circular array

Page 47: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-47

FIGURE 7.18 Changes in a circular array implementation of a queue

Page 48: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-48

Circular Queues

• 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 = (rear+1) % queue.length;

• Note that this array implementation can also reach capacity and may need enlarging

Page 49: Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-49

Queue Implementations

• The enqueue operation is O(1) for all implementations

• The dequeue operation is O(1) for linked and circular array implementations, but O(n) for the noncircular array version due to the need to shift the elements in the queue