Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Queues Queue API Application: Radix Sort Implementation: Using Deque Using Deque Circular Array Circular Array Priority Queue Priority Queue API Implementation."— Presentation transcript:

1 1 Queues Queue API Application: Radix Sort Implementation: Using Deque Using Deque Circular Array Circular Array Priority Queue Priority Queue API Implementation Glimpse: Heaps Lecture 8 Queues and Priority Queues

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

3 3 Queue Example string s1, s2; // Modify s1, leave s2==“” queue 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 4 CLASS queue Constructor queue (); Create an empty queue. CLASS queue 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 5 CLASS 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 66 CLASS 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 size_t size () const; Return the number of elements in the queue.

7 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 (10 0 ).

8 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 (10 1 ). Complexity?O (d*N)

9 9 Implementing a Queue typedef Employee* value_type; class Queue { deque 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 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 11 Circular Array Impl.

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

13 13 Priority Queue Example string s1 = “Bjarne”, s2; priority_queue 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 14 CLASS priority_queue Constructor priority_queue (); Create an empty priority queue. Type T must implement operator < (or provide comparator). CLASS priority_queue 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 15 CLASS priority_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 16 Implementing a Priority Queue (Partial) typedef timer value_type; class PriorityQueue { vector 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 17 Popping From a Heap v[0] = v.back ();


Download ppt "1 Queues Queue API Application: Radix Sort Implementation: Using Deque Using Deque Circular Array Circular Array Priority Queue Priority Queue API Implementation."

Similar presentations


Ads by Google