Download presentation
Presentation is loading. Please wait.
2
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 8 Ming Li Department of Computer Science California State University, Fresno Spring 2007
3
Introduction to Data Structure, Fall 2006 Slide- 2 California State University, Fresno Grocery Store Checkout: A Model for a Queue
4
Introduction to Data Structure, Fall 2006 Slide- 3 California State University, Fresno Queue - Illustration 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
Introduction to Data Structure, Fall 2006 Slide- 4 California State University, Fresno queue(); Create an empty queue. 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. Queue - Operations
6
Introduction to Data Structure, Fall 2006 Slide- 5 California State University, Fresno 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. Queue - Operations
7
Introduction to Data Structure, Fall 2006 Slide- 6 California State University, Fresno 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 int size() const; Return the number of elements in the queue. Queue - Operations
8
Introduction to Data Structure, Fall 2006 Slide- 7 California State University, Fresno Queue - 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 ).
9
Introduction to Data Structure, Fall 2006 Slide- 8 California State University, Fresno Queue - 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 ).
10
Introduction to Data Structure, Fall 2006 Slide- 9 California State University, Fresno Queue - Implementation
11
Introduction to Data Structure, Fall 2006 Slide- 10 California State University, Fresno The Bounded queue Famous producer/consumer problem
12
Introduction to Data Structure, Fall 2006 Slide- 11 California State University, Fresno The Bounded queue Qback = (qback + 1)% MAXQSIZE Qfront = (qfront + 1)% MAXQSIZE How to know if a queue is full?
13
Introduction to Data Structure, Fall 2006 Slide- 12 California State University, Fresno 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.
14
Introduction to Data Structure, Fall 2006 Slide- 13 California State University, Fresno priority_queue(); Create an empty priority queue. Type T must implement the operator <. 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 Priority Queue - Operations
15
Introduction to Data Structure, Fall 2006 Slide- 14 California State University, Fresno 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(). Priority Queue - Operations
16
Introduction to Data Structure, Fall 2006 Slide- 15 California State University, Fresno Queue - Traversal q.pop(); while(!q.empty()) q.pop();
17
Introduction to Data Structure, Fall 2006 Slide- 16 California State University, Fresno Queue - Search if(q.front() != 6) q.pop(); while(!q.empty() && q.front() != 6) q.pop();
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.