tk1924 program design & problem solving session 2011/2012 l6: queues
TRANSCRIPT
TK1924 Program Design & Problem Solving
Session 2011/2012
L6: Queues
Objectives
In this chapter, you will:
• Learn about queues
• Examine various queue operations
• Learn how to implement a queue as an array
• Discover queue applications
2
Queues
• Queue: list of homogeneous elements
• Elements are: – Added at one end (the back or rear)– Deleted from the other end (the front)
• First In First Out (FIFO) data structure– Middle elements are inaccessible
• Example:– Waiting line in a bank
3
Queue Operations
• Some of the queue operations are:– initializeQueue– isEmptyQueue– isFullQueue– front– back– addQueue– deleteQueue
• Abstract class queueADT defines these operations
4
Implementation of Queues as Arrays
• You need at least four (member) variables:– An array
• To store the queue elements
– queueFront• To keep track of first elements
– queueRear• To keep track of last elements
– maxQueueSize• To specify the maximum size of the queue
5
6
Implementation of Queues as Arrays (cont.)
queueFront
queueRear
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Add an element to the queue
• To add an element to the queue:– Advance queueRear to next array position – Add element to position pointed by queueRear
• Example: array size is 10; originally empty
7
queueFront
queueRear
0
2
A B C
addQueue(myQueue, ‘A’); addQueue(myQueue, ‘B’); addQueue(myQueue, ‘C’);
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
delete an element from the queue
• To delete an element from the queue:– Retrieve element pointed to by queueFront– Advance queueFront to next queue element
8
queueFront
queueRear
1
2
B C
deleteQueue();
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
Queue full ?
9
queueFront
queueRear
6
9
G H I J
addQueue(myQueue, ‘A’); addQueue(myQueue, ‘B’); addQueue(myQueue, ‘C’); deleteQueue(); deleteQueue();addQueue(myQueue, ‘D’); addQueue(myQueue, ‘E’); addQueue(myQueue, ‘F’);
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
deleteQueue(); deleteQueue(); addQueue(myQueue, ‘G’); addQueue(myQueue, ‘H’); addQueue(myQueue, ‘I’);deleteQueue(); deleteQueue(); addQueue(myQueue, ‘J’); addQueue(myQueue, ‘K’);
last array position - giving the impression that the queue is full
Solution 1
• When the queue overflows to the rear (i.e., queueRear points to the last array position):– Check value of queueFront– If value of queueFront indicates that there is
room in the front of the array, slide all of the queue elements toward the first array position
• Problem: too slow for large queues
10
Solution 2
• Assume that the array is circular (logically)
11
queueFront
queueRear
6
9
G H I J
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
[0]
[1]
[2][3] [4]
[5]
[6]
[7][8][9]
GH
IJ
Solution 2
• How to advance queueRear to the next array position (first location):
12
queueRear = (queueRear + 1) % maxQueueSize
Solution 2
13
queueFront
queueRear
6
1
K L G H I J
addQueue(myQueue, ‘K’);addQueue(myQueue, ‘L’);
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
• Consider the following case 1:
14
queueFront
queueRear
8
8
I
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
deleteQueue();
queueFront
queueRear
9
8
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
• Consider the following case 2:
15
queueFront
queueRear
9
7
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
addQueue(myQueue, ‘Z’);
queueFront
queueRear
9
8
Z
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
• Problem:– Both cases have identical values for queueFront and queueRear
– However, the former represents an empty queue, whereas the latter shows a full queue
• Solution?
16
• Solution 1: keep a count– Incremented when a new element is added to
the queue– Decremented when an element is removed– Initially, set to 0– Very useful if user (of queue) frequently needs
to know the number of elements in the queue
• We will implement this solution
17
• Solution 2: let queueFront indicate index of the array position preceding the first element– queueRear still indicates index of last one– Queue empty if:
• queueFront == queueRear
– Slot indicated by queueFront is reserved• Queue can hold 9 (not 10) elements
– Queue full if the next available space is the reserved slot indicated by queueFront
18
19
queueFront
queueRear
1
4
X Y Z
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
Reserved slot
Empty Queue and Full Queue
20
Initialize Queue
21
Front
returns the first element of the queue
22
Back
returns the last element of the queue
23
addQueue
24
deleteQueue
25
Constructors
26
Destructors
• The array to store the queue elements is created dynamically– When the queue object goes out of scope, the
destructor simply deallocates the memory occupied by the array
27
Application of Queues: Simulation
• Simulation: a technique in which one system models the behavior of another system
• Computer simulations using queues as the data structure are called queuing systems
28
Designing a Queuing System
• Server: the object that provides the service• Customer: the object receiving the service• Transaction time: service time, or the time
it takes to serve a customer• Model: system that consists of a list of
servers and a waiting queue holding the customers to be served– Customer at front of queue waits for the next
available server
29
Designing a Queuing System (cont'd.)
• We need to know:– Number of servers– Expected arrival time of a customer– Time between the arrivals of customers– Number of events affecting the system
• Performance of system depends on:– How many servers are available– How long it takes to serve a customer– How often a customer arrives
30
Designing a Queuing System (cont'd.)
• If it takes too long to serve a customer and customers arrive frequently, then more servers are needed
• System can be modeled as a time-driven simulation
• Time-driven simulation: the clock is a counter– The passage of, say, one minute can be
implemented by incrementing the counter by 1– Simulation is run for a fixed amount of time
31
Customer
32
Server
33
Server List
• A server list is a set of servers– At a given time, a server is either free or busy
34
Waiting Customers Queue
• When a customer arrives, he/she goes to the end of the queue
• When a server becomes available, the customer at front of queue leaves to conduct the transaction
• After each time unit, the waiting time of each customer in the queue is incremented by 1
• We can use queueType but must add the operation of incrementing the waiting time
35
Waiting Customers Queue (cont'd.)
36
Main Program
• Algorithm:– Declare and initialize the variables– Main loop (see next slide)– Print results
37
Main Program (cont'd.)
38
Summary
• Queue: items are added at one end and removed from the other end– First In First Out (FIFO) data structure
– Operations: add, remove, initialize, destroy, check if queue is empty/full
– Can be implemented as array or linked list
– Middle elements should not be accessed
– Restricted versions of arrays and linked lists
39