chapter04r

72
Data Structures: A Pseudocode Approach with C 1 Chapter 4 Chapter 4 Objectives Upon completion you will be able to: Explain the design, use, and o peration of a queue Implement a queue using a linked list structure Understand the operation of the queue ADT Write application programs using the queue ADT Explain categorizing data and queue simulation Queues Queues

Transcript of chapter04r

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 1/72

Data Structures: A Pseudocode Approach with C 1

Chapter 4Chapter 4

Objectives

Upon completion you will be able to:

Ex

plain the design, use, and operation of a queue Implement a queue using a linked list structure

Understand the operation of the queue ADT

Write application programs using the queue ADT

Explain categorizing data and queue simulation

QueuesQueues

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 2/72

Data Structures: A Pseudocode Approach with C 2

What is a queue ?

A queue is a linear list in which data canbe inserted at one end, called the rear,

and deleted from the other end, called thefront.

It is a first in first out (FIFO) restricteddata structure.

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 3/72

Data Structures: A Pseudocode Approach with C 3

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 4/72

Data Structures: A Pseudocode Approach with C 4

4-1 Queue Operations

This section discusses t he four basic queue operations. Using This section discusses t he four basic queue operations. Using 

diagrammatic figures, it shows how each of t hem work. It diagrammatic figures, it shows how each of t hem work. It 

concludes wit h a comprehensive example t hat demonstrates each concludes wit h a comprehensive example t hat demonstrates each 

operation.operation.

Enqueue

Dequeue

Queue Front Queue Rear

Queue Example

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 5/72

Data Structures: A Pseudocode Approach with C 5

Queue operations

nqueue

Dequeue

Queue front  Queue rear

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 6/72

Data Structures: A Pseudocode Approach with C 6

nqueue

nqueue inserts an element at the rear of thequeue.

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 7/72

Data Structures: A Pseudocode Approach with C 7

Dequeue

Dequeue deletes an element at the front of thequeue.

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 8/72

Data Structures: A Pseudocode Approach with C 8

Queue front 

Queue front retrieves the element at the front of the queue.

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 9/72

Data Structures: A Pseudocode Approach with C 9

Queue rear

Queue rear retrieves the element at the rear of the queue.

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 10/72

Data Structures: A Pseudocode Approach with C 10

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 11/72

Data Structures: A Pseudocode Approach with C 11

(Continued)

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 12/72

Data Structures: A Pseudocode Approach with C 12

4-2 Queue Linked List Design

W e first discuss t he data structure for a linked-list implementation.

W e t hen develop t he eig ht algorit hms required to implement a queue.

Data Structure Queue

Algorithms

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 13/72

Data Structures: A Pseudocode Approach with C 13

Data structure

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 14/72

Data Structures: A Pseudocode Approach with C 14

pointer

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 15/72

Data Structures: A Pseudocode Approach with C 15

Queue algorithms

Create queue

Enqueue

Dequeue

Queuefront 

Queuerear

Empty queue

Full queue

Queue count 

Destroy queue

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 16/72

Data Structures: A Pseudocode Approach with C 16

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 17/72

Data Structures: A Pseudocode Approach with C 17

(Continued)

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 18/72

Data Structures: A Pseudocode Approach with C 18

Create queue

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 19/72

Data Structures: A Pseudocode Approach with C 19

enqueue

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 20/72

Data Structures: A Pseudocode Approach with C 20

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 21/72

Data Structures: A Pseudocode Approach with C 21

dequeue

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 22/72

Data Structures: A Pseudocode Approach with C 22

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 23/72

Data Structures: A Pseudocode Approach with C 23

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 24/72

Data Structures: A Pseudocode Approach with C 24

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 25/72

Data Structures: A Pseudocode Approach with C 25

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 26/72

Data Structures: A Pseudocode Approach with C 26

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 27/72

Data Structures: A Pseudocode Approach with C 27

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 28/72

Data Structures: A Pseudocode Approach with C 28

4-3 Queue ADT

This section develops t he data structures and C code toThis section develops t he data structures and C code to

implement a Queue ADT  . The first program containsimplement a Queue ADT  . The first program contains

t he data structure declarations and a list of t he prototypest he data structure declarations and a list of t he prototypes

 for all of t he functions. W e t hen develop t he C code for  for all of t he functions. W e t hen develop t he C code for t he algorit hms discussed in Section 4.2t he algorit hms discussed in Section 4.2

Queue Structure

Queue ADT Algorithms

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 29/72

Data Structures: A Pseudocode Approach with C 29

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 30/72

Data Structures: A Pseudocode Approach with C 30

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 31/72

Data Structures: A Pseudocode Approach with C 31

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 32/72

Data Structures: A Pseudocode Approach with C 32

(

)

dequeue (queue, (void*)&dataPtr)

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 33/72

Data Structures: A Pseudocode Approach with C 33

queueFront (queue, (void*)&dataPtr)

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 34/72

Data Structures: A Pseudocode Approach with C 34

queueRear (queue, (void*)&dataPtr)

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 35/72

Data Structures: A Pseudocode Approach with C 35

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 36/72

Data Structures: A Pseudocode Approach with C 36

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 37/72

