Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Fall 2007CS 2251 Queues Chapter 6

2 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

3 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

4 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

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

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

7 Fall 2007CS 2257 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 –http://java.sun.com/j2se/1.5.0/docs/api/java/util/Queue.htmlhttp://java.sun.com/j2se/1.5.0/docs/api/java/util/Queue.html 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.

8 Fall 2007CS 2258 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 Accepts and executs menu choices for processing customers. public void processCustomers()‏ A queue of customersprivate Queue customers PurposeData/ Method

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

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

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

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

13 Fall 2007CS 22513 Implementing a Queue Using a Circular Array

14 Fall 2007CS 22514 Implementing a Queue Using a Circular Array

15 Fall 2007CS 22515 Implementing a Queue Using a Circular Array

16 Fall 2007CS 22516 Implementing a Queue Using a Circular Array

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

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

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

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

21 Fall 2007CS 22521 Simulate a Strategy for Serving Airline Passengers

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

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

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

25 Fall 2007CS 22525 Sequence Diagram for Simulation

26 Fall 2007CS 22526 Final UML Diagram

27 Fall 2007CS 22527 AirlineCheckinSim Data

28 Fall 2007CS 22528 AirlineCheckinSim Methods

29 Fall 2007CS 22529 PassengerQueue Data

30 Fall 2007CS 22530 PassengerQueue Methods

31 Fall 2007CS 22531 Passenger Methods

32 Fall 2007CS 22532 Simulation Input Parameters Y -> trueFlag to control simulation trace showAll Total simulation time (min)‏ totalTime max service time ( min)‏ maxProcessingTime # frequent flyers between regular frequentFlyerMax Divide by 60# arrivals per hourregularPassengerQueue arrivalRate Divide by 60# arrivals per hourfrequentFlyerQueue arrivalRate ConversionAttributeVariable

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


Download ppt "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."

Similar presentations


Ads by Google