queues - topstep.weebly.comtopstep.weebly.com/uploads/2/4/9/4/24947581/chapter04.pdf · 4-1 queue...
TRANSCRIPT
1
Chapter 4
Objectives
Upon completion you will be able to:
• Explain 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
Queues
Data Structures: A Pseudocode Approach with C , Second Edition
2
What is a queue ?
A queue is a linear list in which data can be inserted at one end, called the rear, and deleted from the other end, called the front.
It is a first in – first out (FIFO) restricted data structure.
Data Structures: A Pseudocode Approach with C , Second Edition
3Data Structures: A Pseudocode Approach with C , Second Edition
4
4-1 Queue Operations
This section discusses the four basic queue operations. Using
diagrammatic figures, it shows how each of them work. It
concludes with a comprehensive example that demonstrates
each operation.
• Enqueue
• Dequeue
• Queue Front
• Queue Rear
• Queue Example
Data Structures: A Pseudocode Approach with C , Second Edition
5
Queue operations
Enqueue
Dequeue
Queue front
Queue rear
Data Structures: A Pseudocode Approach with C , Second Edition
6
Enqueue
Enqueue inserts an element at the rear of the queue.
Data Structures: A Pseudocode Approach with C , Second Edition
7
Dequeue
Dequeue deletes an element at the front of the queue.
Data Structures: A Pseudocode Approach with C , Second Edition
8
Queue front Queue front retrieves the element at the front of
the queue.
Data Structures: A Pseudocode Approach with C , Second Edition
9
Queue rear Queue rear retrieves the element at the rear of
the queue.
Data Structures: A Pseudocode Approach with C , Second Edition
10Data Structures: A Pseudocode Approach with C , Second Edition
11
(Continued)
Data Structures: A Pseudocode Approach with C , Second Edition
12
4-2 Queue Linked List Design
We first discuss the data structure for a linked-list implementation.
We then develop the eight algorithms required to implement a queue.
• Data Structure Queue
• Algorithms
Data Structures: A Pseudocode Approach with C , Second Edition
13
Data structure
Data Structures: A Pseudocode Approach with C , Second Edition
14
pointer
Data Structures: A Pseudocode Approach with C , Second Edition
15
Queue algorithms
Create queue
Enqueue
Dequeue
Queuefront
Queuerear
Empty queue
Full queue
Queue count
Destroy queue
Data Structures: A Pseudocode Approach with C , Second Edition
16Data Structures: A Pseudocode Approach with C , Second Edition
17
(Continued)
Data Structures: A Pseudocode Approach with C , Second Edition
18
Create queue
Data Structures: A Pseudocode Approach with C , Second Edition
19
enqueue
Data Structures: A Pseudocode Approach with C , Second Edition
20Data Structures: A Pseudocode Approach with C , Second Edition
21
dequeue
Data Structures: A Pseudocode Approach with C , Second Edition
22Data Structures: A Pseudocode Approach with C , Second Edition
23Data Structures: A Pseudocode Approach with C , Second Edition
24Data Structures: A Pseudocode Approach with C , Second Edition
25Data Structures: A Pseudocode Approach with C , Second Edition
26Data Structures: A Pseudocode Approach with C , Second Edition
Data Structures: A Pseudocode Approach with C 27
28
4-3 Queue ADT
This section develops the data structures and C code to
implement a Queue ADT. The first program contains
the data structure declarations and a list of the prototypes
for all of the functions. We then develop the C code for
the algorithms discussed in Section 4.2
• Queue Structure
• Queue ADT Algorithms
Data Structures: A Pseudocode Approach with C , Second Edition
29Data Structures: A Pseudocode Approach with C , Second Edition
30Data Structures: A Pseudocode Approach with C , Second Edition
31
傳進來的是儲存資料變數的位址,用區域指標變數來接。
Data Structures: A Pseudocode Approach with C , Second Edition
32
傳進來的是儲存指到資料變數的指標變數的位址,用區域指標變數(一個指到指標變數的指標變數)來接。
dequeue (queue, (void*)&dataPtr)
Data Structures: A Pseudocode Approach with C , Second Edition
33
queueFront (queue, (void*)&dataPtr)
Data Structures: A Pseudocode Approach with C , Second Edition
34
queueRear (queue, (void*)&dataPtr)
Data Structures: A Pseudocode Approach with C , Second Edition
Data Structures: A Pseudocode Approach with C 35
Data Structures: A Pseudocode Approach with C 36
Data Structures: A Pseudocode Approach with C 37
Data Structures: A Pseudocode Approach with C 38
4-4 Queuing Theory
Queuing theory is a field of applied mathematics that is
used to predict the performance of queues. In this
section we review a few basic queuing theory concepts.
39
Queuing theory
Queuing theory is a field of applied mathematics that is used to predict the performance of queues.
Data Structures: A Pseudocode Approach with C , Second Edition
40
Queue types
Single server queue
Provide service to only one customer at a time.
Multiserver queue
Provide service to many customers at a time.
Multiqueues, multiple single-server queues
Data Structures: A Pseudocode Approach with C , Second Edition
41
Common queue elements
One or more customers
A customer is any person or thing needing service.
Service
The service is any activity needed to accomplish the required result.
Data Structures: A Pseudocode Approach with C , Second Edition
42
Affected factors
Arrival rate
The rate at which customers arrive in the queue for service.
Service time
The average time required to complete the processing of a customer request.
Data Structures: A Pseudocode Approach with C , Second Edition
43
Response time
The average time from the point at which customers 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 a
customer request.
Data Structures: A Pseudocode Approach with C , Second Edition
44Data Structures: A Pseudocode Approach with C , Second Edition
45
4-5 Queue Applications
We 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 the performance and to
increase our understanding of its operation.
• Categorizing Data
• Queue Simulation
Data Structures: A Pseudocode Approach with C , Second Edition
46
Categorizing data
It is often necessary to rearrange data without 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
Data Structures: A Pseudocode Approach with C , Second Edition
47
Categorizing data design
Category queues
Fill category queues
Data Structures: A Pseudocode Approach with C , Second Edition
48Data Structures: A Pseudocode Approach with C , Second Edition
49Data Structures: A Pseudocode Approach with C , Second Edition
50Data Structures: A Pseudocode Approach with C , Second Edition
51
(Continued)
Data Structures: A Pseudocode Approach with C , Second Edition
52Data Structures: A Pseudocode Approach with C , Second Edition
53Data Structures: A Pseudocode Approach with C , Second Edition
54Data Structures: A Pseudocode Approach with C , Second Edition
55
(Continued)
Data Structures: A Pseudocode Approach with C , Second Edition
56
(Continued)
Data Structures: A Pseudocode Approach with C , Second Edition
57Data Structures: A Pseudocode Approach with C , Second Edition
58Data Structures: A Pseudocode Approach with C , Second Edition
59Data Structures: A Pseudocode Approach with C , Second Edition
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 runs for 480 minutes(8×60).
window
clerkNew customer
queue
Data Structures: A Pseudocode Approach with C , Second Edition
61
Queue simulation
The simulation use a digital clock that events start and stop in 1-minute intervals.
In each minute of operation, three events will be checked:
The arrival of customers
The start of customer processing
The completion of customer processing
Data Structures: A Pseudocode Approach with C , Second Edition
62
Events
newCustomer
svcFree (svcStart)
svcComplete
Data Structures: A Pseudocode Approach with C , Second Edition
63
Data structures
Queue head
Queue node
Customer status
Simulation statistics
Data Structures: A Pseudocode Approach with C , Second Edition
64Data Structures: A Pseudocode Approach with C , Second Edition
65
Flag moreCusts
Data Structures: A Pseudocode Approach with C , Second Edition
66
Queue node
custNum
arriveTime
Data Structures: A Pseudocode Approach with C , Second Edition
67
custStatus
custNum
arriveTime
startTime
svcTime
Data Structures: A Pseudocode Approach with C , Second Edition
68
simStatus
numCust
totSvcTime
totWaitTime
maxQueueSize
Data Structures: A Pseudocode Approach with C , Second Edition
69Data Structures: A Pseudocode Approach with C , Second Edition
70
An example
Four customers arrive at time 1, 2, 3 and 5.
Data Structures: A Pseudocode Approach with C , Second Edition
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
svcCompleteYES
1
YES
2
YES
3
YES
4
Data Structures: A Pseudocode Approach with C , Second Edition
72Data Structures: A Pseudocode Approach with C , Second Edition