Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3.

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-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
DATA STRUCTURE & ALGORITHMS
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Unit : Overview of Queues.
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 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
CHP-4 QUEUE.
TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues.
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.
Data Structures Using C++ 2E Chapter 8 Queues. Data Structures Using C++ 2E2 Objectives Learn about queues Examine various queue operations Learn how.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
Exam Review 5/28/15. Software Development (Review)  Make it work…then make it pretty can result in dangerous code  Better to take a unified, consistent.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
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.
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)
© 2011 Pearson Addison-Wesley. All rights reserved 8 B-1 Chapter 8 (continued) Queues.
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.
Introduction Of Queue. Introduction A queue is a non-primitive linear data structure. It is an homogeneous collection of elements in which new elements.
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.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.
1 Data Organization Example 1: A simple text editor –Store the text buffer as a list of lines. –How would we implement the UNDO operation? Example 2: Parsing.
Queues Chapter 8 (continued)
Review Array Array Elements Accessing array elements
CSCE 210 Data Structures and Algorithms
Data Abstraction & Problem Solving with C++
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
ADT description Implementations
Queues.
Linked Lists Chapter 6 Section 6.4 – 6.6
Chapter 15 Lists Objectives
Csc 2720 Data structure Instructor: Zhuojun Duan
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Stacks and Queues.
Queues Queues Queues.
Algorithm and Data Structure Part III Dr. Naimah Yaakob
CMSC 341 Lecture 5 Stacks, Queues
Queues.
Array Lists Chapter 6 Section 6.1 to 6.3
Queues.
CSC 143 Queues [Chapter 7].
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
Queues CSC212.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Introduction to Data Structure
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
Queues cont. Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
CSCS-200 Data Structure and Algorithms
Data Structures – Week #4
[Most of the details about queues are left for to read about and work out in Lab 6.] Def. As a data structure, a queue is an ordered collection of data.
Getting queues right … finally (?)
Queues.
Presentation transcript:

Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Chapter Contents 8.1 Introduction to Queues 8.2 Designing and Building a Queue Class – Array Based 8.3 Linked Queues 8.4 Application of Queues: Buffers and Scheduling 8.5 Case Study: Center Simulation Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Chapter 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 (Optional) See how queues are used in simulations of phenomena that involve waiting in lines Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

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" What other kinds of queues can you think of Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

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 Enqueue (add element to back) Front (retrieve value of element from front) Dequeue (remove element from front) Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Example: Drill and Practice Problems We seek a program to present elementary math problems to students They are presented with a problem where they combine 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 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Example: Drill and Practice Problems For now, we will assume a queue class Note declaration of the AdditionProblem class, Fig 8.1A Implementation, AdditionProblem.cpp, Fig 8.1B View source code of drill and practice program, Fig. 8.1C Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

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 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Designing and Building a Queue Class Array-Based Problems We quickly "walk off the end" of the array Possible solutions Shift array elements Use a circular queue Note that both empty and full queue gives myBack == myFront Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

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 8.2A View implementation, Fig. 8.2B Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

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 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

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) Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

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 8.3A Note definition, LQueue.cpp, Fig 8.3B Driver program, Fig. 8.3C Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

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 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

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 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

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") Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

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 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Application of Queues: Buffers and Scheduling Queues used to schedule tasks within an operating system Job moves from disk to ready queue Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

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 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

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 us pseudorandom number generator Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

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 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

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. 8-4 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

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 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

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. 8-5A Note driver program for simulation, Fig. 8-5B Reference the Timer class and Call class used in the Simulation class Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3