230 likes | 325 Views
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 locations: 1) the Front for data removal and 2) the Rear for data storage No access to elements other than the Front and Rear.
E N D
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
Front Rear enqueue 2 2 Front Rear enqueue 5 2 5 Front Rear 2 5 8 enqueue 8 Front Rear dequeue 5 8 2 Front Rear dequeue 8 5 Abstract View of a Queue
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
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 simulation • Length of simulation • Arrival rate probability of a customer • 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 • (AR)(ST) < 1 (STABLE) • (AR)(ST) > 1 (UNSTABLE) • (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: • Packing • Circular • Free-Space • The three differ in… • The number of control fields needed to maintain the queue • How they enqueue and dequeue • 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)
Data Initial Count 0 Data 2 Enq 2 Count 1 Data 2 5 Enq 5 Count 2 5 Data Deq Count 1 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)
Data Initial Front Rear Count 0 0 0 Data 2 Enq 2 Front Rear Count 1 0 1 Data 2 5 Enq 5 Front Rear Count 2 0 2 Data 5 Deq Front Rear Count 1 1 2 Circular Queue Example Part I
Data 5 8 Enq 8 Front Rear Count 2 1 0 Data 4 5 8 Enq 4 Front Rear Count 3 1 1 Data 4 8 Deq Front Rear Count 2 2 1 Data 4 Deq Front Rear Count 1 0 1 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”
Data Initial Front Rear 2 2 Data 2 Enq 2 Front Rear 2 0 Data 2 5 Enq 5 Front Rear 2 1 Data 5 Deq Front Rear 0 1 Free Space Queue ExamplePart I
Data 5 8 Enq 8 Front Rear 0 2 Data 5 8 Enq 4 Front Rear 0 2 Queue is FULL!!! Free Space Queue ExamplePart II
Static Queue Review Free Space Enq – A[++R] Deq – A[++F] Full – (F==R+1) Empty – (F==R) 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)
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.
Front NULL Rear Front Rear Temp Front Rear Temp Front Rear Enq with a Linked List
Front Rear Front X Rear Data Front Rear Deq with a Linked List
Queue Implementation Review • Array Based • Packing (counter) • Circular (counter, front, rear) • Free Space (front, rear) • Linked List Based • Need only a front and rear pointer • 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