Data Structures: A Pseudocode Approach with C 37

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 38/72

Data Structures: A Pseudocode Approach with C 38

4-4 Queuing Theory

Queuing t heory is a field of applied mat hematics t hat is

used to predict t he performance of queues. In t his

section we review a few basic queuing t heory concepts.

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 39/72

Data Structures: A Pseudocode Approach with C 39

Queuing theory

Queuing theory is a field of appliedmathematics that is used to predict theperformance of queues.

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 40/72

Data Structures: A Pseudocode Approach with C 40

Queue types

Single server queue

Provide service to only one customer at atime.

Multiserver queue

Provide service to many customers at a time.

Multiqueues, multiple single-server queues

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 41/72

Data Structures: A Pseudocode Approach with C 41

Common queue elements

One or more customers

A customer is any person or thing needingservice.

Service

The service is any activity needed toaccomplish the required result.

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 42/72

Data Structures: A Pseudocode Approach with C 42

 Affected factors

Arrival rate

The rate at which customers arrive in thequeue for service.

Service time

The average time required to complete theprocessing of a customer request.

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 43/72

Data Structures: A Pseudocode Approach with C 43

Response time

The average time from the point at whichcustomers enter the queue until the moment they leave the server.

queue time + average service time

Queue time

The average length of time customers wait in queue Average service time

The average time required to complete the processing of acustomer request.

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 44/72

Data Structures: A Pseudocode Approach with C 44

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 45/72

Data Structures: A Pseudocode Approach with C 45

4-5 Queue Applications

W e develop two queue applications. The first shows how to use a

queue to categorize data. The second is a queue simulator,

which is an excellent tool to simulate t he performance and to

increase our understanding of its operation.

Categorizing Data

Queue Simulation

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 46/72

Data Structures: A Pseudocode Approach with C 46

Categorizing data

It is often necessary to rearrange datawithout destroying their basic sequence.

Example

3 22 12 6 10 34 65 29 9 30 81 4 5 19 20 57 44 99

We want the list rearranged as shown below.

Less than 10 : 3 6 9 4 5

Between 10 and 19 : 12 10 19 Between 20 and 29 : 22 29 20

30 and greater : 34 65 30 81 57 44 99

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 47/72

Data Structures: A Pseudocode Approach with C 47

Categorizing data design

Category queues

Fill category queues

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 48/72

Data Structures: A Pseudocode Approach with C 48

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 49/72

Data Structures: A Pseudocode Approach with C 49

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 50/72

Data Structures: A Pseudocode Approach with C 50

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 51/72

Data Structures: A Pseudocode Approach with C 51

(Continued)

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 52/72

Data Structures: A Pseudocode Approach with C 52

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 53/72

Data Structures: A Pseudocode Approach with C 53

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 54/72

Data Structures: A Pseudocode Approach with C 54

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 55/72

Data Structures: A Pseudocode Approach with C 55

(Continued)

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 56/72

Data Structures: A Pseudocode Approach with C 56

(Continued)

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 57/72

Data Structures: A Pseudocode Approach with C 57

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 58/72

Data Structures: A Pseudocode Approach with C 58

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 59/72

Data Structures: A Pseudocode Approach with C 59

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 60/72

Data Structures: A Pseudocode Approach with C 60

Queue simulation

We build a model of a single-server queue.

the store is open 8 hours per day, 7 days a week.

To simulate a day, we build a model that runsfor 480 minutes(8×60).

window

clerk 

New customer

queue

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 61/72

Data Structures: A Pseudocode Approach with C 61

Queue simulation

The simulation use a digital clock that events start and stop in 1-minute intervals.

In each minute of operation, three eventswill be checked:

The arrival of customers

The start of customer processing

The completion of customer processing

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 62/72

Data Structures: A Pseudocode Approach with C 62

Events

newCustomer

svcFree (svcStart)

svcComplete

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 63/72

Data Structures: A Pseudocode Approach with C 63

Data structures

Queue head Queue node

Customer status

Simulation statistics

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 64/72

Data Structures: A Pseudocode Approach with C 64

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 65/72

Data Structures: A Pseudocode Approach with C 65

Flag moreCusts

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 66/72

Data Structures: A Pseudocode Approach with C 66

Queue node

custNum

arriveTime

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 67/72

Data Structures: A Pseudocode Approach with C 67

custStatus

custNum

arriveTime

startTime

svcTime

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 68/72

Data Structures: A Pseudocode Approach with C 68

simStatus

numCust

totSvcTime

totWaitTime

maxQueueSize

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 69/72

Data Structures: A Pseudocode Approach with C 69

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 70/72

Data Structures: A Pseudocode Approach with C 70

 An example

Four customers arrive at time 1, 2, 3 and5.

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 71/72

Data Structures: A Pseudocode Approach with C 71

 An example

clocks

events1 2 3 4 5 6 7 8

newCustomer 1 2 3 4

svcFree

(svcStart)

 YES

1

 YES

2

 YES

3

 YES

4

svcComplete YES

1

 YES

2

 YES

3

 YES

4

8/9/2019 chapter04r

http://slidepdf.com/reader/full/chapter04r 72/72