View
214
Download
1
Tags:
Embed Size (px)
Citation preview
ITEC200 Week06
Queues
www.ics.mq.edu.au/ppdp 2
Learning Objectives – Week06Queues (Ch6)
Students can• Manage data using the queue Abstract Data Type and
the methods provided in it’s Java implementation (offer, remove, poll, peek and element)
• Compare and contrast implementations of the Queue interface (single-linked list, double-linked list, circular array) and make augmentations to them
• Analyse and augment a simulation of a physical system that uses Queues and random number generators
• Recognise appropriate circumstances to implement queues for data management and simulation purposes
www.ics.mq.edu.au/ppdp 3
Queue Abstract Data Type
• Queue ADT is an organisation of data whereby insertion occurs at the tail and removal occurs at the head.
• 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
www.ics.mq.edu.au/ppdp 4
Applications of Queues
• Operating systems use queues to sequence tasks waiting for a scarce resource– Printer queue
– Tasks waiting for CPU
• Simulation of physical systems uses queues to simulate any ‘first-in first-out’ (FIFO) system– Supermarket checkouts
– Tollbooths
www.ics.mq.edu.au/ppdp 5
Specification for a Queue Interface
www.ics.mq.edu.au/ppdp 6
Class LinkedList Implements the Queue Interface
• The Java 5.0 LinkedList class implements the Queue interface: Queue<String> names = new LinkedList<String>();
creates a new Queue reference, names, that stores references to String objects
• The actual object referenced by names is type LinkedList<String>
• Because names is a type Queue<String> reference, you can apply only the Queue methods to it.
www.ics.mq.edu.au/ppdp 7
Case Study: Maintaining a Queue of Customers
• Class MaintainQueue provides a GUI to perform operations on the queue
• Algorithms for GUI and for finding the position of a person in the queue are considered
www.ics.mq.edu.au/ppdp 8
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
www.ics.mq.edu.au/ppdp 9
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<E>
objects
www.ics.mq.edu.au/ppdp 10
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
• Implementing a Queue as a Circular Array overcomes space inefficiencies
www.ics.mq.edu.au/ppdp 11
Implementing a Queue Using a Circular Array (continued)
www.ics.mq.edu.au/ppdp 12
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 stores 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
www.ics.mq.edu.au/ppdp 13
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
www.ics.mq.edu.au/ppdp 14
Case Study: Simulating a Strategy for Serving Airline Passengers
www.ics.mq.edu.au/ppdp 15
Simulate a Strategy for Serving Airline Passengers (continued)
www.ics.mq.edu.au/ppdp 16
Simulate a Strategy for Serving Airline Passengers (continued)
www.ics.mq.edu.au/ppdp 17
Simulating Waiting Lines Using Queues (continued)
www.ics.mq.edu.au/ppdp 18
Simulating Waiting Lines Using Queues (continued)
www.ics.mq.edu.au/ppdp 19
Simulating Waiting Lines Using Queues (continued)
www.ics.mq.edu.au/ppdp 20
Simulating Waiting Lines Using Queues (continued)
www.ics.mq.edu.au/ppdp 21
Simulating Waiting Lines Using Queues (continued)
www.ics.mq.edu.au/ppdp 22
Simulating Waiting Lines Using Queues (continued)
www.ics.mq.edu.au/ppdp 23
Where to from here…
• Work through Chapter 6 of the Koffman & Wolfgang Text
• Conceptual Questions and Practical Exercises• Submit all preliminary work• Be prompt for your online class
www.ics.mq.edu.au/ppdp 24
Acknowledgements
These slides were based upon the Objects, Abstraction, Data Structures and Design using Java Version 5.0 Chapter 6 PowerPoint presentation
by Elliot B. Koffman and Paul A. T. Wolfgang