comp 245 data structures queues. introduction to the queue adt it is a fifo (first-in, first-out)...
TRANSCRIPT
![Page 1: Comp 245 Data 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](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d205503460f949f56c9/html5/thumbnails/1.jpg)
Comp 245Data Structures
Queues
![Page 2: Comp 245 Data 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](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d205503460f949f56c9/html5/thumbnails/2.jpg)
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
![Page 3: Comp 245 Data 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](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d205503460f949f56c9/html5/thumbnails/3.jpg)
Abstract View of a Queue
![Page 4: Comp 245 Data 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](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d205503460f949f56c9/html5/thumbnails/4.jpg)
A RRRRRobust Queue
FullThe Full() function will be used in
conjunction with Enqueue
EmptyThe Empty() function will be used in
conjunction with Dequeue
![Page 5: Comp 245 Data 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](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d205503460f949f56c9/html5/thumbnails/5.jpg)
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.
![Page 6: Comp 245 Data 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](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d205503460f949f56c9/html5/thumbnails/6.jpg)
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.
![Page 7: Comp 245 Data 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](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d205503460f949f56c9/html5/thumbnails/7.jpg)
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)
![Page 8: Comp 245 Data 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](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d205503460f949f56c9/html5/thumbnails/8.jpg)
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
![Page 9: Comp 245 Data 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](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d205503460f949f56c9/html5/thumbnails/9.jpg)
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
![Page 10: Comp 245 Data 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](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d205503460f949f56c9/html5/thumbnails/10.jpg)
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)
![Page 11: Comp 245 Data 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](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d205503460f949f56c9/html5/thumbnails/11.jpg)
Packing Queue Example
![Page 12: Comp 245 Data 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](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d205503460f949f56c9/html5/thumbnails/12.jpg)
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)
![Page 13: Comp 245 Data 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](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d205503460f949f56c9/html5/thumbnails/13.jpg)
Circular Queue Example Part I
![Page 14: Comp 245 Data 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](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d205503460f949f56c9/html5/thumbnails/14.jpg)
Circular Queue Example Part II
![Page 15: Comp 245 Data 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](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d205503460f949f56c9/html5/thumbnails/15.jpg)
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”
![Page 16: Comp 245 Data 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](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d205503460f949f56c9/html5/thumbnails/16.jpg)
Free Space Queue ExamplePart I
![Page 17: Comp 245 Data 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](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d205503460f949f56c9/html5/thumbnails/17.jpg)
Free Space Queue ExamplePart II
![Page 18: Comp 245 Data 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](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d205503460f949f56c9/html5/thumbnails/18.jpg)
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)
![Page 19: Comp 245 Data 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](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d205503460f949f56c9/html5/thumbnails/19.jpg)
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.
![Page 20: Comp 245 Data 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](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d205503460f949f56c9/html5/thumbnails/20.jpg)
Enq with a Linked List
![Page 21: Comp 245 Data 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](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d205503460f949f56c9/html5/thumbnails/21.jpg)
Deq with a Linked List
![Page 22: Comp 245 Data 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](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d205503460f949f56c9/html5/thumbnails/22.jpg)
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
![Page 23: Comp 245 Data 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](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d205503460f949f56c9/html5/thumbnails/23.jpg)
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