1 / 106

Queues

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.

Télécharger la présentation

Queues

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Queues Briana B. Morrison Adapted from Alan Eugenio

  2. Topics • Define Queue • APIs • Applications • Radix Sort • Simulation • Implementation • Array based • Circular • Empty, one value, full • Linked list based • Deques • Priority Queues Queues

  3. Queues

  4. 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

  5. Queues

  6. Queues

  7. 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

  8. 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

  9. 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

  10. Queues

  11. 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

  12. Queues

  13. Queues

  14. deque? list? vector? OK OK NOT OK: NO pop_front METHOD Queues

  15. 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

  16. Queues

  17. Queues

  18. Queues

  19. Queues

  20. 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

  21. Queues

  22. 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

  23. 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

  24. 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

  25. // 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

  26. // 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

  27. Queues

  28. 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

  29. Queues

  30. Queues

  31. Queues

  32. Queues

  33. Queues

  34. Queues

  35. 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

  36. 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

  37. Simulate Strategies for Airline Check-In Queues

  38. 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

  39. 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

  40. 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

  41. Simulate Airline Check-In Queues

  42. Queues

  43. Queues

  44. Queues

  45. Queues

  46. Queues

  47. Queues

  48. Queues

  49. Queues

  50. Queues

More Related