+ All Categories
Home > Documents > Queues Chapter 6. Chapter Objectives To learn how to represent a waiting line (queue) and how to...

Queues Chapter 6. Chapter Objectives To learn how to represent a waiting line (queue) and how to...

Date post: 19-Dec-2015
Category:
View: 223 times
Download: 5 times
Share this document with a friend
Popular Tags:
35
Queues Chapter 6
Transcript
Page 1: 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.

QueuesQueues

Chapter 6Chapter 6

Page 2: 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.

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

Page 3: 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.

Queue Abstract Data TypeQueue 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

Page 4: 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.

Queue Abstract Data Type (continued)

Queue Abstract Data Type (continued)

Page 5: 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.

A Print QueueA 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

Page 6: 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.

A Print Queue (continued)A Print Queue (continued)

Page 7: 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.

The Unsuitability of a Print Stack

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

Page 8: 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.

Using a Queue for Traversing a Multi-Branch Data StructureUsing 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

Page 9: 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.

Specification for a Queue Interface

Specification for a Queue Interface

Page 10: 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.

Class LinkedList Implements the 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<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.

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

Page 11: 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.

Maintaining a Queue of Customers

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

Page 12: 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.

Maintaining a Queue of Customers (continued)Maintaining a Queue of Customers (continued)

Page 13: 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.

Using a Double-Linked List to Implement the Queue Interface

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

Page 14: 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.

Using a Single-Linked List to Implement a Queue

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

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

Page 15: 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.

Using a Single-Linked List to Implement a Queue

(continued)

Using a Single-Linked List to Implement a Queue

(continued)

Page 16: 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.

Implementing a Queue Using a Circular Array

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

Page 17: 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.

Implementing a Queue Using a Circular Array (continued)

Implementing a Queue Using a Circular Array (continued)

Page 18: 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.

Implementing a Queue Using a Circular Array (continued)

Implementing a Queue Using a Circular Array (continued)

Page 19: 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.

Implementing a Queue Using a Circular Array (continued)

Implementing a Queue Using a Circular Array (continued)

Page 20: 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.

Implementing a Queue Using a Circular Array (continued)

Implementing a Queue Using a Circular Array (continued)

Page 21: 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.

Implementing Class ArrayQueue<E>.IterImplementing Class ArrayQueue<E>.Iter

Just as for class ListQueue<E>, 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<E>, 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

Page 22: 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.

Comparing the Three Implementations

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

Page 23: 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.

Simulating Waiting Lines Using Queues

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

Page 24: 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.

Simulating Waiting Lines Using Queues (continued)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

Page 25: 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.

Simulate a Strategy for Serving Airline Passengers

Simulate a Strategy for Serving Airline Passengers

Page 26: 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.

Simulate a Strategy for Serving Airline Passengers

(continued)

Simulate a Strategy for Serving Airline Passengers

(continued)

Page 27: 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.

Simulate a Strategy for Serving Airline Passengers

(continued)

Simulate a Strategy for Serving Airline Passengers

(continued)

Page 28: 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.

Simulating Waiting Lines Using Queues (continued)Simulating Waiting Lines Using Queues (continued)

Page 29: 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.

Simulating Waiting Lines Using Queues (continued)Simulating Waiting Lines Using Queues (continued)

Page 30: 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.

Simulating Waiting Lines Using Queues (continued)Simulating Waiting Lines Using Queues (continued)

Page 31: 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.

Simulating Waiting Lines Using Queues (continued)Simulating Waiting Lines Using Queues (continued)

Page 32: 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.

Simulating Waiting Lines Using Queues (continued)Simulating Waiting Lines Using Queues (continued)

Page 33: 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.

Simulating Waiting Lines Using Queues (continued)Simulating Waiting Lines Using Queues (continued)

Page 34: 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.

Simulating Waiting Lines Using Queues (continued)Simulating Waiting Lines Using Queues (continued)

Page 35: 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.

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


Recommended