tk1924 program design & problem solving session 2011/2012 l6: queues

39
TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues

Upload: brice-goodman

Post on 28-Dec-2015

218 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues

TK1924 Program Design & Problem Solving

Session 2011/2012

L6: Queues

Page 2: 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

Page 3: TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues

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

Page 4: TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues

Queue Operations

• Some of the queue operations are:– initializeQueue– isEmptyQueue– isFullQueue– front– back– addQueue– deleteQueue

• Abstract class queueADT defines these operations

4

Page 5: TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues

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

Page 6: TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues

6

Implementation of Queues as Arrays (cont.)

queueFront

queueRear

[0] [1] [2] [3] [4] [5] [6] [7] [8]

Page 7: TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues

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]

Page 8: TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues

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]

Page 9: TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues

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

Page 10: TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues

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

Page 11: TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues

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

Page 12: TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues

Solution 2

• How to advance queueRear to the next array position (first location):

12

queueRear = (queueRear + 1) % maxQueueSize

Page 13: TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues

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]

Page 14: TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues

• 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]

Page 15: TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues

• 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]

Page 16: TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues

• 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

Page 17: TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues

• 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

Page 18: TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues

• 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

Page 19: TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues

19

queueFront

queueRear

1

4

X Y Z

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

Reserved slot

Page 20: TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues

Empty Queue and Full Queue

20

Page 21: TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues

Initialize Queue

21

Page 22: TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues

Front

returns the first element of the queue

22

Page 23: TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues

Back

returns the last element of the queue

23

Page 24: TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues

addQueue

24

Page 25: TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues

deleteQueue

25

Page 26: TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues

Constructors

26

Page 27: TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues

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

Page 28: TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues

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

Page 29: TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues

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

Page 30: TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues

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

Page 31: TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues

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

Page 32: TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues

Customer

32

Page 33: TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues

Server

33

Page 34: TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues

Server List

• A server list is a set of servers– At a given time, a server is either free or busy

34

Page 35: TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues

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

Page 36: TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues

Waiting Customers Queue (cont'd.)

36

Page 37: TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues

Main Program

• Algorithm:– Declare and initialize the variables– Main loop (see next slide)– Print results

37

Page 38: TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues

Main Program (cont'd.)

38

Page 39: TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues

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