Download presentation
Presentation is loading. Please wait.
Published byBrice Goodman Modified over 9 years ago
1
TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues
2
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
3
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
4
Queue Operations Some of the queue operations are: – initializeQueue – isEmptyQueue – isFullQueue – front – back – addQueue – deleteQueue Abstract class queueADT defines these operations 4
5
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
6 Implementation of Queues as Arrays (cont.) queueFront queueRear [0][1][2][3][4][5][6][7][8]
7
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 ABC addQueue(myQueue, ‘A’); addQueue(myQueue, ‘B’); addQueue(myQueue, ‘C’); [0][1][2][3][4][5][6][7][8][9]
8
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 BC deleteQueue(); [0][1][2][3][4][5][6][7][8][9]
9
Queue full ? 9 queueFront queueRear 6 9 GHIJ 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(); addQueue(myQueue, ‘J’); addQueue(myQueue, ‘K’); last array position - giving the impression that the queue is full
10
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
11
Solution 2 Assume that the array is circular (logically) 11 queueFront queueRear 6 9 GHIJ [0][1][2][3][4][5][6][7][8][9] [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] G H I J
12
Solution 2 How to advance queueRear to the next array position (first location): 12 queueRear = (queueRear + 1) % maxQueueSize
13
Solution 2 13 queueFront queueRear 6 1 KLGHIJ addQueue(myQueue, ‘K’); addQueue(myQueue, ‘L’); [0][1][2][3][4][5][6][7][8][9]
14
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]
15
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]
16
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
17
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
18
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
19 queueFront queueRear 1 4 XYZ [0][1][2][3][4][5][6][7][8][9] Reserved slot
20
Empty Queue and Full Queue 20
21
Initialize Queue 21
22
Front returns the first element of the queue 22
23
Back returns the last element of the queue 23
24
addQueue 24
25
deleteQueue 25
26
Constructors 26
27
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
28
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
29
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
30
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
31
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
32
Customer 32
33
Server 33
34
Server List A server list is a set of servers –At a given time, a server is either free or busy 34
35
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
36
Waiting Customers Queue (cont'd.) 36
37
Main Program Algorithm: –Declare and initialize the variables –Main loop (see next slide) –Print results 37
38
Main Program (cont'd.) 38
39
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.