Queues.

Slides:



Advertisements
Similar presentations
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Advertisements

Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
Queues CS 308 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: –Elements are added at.
What is a Queue? n Logical (or ADT) level: A queue is an ordered group of homogeneous items (elements), in which new elements are added at one end (the.
Queues CS-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.
Queues CS 3358 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: – Elements are added.
What is a Queue? A queue is a FIFO “first in, first out” structure.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Chapter 5 ADTs Stack and Queue. 2 Goals Describe a stack and its operations at a logical level Demonstrate the effect of stack operations using a particular.
1 A full binary tree A full binary tree is a binary tree in which all the leaves are on the same level and every non leaf node has two children. SHAPE.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
1 Chapter 4 Stack and Queue ADT. 2 Stacks of Coins and Bills.
1 Queues CPS212 Gordon College. 2 Introduction to Queues A queue is a waiting line – seen in daily life –Real world examples – toll booths, bank, food.
1 C++ Plus Data Structures Nell Dale Queues ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified.
1 Queues and Priority Queues Chapter 8. 2 Introduction to Queues A queue is a waiting line – seen in daily life –Real world examples – toll booths, bank,
Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Chapter 16 Stack and Queues part2
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Modified from the slides by Sylvia Sorkin, Community College of Baltimore County -
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Stacks And Queues Chapter 18.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
Queues Chapter 5 Queue Definition A queue is an ordered collection of data items such that: –Items can be removed only at one end (the front of the queue)
Give Eg:? Queues. Introduction DEFINITION: A Queue is an ordered collection of element in which insertions are made at one end and deletions are made.
1 Queues Chapter 4. 2 Objectives You will be able to Describe a queue as an ADT. Build a dynamic array based implementation of a queue ADT.
CSI 1340 Introduction to Computer Science II Chapter 6 Lists Plus.
Queues 1. Introduction A sequential collection of data Changes occur at both ends, not just one Queue applications –files waiting to be printed –"jobs"
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
1 Data Structures and Algorithms Queue. 2 The Queue ADT Introduction to the Queue data structure Designing a Queue class using dynamic arrays Linked Queues.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.
Review Array Array Elements Accessing array elements
CSCE 210 Data Structures and Algorithms
Data Structures Using C++ 2E
Data Structures Using C, 2e
Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009
Queues.
18 Chapter Stacks and Queues
CS505 Data Structures and Algorithms
Chapter 18: Stacks and Queues.
CC 215 Data Structures Queue ADT
Csc 2720 Data structure Instructor: Zhuojun Duan
C++ Plus Data Structures
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
CMSC 341 Lecture 5 Stacks, Queues
Queues.
Chapter 19: Stacks and Queues.
C++ Plus Data Structures
Queues.
CSC 143 Queues [Chapter 7].
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Using a Queue Chapter 8 introduces the queue data type.
C++ Plus Data Structures
Queues.
Using a Queue Chapter 8 introduces the queue data type.
Queues cont. Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
CSCS-200 Data Structure and Algorithms
Getting queues right … finally (?)
Queues.
Presentation transcript:

Queues

Presentation Contents Introduction to Queues Designing and Building a Queue Class – Array Based and Linked Queues Application of Queues: Buffers and Scheduling Case Study: Center Simulation

Presentation Objectives To study a queue as an ADT Build a static-array-based implementation of queues Build a dynamic-array-based implementation of queues Show how queues are used in I/O buffers and scheduling in a computer system See how queues are used in simulations of phenomena that involve waiting in lines

Introduction to Queues A queue is a waiting line – seen in daily life A line of people waiting for a bank teller A line of cars at a toll both "This is the captain, we're 5th in line for takeoff”

The Queue As an ADT A queue is a sequence of data elements In the sequence Items can be removed only at the front Items can be added only at the other end, the back Basic operations Construct a queue Check if empty/full Enqueue (add element to back) Front (retrieve value of element from front) Dequeue (remove element from front)

Example: Drill and Practice Problems We seek a program to present elementary math problems to students They are presented with a problem where they add randomly generated integers When they respond and are Successful – proceed to another problem Unsuccessful – problem stored for asking again later The program will determine the number of problems and largest values used in the operation Then it will generate that many problems and place them in the problem queue

Example: Drill and Practice Problems View source code of drill and practice program, Figures A, B and C

Designing and Building a Queue Class - Array-Based Consider an array in which to store a queue Note additional variables needed myFront, myBack Picture a queue object like this

Designing and Building a Queue Class - Array-Based Problems We quickly "walk off the end" of the array (how does this occur?) Possible solutions Shift array elements Use a circular queue with modulus Note that both empty and full queue gives myBack == myFront

Designing and Building a Queue Class - Array-Based Using a static array QUEUE_CAPACITY specified Enqueue increments myBack using mod operator, checks for full queue Dequeue increments myFront using mod operator, checks for empty queue Note declaration of Queue class, Fig 2A View implementation, Fig. 2B

Using Dynamic Array to Store Queue Elements Similar problems as with list and stack Fixed size array can be specified too large or too small Dynamic array design allows sizing of array for multiple situations Results in structure as shown myCapacity determined at run time

Linked Queues Even with dynamic allocation of queue size Array size is still fixed Cannot be adjusted during run of program Could use linked list to store queue elements Can grow and shrink to fit the situation No need for upper bound (myCapacity)

Linked Queues Constructor initializes myFront, myBack Front Dequeue return myFront->data Dequeue Delete first node (watch for empty queue) Enqueue Insert node at end of list View LQueue.h declaration, Fig 3A Note definition, LQueue.cpp, Fig 3B Driver program, Fig. 3C

Circular Linked List Possible to treat the linked list as circular Last node points back to first node Alternatively keep pointer to last node rather than first node

//-------------------------------------------------------- // CLASS TEMPLATE DEFINITION FOR CIRCULAR QUEUE #include "ItemType.h" // for ItemType template<class ItemType> class QueType { public: QueType( ); QueType( int max ); // PARAMETERIZED CONSTRUCTOR ~QueType( ) ; // DESTRUCTOR . . . bool IsFull( ) const; void Enqueue( ItemType item ); void Dequeue( ItemType& item ); private: int front; int rear; int maxQue; ItemType* items; // DYNAMIC ARRAY IMPLEMENTATION }; 15

//-------------------------------------------------------- // CLASS TEMPLATE DEFINITION FOR CIRCULAR QUEUE cont’d template<class ItemType> QueType<ItemType>::QueType( int max ) // PARAMETERIZED { maxQue = max; front = -1; rear = -1; items = new ItemType[maxQue]; // dynamically allocates } bool QueType<ItemType>::IsEmpty( ) return (front == -1 ) 16

//-------------------------------------------------------- // CLASS TEMPLATE DEFINITION FOR CIRCULAR QUEUE cont’d template<class ItemType> QueType<ItemType>::~QueType( ) { delete [ ] items; // deallocates array } . bool QueType<ItemType>::IsFull( ) { // WRAP AROUND return ( (rear + 1) % maxQue == front ) 17

SAYS ALL PUBLIC MEMBERS OF QueType CAN BE INVOKED FOR OBJECTS OF TYPE CountedQueType // DERIVED CLASS CountedQueType FROM BASE CLASS QueType template<class ItemType> class CountedQueType : public QueType<ItemType> { public: CountedQueType( ); void Enqueue( ItemType newItem ); void Dequeue( ItemType& item ); int LengthIs( ) const; // Returns number of items on the counted queue. private: int length; };

// Member function definitions for class CountedQue template<class ItemType> CountedQueType<ItemType>::CountedQueType( ) : QueType<ItemType>( ) { length = 0 ; } int CountedQueType<ItemType>::LengthIs( ) const return length ; 19

template<class ItemType> void CountedQueType<ItemType>::Enqueue( ItemType newItem ) // Adds newItem to the rear of the queue. // Increments length. { length++; QueType<ItemType>::Enqueue( newItem ); } void CountedQueType<ItemType>::Dequeue(ItemType& item ) // Removes item from the rear of the queue. // Decrements length. length--; QueType<ItemType>::Dequeue( item );

Application of Queues: Buffers and Scheduling Important use of queues is I/O scheduling Use buffers in memory to improve program execution Buffer arranged in FIFO structure

Application of Queues: Buffers and Scheduling Also times when insertions, deletions must be made from both ends Consider a scrolling window on the screen This requires a double ended queue Called a deque (pronounced "deck") Could also be considered a double ended stack (or "dack")

Application of Queues: Buffers and Scheduling Consider a keyboard buffer Acts as a queue But elements may be removed from the back of the queue with backspace key A printer spool is a queue of print jobs

Application of Queues: Buffers and Scheduling Queues used to schedule tasks within an operating system Job moves from disk to ready queue

Application of Queues: Buffers and Scheduling Ready queue may actually be a priority queue … job may get to "cut the line" based on its priority

Case Study: Information Center Simulation Most waiting lines (queues) are dynamic Their lengths grow and shrink over time Simulation models this dynamic process Enables study of the behavior of the process Modeled with one or more equations Queue behavior involves randomness We will use pseudorandom number generator

Problem Analysis and Specification Consider an information center Calls arrive at random intervals Placed in queue of incoming calls When agent becomes available, services call at front of queue We will simulate receipt of "calls" for some number of "minutes" we keep track of calls in the queue

Problem Analysis and Specification Input to the simulation program Time limit Arrival rate of calls Distribution of service times Desired output Number of calls processed Average waiting time per call Note declaration of Simulation class, Fig. 4

Problem Analysis and Specification Constructor Initialize input data members myTimer, myIncomingCalls initialized by their constructors The run() method Starts and manages simulation The checkForNewCall() method Random number generated, compared to myArrivalRate If new call has arrived, create new Call object, place in queue

Problem Analysis and Specification The service() method Checks time remaining for current call When done, retrieves and starts up next call from front of queue The display() method Report generated at end of simulation View definition of function members Fig. 5A Note driver program for simulation, Fig. 5B Reference the Timer class and Call class used in the Simulation class