Presentation is loading. Please wait.

Presentation is loading. Please wait.

Main Index Contents 11 Main Index Contents Model for a Queue Model for a Queue The Queue The Queue ADTQueue ADT (3 slides) Queue ADT Radix Sort Radix Sort.

Similar presentations


Presentation on theme: "Main Index Contents 11 Main Index Contents Model for a Queue Model for a Queue The Queue The Queue ADTQueue ADT (3 slides) Queue ADT Radix Sort Radix Sort."— Presentation transcript:

1 Main Index Contents 11 Main Index Contents Model for a Queue Model for a Queue The Queue The Queue ADTQueue ADT (3 slides) Queue ADT Radix Sort Radix Sort (2 slides) Radix Sort miniQueue() Bounded Queue Bounded Queue Priority Queue Priority Queue Priority Queue ADTPriority Queue ADT (2 slides) Priority Queue ADT Chapter 8 – Queues and Priority Queues Removing Items from a Heap Removing Items from a Heap Summary SlidesSummary Slides (5 slides) Summary Slides

2 Main Index Contents 22 Main Index Contents Grocery Store Checkout: A Model for a Queue

3 Main Index Contents 33 Main Index Contents 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.

4 Main Index Contents 44 Main Index Contents 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&l front(); Return a reference to the value of the item at the font of the queue. Precondition: The queue is not empty.

5 Main Index Contents 55 Main Index Contents CLASS queue Operations const T& front() const; Constant version of front(). 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 Main Index Contents 66 Main Index Contents 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 int size() const; Return the number of elements in the queue.

7 Main Index Contents 77 Main Index Contents 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 (10 0 ).

8 Main Index Contents 88 Main Index Contents The Radix Sort Final Sequence:91 6 85 15 92 35 30 22 39 Pass 1: Take the new sequence and distribute the cards into bins determined by the 10's digit (10 1 ).

9 Main Index Contents 99 Main Index Contents

10 Main Index Contents 10 Main Index Contents The Bounded queue

11 Main Index Contents 11 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.

12 Main Index Contents 12 Main Index Contents CLASS priority_queue Constructor priority_queue(); Create an empty priority queue. Type T must implement the operator <. CLASS priority_queue 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

13 Main Index Contents 13 Main Index Contents 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().

14 Main Index Contents 14 Removing Elements From a Heap

15 Main Index Contents 15 Main Index Contents Summary Slide 1 §- 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.

16 Main Index Contents 16 Main Index Contents Summary Slide 2 §- The radix sort algorithm -Orders an integer vector by using queues (bins). -This sorting technique has running time O(n) but has only specialized applications. -The more general in-place O(n log2n) sorting algorithms are preferable in most cases.

17 Main Index Contents 17 Main Index Contents Summary Slide 3 §- The miniQueue class -Provides a class with STL queue class interface. -Uses the list class by object composition.

18 Main Index Contents 18 Main Index Contents Summary Slide 4 §- 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.

19 Main Index Contents 19 s Main Index Contents Summary Slide 5 §- Priority queue -Pop() returns the highest priority item (largest or smallest). -Normally implemented by a heap, which is discussed in Chapter 14. -The push() and pop() operations have running time O(log 2 n)


Download ppt "Main Index Contents 11 Main Index Contents Model for a Queue Model for a Queue The Queue The Queue ADTQueue ADT (3 slides) Queue ADT Radix Sort Radix Sort."

Similar presentations


Ads by Google