comp 245 data structures queues. introduction to the queue adt it is a fifo (first-in, first-out)...

Post on 18-Dec-2015

215 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Comp 245Data Structures

Queues

Introduction to the Queue ADT

It is a FIFO (first-in, first-out) structure Access to the Queue can take place at

two locations: 1) the Front for data removal and 2) the Rear for data storage

No access to elements other than the Front and Rear

Abstract View of a Queue

A RRRRRobust Queue

FullThe Full() function will be used in

conjunction with Enqueue

EmptyThe Empty() function will be used in

conjunction with Dequeue

Queue ApplicationDrive-In Teller Simulation

Computers are excellent for simulating real-life situations.

Queuing (Line) simulation software is available to observe a key behavior – WAIT TIME for a customer.

Running this simulation can help a business determine how many lines to have open at certain times during the day in order to achieve an acceptable wait time for customers. Also, this helps tellers (servicers) to be productive.

Queue ApplicationDrive-In Teller Simulation

The Drive-In Teller Simulation will model a single-server, single-queue situation.

We must obtain the following information before we run the simulationo Length of simulationo Arrival rate probability of a customero Expected service time

The output from the simulation will be average wait time for a customer.

Queue ApplicationDrive-In Teller Simulation

Queuing Equations

AR = Arrival Probability, ST = Service Time

1) (AR)(ST) < 1 (STABLE)

2) (AR)(ST) > 1 (UNSTABLE)

3) (AR)(ST) = 1 (FLUCUATING)

Queue ApplicationDrive-In Teller Simulation

How do we represent a clock?A counter variable

How do we determine if a customer has arrived in line?A random number generator will dictate an enqueue.

How do we represent a teller?A counter variable

How do we represent a customer?A timestamp

How do we determine how long a customer has waited?Subtract timestamp from current time on clock

How does a customer get out of line to be serviced?When the teller becomes available and the queue is not empty a

dequeue is executed. How do we determine average wait time for a customer?

Total wait time accumulation/Total customers served

Implementing a Queue ADTArray Based

There are three different types of array based Queues:o Packingo Circularo Free-Space

The three differ in…o The number of control fields needed to

maintain the queueo How they enqueue and dequeueo How they determine if the queue is empty or full

Implementing a Queue ADTArray Based - Packing

Needs an array(A) and a count(C) to implement.

Enq – data will be added to the queue at A[C]

Deq – data will be removed from the queue at A[0]; IMPORTANT: after a Deq, array must be packed!

Full – (C == MAX)

Empty- (C == 0)

Packing Queue Example

Implementing a Queue ADTArray Based - Circular

Needs an Array(A), Count(C), Front(F) and a Rear(R) to implement.

Enq – will add data to A[R], after the Enq, R must be incremented or wrapped. That is R++ or R = 0. Count must be incremented.

Deq – will remove data from A[F], after the Deq, F must be incremented or wrapped. That is F++ or F = 0. Count must be decremented.

Empty – (C = = 0)

Full – (C == MAX)

Circular Queue Example Part I

Circular Queue Example Part II

Implementing a Queue ADTArray Based – Free Space

Needs an Array(A), Front(F) and a Rear(R) to implement.

Initially set F and R to the last slot in the array.

Enq – You will increment or wrap R first and then add data to A[R]

Deq – You will increment or wrap F first and then remove data from A[F]

Empty – (F = = R)

Full – (F == R+1 or F == wrap(R)) “THIS PROTECTS EMPTY CONDITION”

Free Space Queue ExamplePart I

Free Space Queue ExamplePart II

Static Queue Review

Packing

Enq – A[C]

Deq – A[0]

Full – (C==Max)

Empty – (C==0)

Circular

Enq – A[R++]

Deq – A[F++]

Full – (C==Max)

Empty – (C==0)

Free Space

Enq – A[++R]

Deq – A[++F]

Full – (F==R+1)

Empty – (F==R)

Implementing a Queue ADTLinked List Based

Requires two pointers: 1) Front and 2) Rear

There is a “special” condition on Enq:

Enq when the Queue is empty.

There is a “special” condition on Deq:

Deq when the Queue has only one element.

Enq with a Linked List

Deq with a Linked List

Queue Implementation Review

Array Basedo Packing (counter)o Circular (counter, front, rear)o Free Space (front, rear)

Linked List Basedo Need only a front and rear pointero Must handle special cases for enq and

deq

C++ STL Queue Container Class

The STL queue is a type of container specifically designed to operate in a FIFO context (first-in first-out), where elements are inserted into one end of the container and extracted from the other.http://www.cplusplus.com/reference/stl/queue/

Here is an example of STL queue usage:queueSTL.cpp

top related