1 / 20

Chapter 8

Chapter 8. Queues and Priority Queues. Outline. Model for a Queue The Queue Queue ADT Radix Sort miniQueue() Bounded Queue Priority Queue Priority Queue ADT. Grocery Store Checkout: A Model for a Queue. The Queue.

whinson
Download Presentation

Chapter 8

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. Chapter 8 Queues and Priority Queues

  2. Outline • Model for a Queue • The Queue • Queue ADT • Radix Sort • miniQueue() • Bounded Queue • Priority Queue • Priority Queue ADT

  3. Grocery Store Checkout: A Model for a Queue

  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.

  5. §- Queue - A first-come-first-served data structure. §- Insertion operations (push()) occur at the back of the sequence §- deletion operations (pop()) occur at the front of the sequence. 5

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

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

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

  9. §- The miniQueue class - Provides a class with STL queue class interface. - Uses the list class by object composition. 9

  10. The Bounded queue

  11. §- Implementing a queue with a fixed-size array - Indices qfront and qback move circularly around the array. - Gives O(1) time push() and pop() operations with no wasted space in the array. 12

  12. Queue class Static queue class (built on array): (P1101) Queue.h Example program using Queue.h Dynamic queue class (built on linked list): (P1109) DynamicQueue.h Example program using DynamicQueue.h

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

  14. The Radix Sort New Sequence: 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).

  15. §- The radix sort algorithm • Implementations: • Orders an integer vector by using queues (bins). • Divide the integer by a power (10^i) and push the integer into bin based on the remainder after division by 10 in the i-th pass • d_sort.h • Complexity • This sorting technique has running time O(n) but has only specialized applications. • The more general in-place O(n logn) sorting algorithms are preferable in most cases. 16

  16. Priority Queue A Special form of queue from which items are removed according to their designated priority and not the order in which they entered. Items entered the queue in sequential order but will be removed in the order #2, #1, #4, #3.

  17. §- Priority queue - Pop() returns the highest priority item (largest or smallest). - The push() and pop() operations have running time O(log2n) 18

  18. priority_queue(); Create an empty priority queue. Type T must implement the operator <. CLASS priority_queue CLASS priority_queue <queue> Constructor Operations bool empty() const; Check whether the priority queue is empty. Return true if it is empty, and false otherwise. Create 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

  19. 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. int size() const; Return the number of items in the priority queue. T& top(); Return a reference to the item having the highest priority. Precondition: The priority queue is not empty. const T& top(); Constant version of top().

More Related