Download presentation
Presentation is loading. Please wait.
Published byMegan Cummings Modified over 9 years ago
1
Comp 245 Data Structures Queues
2
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
3
Abstract View of a Queue
4
A RRRRRobust Queue Full The Full() function will be used in conjunction with Enqueue Empty The Empty() function will be used in conjunction with Dequeue
5
Queue Application Drive-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.
6
Queue Application Drive-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 simulation o Length of simulation o Arrival rate probability of a customer o Expected service time The output from the simulation will be average wait time for a customer.
7
Queue Application Drive-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)
8
Queue Application Drive-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
9
Implementing a Queue ADT Array Based There are three different types of array based Queues: o Packing o Circular o Free-Space The three differ in… o The number of control fields needed to maintain the queue o How they enqueue and dequeue o How they determine if the queue is empty or full
10
Implementing a Queue ADT Array 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)
11
Packing Queue Example
12
Implementing a Queue ADT Array 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)
13
Circular Queue Example Part I
14
Circular Queue Example Part II
15
Implementing a Queue ADT Array 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”
16
Free Space Queue Example Part I
17
Free Space Queue Example Part II
18
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)
19
Implementing a Queue ADT Linked 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.
20
Enq with a Linked List
21
Deq with a Linked List
22
Queue Implementation Review Array Based o Packing (counter) o Circular (counter, front, rear) o Free Space (front, rear) Linked List Based o Need only a front and rear pointer o Must handle special cases for enq and deq
23
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.