1 / 16

Introduction to Data Structure

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.

quinta
Télécharger la présentation

Introduction to Data Structure

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. Introduction to Data Structure Chapter 8 Ming Li Department of Computer Science California State University, Fresno Spring 2007

  2. Grocery Store Checkout: A Model for a Queue

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

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

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

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

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

  9. Queue - Implementation

  10. The Bounded queue Famous producer/consumer problem

  11. The Bounded queue Qback = (qback + 1)% MAXQSIZE Qfront = (qfront + 1)% MAXQSIZE How to know if a queue is full?

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

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

  14. 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().

  15. Queue - Traversal while(!q.empty()) q.pop(); q.pop();

  16. Queue - Search if(q.front() != 6) q.pop(); while(!q.empty() && q.front() != 6) q.pop();

More Related