Queues Chapter 6. Chapter Objectives  To learn how to represent a waiting line (queue) and how to use the methods in the Queue interface for insertion.

Slides:



Advertisements
Similar presentations
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Advertisements

Chapter 6 Queues and Deques.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Fundamentals of Python: From First Programs Through Data Structures
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Queues Ellen Walker CPSC 201 Data Structures Hiram College.
Today’s Agenda  Stacks  Queues  Priority Queues CS2336: Computer Science II.
CHAPTER 4 Queues. Queue  The queue, like the stack, is a widely used data structure  A queue differs from a stack in one important way  A stack is.
CHAPTER 4 Queues MIDTERM THURSDAY, OCTOBER 17 IN LAB.
Comp 245 Data Structures Queues. Introduction to the Queue ADT It is a FIFO (first-in, first-out) structure Access to the Queue can take place at two.
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
ITEC200 Week06 Queues. 2 Learning Objectives – Week06 Queues (Ch6) Students can Manage data using the queue Abstract Data Type.
CHAPTER 4 Queues. Chapter Objectives  To learn how to represent a waiting line (queue) and how to use the methods in the Queue interface for insertion.
© 2006 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
Queues Chapter 6 Chapter 6: Queues2 Chapter Objectives Learn how to represent a “waiting line”, i.e., a queue Learn how to use the methods in the Queue.
CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
Cmpt-225 Queues. A queue is a data structure that only allows items to be inserted at the end and removed from the front Queues are FIFO (First In First.
Abstractions, Collections & Data Structures Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Fall 2007CS 2251 Queues Chapter 6. Fall 2007CS 2252 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Queues Chapter 6. Chapter 6: Queues2 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in the Queue interface.
Fall 2007CS 2251 Queues Chapter 6. Fall 2007CS 2252 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in.
Queues What is a Queue? Queue Implementations: Queue As Array
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 2007.
Chapter 6.6, (event-driven simulation) Queues 1CSCI 3333 Data Structures.
Definition Stack is an ordered collection of data items in which access is possible only at one end (called the top of the stack). Stacks are known.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
Introduction to Data Structures. Data Structures A data structure is a scheme for organizing data in the memory of a computer. Some of the more commonly.
COMP 121 Week 14: Queues. Objectives Learn how to represent a queue Learn how to use the methods in the Queue interface Understand how to implement the.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues (slightly modified by Dan Fleck)
TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues.
CHAPTER 4 Queues. Chapter Objectives  To learn how to represent a waiting line (queue) and how to use the methods in the Queue interface for insertion.
Lists Based on content from: Java Foundations, 3rd Edition.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues.
Fall 2007CS 2251 Queues Chapter 6. Fall 2007CS 2252 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in.
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ LinkedList ◦ Set ◦ Map 2.
HIT2037- HIT6037 Software Development in Java 22 – Data Structures and Introduction.
Queues Chapter 6. Chapter 6: Queues Chapter Objectives To learn how to represent a waiting line (queue) and how to use the five methods in the Queue interface:
LECTURE 26: QUEUES CSC 212 – Data Structures. Using Stack.
A data structure is a type of data storage ….similar to an array. There are many data structures in Java (Stacks, Queues, LinkedList, Sets, Maps, HashTables,
Data Structures Using Java1 Chapter 7 Queues. Data Structures Using Java2 Chapter Objectives Learn about queues Examine various queue operations Learn.
Chapter Objectives  Learn how to represent a waiting line (queue)  Become proficient using the methods in the Queue  Understand how to implement the.
18-1 Queues Data Structures and Design with Java and JUnit © Rick Mercer.
Data Structures Types of Data Structure Data Structure Operations Examples Choosing Data Structures Data Structures in Alice.
COMP 121 Week 11: Linked Lists.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
© 2011 Pearson Addison-Wesley. All rights reserved 8 B-1 Chapter 8 (continued) Queues.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Linear Data Structures
CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008.
Onlinedeeneislam.blogspot.com1 Design and Analysis of Algorithms Slide # 1 Download From
INTRODUCTION TO DATA STRUCTURES 1. DATA STRUCTURES A data structure is a scheme for organizing data in the memory of a computer. Some of the more commonly.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Lecture 7 Queues Stacks Trees.
CSE 373: Data Structures and Algorithms
Stacks and Queues.
Monday, February 26, 2018 Announcements… For Today…
Building Java Programs
THURSDAY, OCTOBER 17 IN LAB
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.
Chapter 8 Queues © 2006 Pearson Addison-Wesley. All rights reserved.
Chapter 4 Queues.
Queues.
Chapter 6 – Queues and Deques
Presentation transcript:

Queues Chapter 6

Chapter Objectives  To learn how to represent a waiting line (queue) and how to use the methods in the Queue interface for insertion (offer and add), removal (remove and poll), and for accessing the element at the front (peek and element)  To understand how to implement the Queue interface using a single-linked list, a circular array, and a double- linked list  To understand how to simulate the operation of a physical system that has one or more waiting lines using Queues and random number generators  To learn how to represent a waiting line (queue) and how to use the methods in the Queue interface for insertion (offer and add), removal (remove and poll), and for accessing the element at the front (peek and element)  To understand how to implement the Queue interface using a single-linked list, a circular array, and a double- linked list  To understand how to simulate the operation of a physical system that has one or more waiting lines using Queues and random number generators

Queue Abstract Data Type  Can visualize a queue as a line of customers waiting for service  The next person to be served is the one who has waited the longest  New elements are placed at the end of the line  Can visualize a queue as a line of customers waiting for service  The next person to be served is the one who has waited the longest  New elements are placed at the end of the line

Queue Abstract Data Type (continued)

A Print Queue  Operating systems use queues to  Track of tasks waiting for a scarce resource  To ensure that the tasks are carried out in the order that they were generated  Print queue: printing is much slower than the process of selecting pages to print and so a queue is used  Operating systems use queues to  Track of tasks waiting for a scarce resource  To ensure that the tasks are carried out in the order that they were generated  Print queue: printing is much slower than the process of selecting pages to print and so a queue is used

A Print Queue (continued)

The Unsuitability of a Print Stack  Stacks are last-in, first-out (LIFO)  The most recently selected document would be the next to print  Unless the printer queue is empty, your print job may never get executed if others are issuing print jobs  Stacks are last-in, first-out (LIFO)  The most recently selected document would be the next to print  Unless the printer queue is empty, your print job may never get executed if others are issuing print jobs

Using a Queue for Traversing a Multi-Branch Data Structure  A graph models a network of nodes, with many links connecting each node to other nodes in the network  A node in a graph may have several successors  Programmers often use a queue to ensure that nodes closer to the starting point are visited before nodes that are farther away  A graph models a network of nodes, with many links connecting each node to other nodes in the network  A node in a graph may have several successors  Programmers often use a queue to ensure that nodes closer to the starting point are visited before nodes that are farther away

Specification for a Queue Interface

Class LinkedList Implements the Queue Interface  LinkedList class provides methods for inserting and removing elements at either end of a double-linked list  The Java 5.0 LinkedList class implements the Queue interface  Queue names = new LinkedList (); creates a new Queue reference, names, that stores references to String objects  The actual object referenced by names is type LinkedList  Because names is a type Queue reference, you can apply only the Queue methods to it.  LinkedList class provides methods for inserting and removing elements at either end of a double-linked list  The Java 5.0 LinkedList class implements the Queue interface  Queue names = new LinkedList (); creates a new Queue reference, names, that stores references to String objects  The actual object referenced by names is type LinkedList  Because names is a type Queue reference, you can apply only the Queue methods to it.

Maintaining a Queue of Customers  Queue is good for storing a list of customers as they should be serviced in the order in which they arrived  Algorithm for processCustomers  While the user is not finished  Display the menu and get the operation selected  Perform the operation selected  Queue is good for storing a list of customers as they should be serviced in the order in which they arrived  Algorithm for processCustomers  While the user is not finished  Display the menu and get the operation selected  Perform the operation selected

Maintaining a Queue of Customers (continued)

Using a Double-Linked List to Implement the Queue Interface  Insertion and removal from either end of a double-linked list is O(1) so either end can be the front (or rear) of the queue  Java designers decided to make the head of the linked list the front of the queue and the tail the rear of the queue  Limitation: LinkedList object is used as a queue, it may be possible to apply other LinkedList methods in addition to the ones required by the Queue interface  Insertion and removal from either end of a double-linked list is O(1) so either end can be the front (or rear) of the queue  Java designers decided to make the head of the linked list the front of the queue and the tail the rear of the queue  Limitation: LinkedList object is used as a queue, it may be possible to apply other LinkedList methods in addition to the ones required by the Queue interface

Using a Single-Linked List to Implement a Queue  Can implement a queue using a single-linked list  Class ListQueue contains a collection of Node objects  Insertions are at the rear of a queue and removals are from the front  Need a reference to the last list node  Number of elements in the queue is changed by methods insert and remove  Can implement a queue using a single-linked list  Class ListQueue contains a collection of Node objects  Insertions are at the rear of a queue and removals are from the front  Need a reference to the last list node  Number of elements in the queue is changed by methods insert and remove

Using a Single-Linked List to Implement a Queue (continued)

Implementing a Queue Using a Circular Array  Time efficiency of using a single- or double-linked list to implement a queue is acceptable  However there are some space inefficiencies  Storage space is increased when using a linked list due to references stored at each list node  Array Implementation  Insertion at rear of array is constant time  Removal from the front is linear time  Removal from rear of array is constant time  Insertion at the front is linear time  Time efficiency of using a single- or double-linked list to implement a queue is acceptable  However there are some space inefficiencies  Storage space is increased when using a linked list due to references stored at each list node  Array Implementation  Insertion at rear of array is constant time  Removal from the front is linear time  Removal from rear of array is constant time  Insertion at the front is linear time

Implementing a Queue Using a Circular Array (continued)

Implementing Class ArrayQueue.Iter  Just as for class ListQueue, we must implement the missing Queue methods and an inner class Iter to fully implement the Queue interface  Data field index stores the subscript of the next element to access  The constructor initializes index to front when a new Iter object is created  Data field count keeps track of the number of items accessed so far  Method Iter.remove throws an Unsupported- OperationException because it would violate the contract for a queue to remove an item other than the first one  Just as for class ListQueue, we must implement the missing Queue methods and an inner class Iter to fully implement the Queue interface  Data field index stores the subscript of the next element to access  The constructor initializes index to front when a new Iter object is created  Data field count keeps track of the number of items accessed so far  Method Iter.remove throws an Unsupported- OperationException because it would violate the contract for a queue to remove an item other than the first one

Comparing the Three Implementations  All three implementations are comparable in terms of computation time  Linked-list implementations require more storage because of the extra space required for the links  Each node for a single-linked list would store a total of two references  Each node for a double-linked list would store a total of three references  A circular array that is filled to capacity would require half the storage of a single-linked list to store the same number of elements  All three implementations are comparable in terms of computation time  Linked-list implementations require more storage because of the extra space required for the links  Each node for a single-linked list would store a total of two references  Each node for a double-linked list would store a total of three references  A circular array that is filled to capacity would require half the storage of a single-linked list to store the same number of elements

Simulating Waiting Lines Using Queues  Simulation is used to study the performance of a physical system by using a physical, mathematical, or computer model of the system  Simulation allows designers of a new system to estimate the expected performance before building it  Simulation can lead to changes in the design that will improve the expected performance of the new system  Useful when the real system would be too expensive to build or too dangerous to experiment with after its construction  Simulation is used to study the performance of a physical system by using a physical, mathematical, or computer model of the system  Simulation allows designers of a new system to estimate the expected performance before building it  Simulation can lead to changes in the design that will improve the expected performance of the new system  Useful when the real system would be too expensive to build or too dangerous to experiment with after its construction

Simulating Waiting Lines Using Queues (continued)  System designers often use computer models to simulate physical systems  Airline check-in counter for example  A special branch of mathematics called queuing theory has been developed to study such problems  System designers often use computer models to simulate physical systems  Airline check-in counter for example  A special branch of mathematics called queuing theory has been developed to study such problems

Simulate a Strategy for Serving Airline Passengers

Simulate a Strategy for Serving Airline Passengers (continued)

Simulating Waiting Lines Using Queues (continued)

Chapter Review  Queue is an abstract data type with a first-in, first-out structure (FIFO)  The Queue interface declares methods offer, remove, poll, peek, and element.  Three ways to implement the Queue interface: double- linked list, single-linked list, and circular array  To avoid the cost of building a physical system or running an actual experiment, computer simulation can be used to evaluate the expected performance of a system or operation strategy  Queue is an abstract data type with a first-in, first-out structure (FIFO)  The Queue interface declares methods offer, remove, poll, peek, and element.  Three ways to implement the Queue interface: double- linked list, single-linked list, and circular array  To avoid the cost of building a physical system or running an actual experiment, computer simulation can be used to evaluate the expected performance of a system or operation strategy