1 / 17

Queues Queue API Application: Radix Sort Implementation: Using Deque Circular Array

Lecture 8 Queues and Priority Queues. Queues Queue API Application: Radix Sort Implementation: Using Deque Circular Array Priority Queue Priority Queue API Implementation Glimpse: Heaps. Queues. Three container adapters stack: LIFO discipline queue: FIFO priority_queue: HPFO

amato
Download Presentation

Queues Queue API Application: Radix Sort Implementation: Using Deque Circular Array

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. Lecture 8 Queues and Priority Queues • Queues • Queue API • Application: Radix Sort • Implementation: • Using Deque • Circular Array • Priority Queue • Priority Queue API • Implementation Glimpse: Heaps

  2. Queues • Three container adapters • stack: LIFO discipline • queue: FIFO • priority_queue: HPFO • Queue • <queue> • Remove from front, insert at back • Underlying sequence container: deque • Priority_queue • <queue> • top () yields HP element • Underlying SC: vector

  3. Queue Example string s1, s2; // Modify s1, leave s2==“” queue<char> q; size_t n = s1.length (); for (size_t i = 0; i < n; ++i) q.push (s1[n – i – 1]); while (! q.empty ()) { s2 += q.front (); q.pop (); } cout << boolalpha << (s1 == s2) << endl;

  4. 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 front of the queue. Precondition: The queue is not empty.

  5. CLASS queue <queue> Operations const T& front () const; Constant version of front(). T& back (); const T& back () const; 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.

  6. 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 CLASS queue <queue> Operations size_t size () const; Return the number of elements in the queue. 6

  7. Radix Sort Order ten 2 digit numbers in 10 bins from smallest to largest. Requires d = 2 passes, generally d = max (digits in an element) passes. Initial Sequence: 91 06 85 15 92 35 30 22 39 Pass 0: Distribute the #’s into bins according to the 1's digit (100).

  8. The Radix Sort New Sequence: 30 91 92 22 85 15 35 06 39 Pass 1: Take new sequence and distribute the #’s into bins determined by the 10's digit (101). Complexity? O (d*N)

  9. Implementing a Queue typedef Employee* value_type; class Queue { deque<value_type> d; public: Queue () : d () { } // Initially empty value_type &front () { ?? } value_type &back () { ?? } // Const versions too void push (const value_type& v) { d.push_back (v); } void pop () { ?? } bool empty () const { return d.empty (); } size_t size () const { return d.size (); } };

  10. Implementing a Queue (Alternative) typedef Employee* value_type; class Queue { value_type A[CAP]; size_t qFront, qBack, count; // [qFront, qBack) size_t decr (size_t index) { index += CAP - 1; return index % CAP; } size_t incr (size_t index) { ++index; return index % CAP; } public: Queue () : qFront (0), qBack (0), count (0) { } value_type& front () { return (A[qFront]); } value_type& back () { size_t last = decr (qBack); return (A[last]); } void push (const value_type& v) // Check overflow? { A[qBack] = v; qBack = incr (qBack); ++count; } void pop () // Underflow? { qFront = incr (qFront); --count; } bool empty () const { return (count == 0); } };

  11. Circular Array Impl.

  12. Priority Queue Form of queue in which values are removed according to their designated priority 3 2 6 1 Items entered the queue in sequential order but will be removed in the order Job #2, #1, #4, #3.

  13. Priority Queue Example string s1 = “Bjarne”, s2; priority_queue <char> pq; size_t n = s1.length (); for (size_t i = 0; i < n; i++) pq.push (s1[i]); while (! pq.empty ()) { s2 += q.top (); q.pop (); } cout << s2 << endl;

  14. priority_queue (); Create an empty priority queue. Type T must implement operator < (or provide comparator). CLASS priority_queue CLASS priority_queue <queue> <queue> Constructor Operations bool empty () const; Check whether the priority queue is empty. Return true if it is empty, and false otherwise. void pop (); Remove the item of highest priority from the queue. Precondition: The priority queue is not empty. Postcondition: The priority queue has 1 less element

  15. CLASS priority_queue <queue> Operations void push (const T& item); Insert the argument item into the priority queue. Postcondition: The priority queue contains a new element. size_t size () const; Return the number of items in the priority queue. T& top ();  NO! Why? Return a reference to the item having the highest priority. Precondition: The priority queue is not empty. const T& top () const; Constant version of top().

  16. Implementing a Priority Queue (Partial) typedef timer value_type; class PriorityQueue { vector<value_type> v; // v is maintained as a heap public: PriorityQueue () { } // initially empty const value_type& top () const { return v.front (); } void push (const value_type& i) { v.push_back (i); upHeap (); } void pop () { v[0] = v.back (); v.pop_back (); downHeap (); } bool empty () const { return v.empty (); } size_t size () const { return v.size (); } };

  17. Popping From a Heap v[0] = v.back ();

More Related