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.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Chapter 6 Queues and Deques.
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.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Queues Ellen Walker CPSC 201 Data Structures Hiram College.
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.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
COMP 121 Week 11: Linked Lists. Objectives Understand how single-, double-, and circular-linked list data structures are implemented Understand the LinkedList.
Lecture 8 CS203. Implementation of Data Structures 2 In the last couple of weeks, we have covered various data structures that are implemented in the.
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.
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.
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.
1 Queues Queue Concept Queue Design Considerations Queues in Java Collections APIs Queue Applications Reading L&C , 9.3.
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.
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 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.
Cmp Sci 187: Midterm Review Based on Lecture Notes.
Queues What is a Queue? Queue Implementations: Queue As Array
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
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.
Linked Lists. RHS – SOC 2 Linked lists We can already store collec- tions of objects in arrays and array lists – why would we need other data structures…?
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues (slightly modified by Dan Fleck)
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.
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 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:
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.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
18-1 Queues Data Structures and Design with Java and JUnit © Rick Mercer.
Implementation of QUEUE For more notes and topics visit: eITnotes.com.
COMP 121 Week 11: Linked Lists.
Lecture Objectives  Linked list data structures:  Singly-linked (cont.)  Doubly-linked  Circular  Implementing the List interface as a linked list.
CS 367 Introduction to Data Structures Lecture 5.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008.
Onlinedeeneislam.blogspot.com1 Design and Analysis of Algorithms Slide # 1 Download From
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Queues CS 367 – Introduction to Data Structures. Queue A queue is a data structure that stores data in such a way that the last piece of data stored,
CS-2851 Dr. Mark L. Hornick 1 Linked-List collections Structure and Implementation.
Queue ADT for lining up politely. COSC 2006 queue2 Queue – simple collection class  first-in first-out (FIFO) structure insert new elements at one end.
1 Data Organization Example 1: Heap storage management Maintain a sequence of free chunks of memory Find an appropriate chunk when allocation is requested.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Queues Rem Collier Room A1.02
Heaps And Priority Queues
CSE 373: Data Structures and Algorithms
Queues Queue Concept Queue Design Considerations
Top Ten Words that Almost Rhyme with “Peas”
THURSDAY, OCTOBER 17 IN LAB
Queues.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Chapter 4 Queues.
CSC 248 – fundamentals of Data structure
Presentation transcript:

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

Fall 2007CS 2253 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 –First-in first-out New elements are placed at the end of the line

Fall 2007CS 2254 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

Fall 2007CS 2255 A Print Queue

Fall 2007CS 2256 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

Fall 2007CS 2257 Queue Interface MethodBehavior boolean offer( E item)Insert item at the end of the queue. E remove()Remove the entry at front of the queue. Throws NoSuchElementException. E poll()Removes the entry at front. Returns null if there is none. E peek()Returns entry at front or null if there is none. E element()Returns entry at front. Throws NoSuchElementException.

Fall 2007CS 2258 LinkedList as a Queue Any doubly linked list class provides the basic functionality of a queue The Java 5.0 LinkedList class implements the Queue interface – Desired behavior (not actual behavior) –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.

Fall 2007CS 2259 Maintaining a Customer Queue Queue is good for storing a list of customers as they should be serviced in the order in which they arrived Data/ MethodPurpose private Queue customers A queue of customers public void processCustomers() Accepts and executs menu choices for processing customers.

Fall 2007CS Doubly-Linked List as Queues 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

Fall 2007CS Singly-Linked List as 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

Fall 2007CS Single-Linked List to Implement a Queue 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

Fall 2007CS Using a Circular Array for a Queue Simple 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 Using a circular array makes every operation constant time

Fall 2007CS Implementing a Queue Using a Circular Array

Fall 2007CS Implementing a Queue Using a Circular Array

Fall 2007CS Implementing a Queue Using a Circular Array

Fall 2007CS Implementing a Queue Using a Circular Array

Fall 2007CS 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

Fall 2007CS 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

Fall 2007CS 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

Fall 2007CS Simulating Waiting Lines Using Queues 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

Fall 2007CS Simulate a Strategy for Serving Airline Passengers

Fall 2007CS Simulate Strategies for Serving Airline Passengers Use the nouns in the problem description to come up with apreliminary set of potential classes.

Fall 2007CS UML Sequence Diagrams Sequence diagrams model a program by showing interactions between objects Actors and objects are displayed horizontally –Each object has a lifeline Time increases down the diagram Messages are represented by labeled arrows between objects

Fall 2007CS Sequence Diagrams Can model a program by showing interactions between objects Actors and objects are displayed horizontally –Each object has a lifeline Time increases down the diagram Labeled arrows represent messages between objects

Fall 2007CS Sequence Diagram for Simulation

Fall 2007CS Final UML Diagram

Fall 2007CS AirlineCheckinSim Data

Fall 2007CS AirlineCheckinSim Methods

Fall 2007CS PassengerQueue Data

Fall 2007CS PassengerQueue Methods

Fall 2007CS Passenger Methods

Fall 2007CS Simulation Input Parameters VariableAttributeConversion frequentFlyerQueue arrivalRate # arrivals per hourDivide by 60 regularPassengerQueue arrivalRate # arrivals per hourDivide by 60 frequentFlyerMax# frequent flyers between regular maxProcessingTimemax service time ( min) totalTimeTotal simulation time (min) showAllFlag to control simulation trace Y -> true

Fall 2007CS 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