Tải bản đầy đủ (.ppt) (49 trang)

Ch07 Queue.ppt

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.68 MB, 49 trang )

Chapter 7
Queues
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
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
another

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
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
Copyright © 2005 Pearson
Addison-Wesley. All rights
reserved. 7-5
FIGURE 7.1
A conceptual view of a queue

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
Copyright © 2005 Pearson
Addison-Wesley. All rights
reserved. 7-7
FIGURE 7.2
The operations on a queue
Copyright © 2005 Pearson
Addison-Wesley. All rights
reserved. 7-8
FIGURE 7.3
The QueueADT interface in UML
Copyright © 2005 Pearson
Addison-Wesley. All rights
reserved. 7-9

Listing 7.1
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
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
Copyright © 2005 Pearson
Addison-Wesley. All rights
reserved. 7-12
FIGURE 7.4 An encoded message
using a repeating key
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

See Codes.java (page 183)
Copyright © 2005 Pearson

Addison-Wesley. All rights
reserved. 7-14
Listing 7.2
Copyright © 2005 Pearson
Addison-Wesley. All rights
reserved. 7-15
Listing 7.2 (cont.)
Copyright © 2005 Pearson
Addison-Wesley. All rights
reserved. 7-16
FIGURE 7.5 UML description of the
Codes program
Copyright © 2005 Pearson
Addison-Wesley. All rights
reserved. 7-17
Listing 7.3
Copyright © 2005 Pearson
Addison-Wesley. All rights
reserved. 7-18
Listing 7.3 (cont.)
Copyright © 2005 Pearson
Addison-Wesley. All rights
reserved. 7-19
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

See Customer.java (page 186)

See TicketCounter.java (page 188)
Copyright © 2005 Pearson
Addison-Wesley. All rights
reserved. 7-20
Listing 7.4
Copyright © 2005 Pearson
Addison-Wesley. All rights
reserved. 7-21
Listing 7.4 (cont.)
Copyright © 2005 Pearson
Addison-Wesley. All rights
reserved. 7-22
Listing 7.4 (cont.)
Copyright © 2005 Pearson
Addison-Wesley. All rights
reserved. 7-23
FIGURE 7.6 UML description of the
TicketCounter program
Copyright © 2005 Pearson

Addison-Wesley. All rights
reserved. 7-24
FIGURE 7.7 The results of the ticket
counter simulation
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 in
the sort key

For example, if the sort key was 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

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×