160 likes | 259 Views
Introduction to Data Structure. Chapter 8 Ming Li Department of Computer Science California State University, Fresno Spring 2007. Grocery Store Checkout: A Model for a Queue. Queue - Illustration.
E N D
Introduction to Data Structure Chapter 8 Ming Li Department of Computer Science California State University, Fresno Spring 2007
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.
Queue - Operations 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 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> 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. 6
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 (100).
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 (101).
The Bounded queue Famous producer/consumer problem
The Bounded queue Qback = (qback + 1)% MAXQSIZE Qfront = (qfront + 1)% MAXQSIZE How to know if a queue is full?
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 - Operations 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 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().
Queue - Traversal while(!q.empty()) q.pop(); q.pop();
Queue - Search if(q.front() != 6) q.pop(); while(!q.empty() && q.front() != 6) q.pop();