200 likes | 219 Views
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.
E N D
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
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.
§- 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
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.
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.
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.
§- The miniQueue class - Provides a class with STL queue class interface. - Uses the list class by object composition. 9
§- 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
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
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).
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).
§- 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
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.
§- Priority queue - Pop() returns the highest priority item (largest or smallest). - The push() and pop() operations have running time O(log2n) 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
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().