1.08k likes | 1.25k Views
Queues. Briana B. Morrison Adapted from Alan Eugenio. Topics. Define Queue APIs Applications Radix Sort Simulation Implementation Array based Circular Empty, one value, full Linked list based Deques Priority Queues. The Queue.
E N D
Queues Briana B. Morrison Adapted from Alan Eugenio
Topics • Define Queue • APIs • Applications • Radix Sort • Simulation • Implementation • Array based • Circular • Empty, one value, full • Linked list based • Deques • Priority Queues Queues
The Queue A Queue is a FIFO (First in First Out) Data Structure. Elements are inserted in the Rear of the queue and are removed at the Front. Queues
queue(); Create an empty queue. CLASS queue CLASS queue <queue> <queue> Constructor Operations bool empty() const; Check whether the queue is empty. Return true if it is empty and false otherwise. T& front(); Return a reference to the value of the item at the font of the queue. Precondition: The queue is not empty. Queues
const T& front() const; Constant version of front(). CLASS queue <queue> Operations void pop(); Remove the item from the front of the queue. Precondition: The queue is not empty. Postcondition: The element at the front of the queue is the element that was added immediately after the element just popped or the queue is empty. Queues
CLASS queue <queue> Operations void push(const T& item); Insert the argument item at the back of the queue. Postcondition: The queue has a new item at the back int size() const; Return the number of elements in the queue. Queues
DETERMINE THE OUTPUT FROM THE FOLLOWING: queue<int> my_queue; for (int i = 0; i < 10; i++) my_queue.push (i * i); while (!my_queue.empty()) { cout << my_queue.front() << endl; my_queue.pop(); } // while Queues
deque? list? vector? OK OK NOT OK: NO pop_front METHOD Queues
Implementing Queue: adapter of std::list • This is a simple adapter class, with following mappings: • Queue push maps to push_back • Queue front maps front • Queue pop maps to pop_front • ... • This is the approach taken by the C++ standard library. • Any sequential container that supports push_back, front, and pop_front can be used. • The list • The deque Queues
Applications of Queues • Direct applications • Waiting lists, bureaucracy • Access to shared resources (e.g., printer) • Multiprogramming • Indirect applications • Auxiliary data structure for algorithms • Component of other data structures Queues
The Radix Sort Order ten 2 digit numbers in 10 bins from smallest number to largest number. Requires 2 calls to the sort Algorithm. Initial Sequence: 91 6 85 15 92 35 30 22 39 Pass 0: Distribute the cards into bins according to the 1's digit (100). Queues
The Radix Sort After Collection: 30 91 92 22 85 15 35 6 39 Pass 1: Take the new sequence and distribute the cards into bins determined by the 10's digit (101). Final Sequence: 6 15 22 30 35 39 85 91 92 Queues
Radix Sort • Use an array of queues (or vector of queues) as the “buckets” void radixSort (vector<int>& v, int d) { int i; int power = 1; queue<int> digitQueue[10]; for (i=0;i < d;i++) { distribute(v, digitQueue, power); collect(digitQueue, v); power *= 10; } } Queues
// support function for radixSort() // distribute vector elements into one of 10 queues // using the digit corresponding to power // power = 1 ==> 1's digit // power = 10 ==> 10's digit // power = 100 ==> 100's digit // ... void distribute(const vector<int>& v, queue<int> digitQueue[], int power) { int i; // loop through the vector, inserting each element into // the queue (v[i] / power) % 10 for (i = 0; i < v.size(); i++) digitQueue[(v[i] / power) % 10].push(v[i]); } Queues
// support function for radixSort() // gather elements from the queues and copy back to the vector void collect(queue<int> digitQueue[], vector<int>& v) { int i = 0, digit; // scan the vector of queues using indices 0, 1, 2, etc. for (digit = 0; digit < 10; digit++) // collect items until queue empty and copy items back // to the vector while (!digitQueue[digit].empty()) { v[i] = digitQueue[digit].front(); digitQueue[digit].pop(); i++; } } Queues
A SYSTEM IS A COLLECTION OF INTERACTING PARTS. A MODEL IS A SIMPLIFICATION OF A SYSTEM. THE PURPOSE OF BUILDING A MODEL IS TO STUDY THE UNDERLYING SYSTEM. Queues
Simulating Waiting Lines Using Queues • Simulation is used to study the performance: • Of a physical (“real”) system • By using a physical, mathematical, or computer model of the system • Simulation allows designers to estimate performance • Before building a system • Simulation can lead to design improvements • Giving better expected performance of the system Queues
Simulating Waiting Lines Using Queues • Simulation is particular useful when: • Building/changing the system is expensive • Changing the system later may be dangerous • Often use computer models to simulate “real” systems • Airline check-in counter, for example • Special branch of mathematics for these problems: Queuing Theory Queues
Simulate Airline Check-In • We will maintain a simulated clock • Counts in integer “ticks”, from 0 • At each tick, one or more events can happen: • Frequent flyer (FF) passenger arrives in line • Regular (R) passenger arrives in line • Agent finishes, then serves next FF passenger • Agent finishes, then serves next R passenger • Agent is idle (both lines empty) Queues
Simulate Airline Check-In • Simulation uses some parameters: • Max # FF served between regular passengers • Arrival rate of FF passengers • Arrival rate of R passengers • Service time • Desired output: • Statistics on waiting times, agent idle time, etc. • Optionally, a detailed trace Queues
Simulate Airline Check-In • Design approach: • Agent data type models airline agent • Passenger data type models passengers • 2 queue<Passenger>, 1 for FF, 1 for R • Overall Airline_Checkin_Sim class Queues
Simulate Airline Check-In Queues