Presentation is loading. Please wait.

Presentation is loading. Please wait.

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:

Similar presentations


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

1 Queues Chapter 6

2 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: insert, retrieve, peek, getSize, and isEmpty 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 Chapter 6: Queues 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

4 Chapter 6: Queues Queue Abstract Data Type (continued)

5 Chapter 6: Queues 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

6 Chapter 6: Queues A Print Queue (continued)

7 Chapter 6: Queues 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

8 Chapter 6: Queues 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

9 Chapter 6: Queues Specification for a Queue Interface

10 Chapter 6: Queues 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

11 Chapter 6: Queues Maintaining a Queue of Customers (continued)

12 Chapter 6: Queues Maintaining a Queue of Customers (continued)

13 Chapter 6: Queues Using a Single-Linked List to Implement a Queue Can implement a queue using a single-linked list 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

14 Chapter 6: Queues Using a Single-Linked List to Implement a Queue (continued)

15 Chapter 6: Queues Implementing a Queue Using Java’s LinkedList Can be implemented as an adapter of any class that implements the List interface ArrayList Vector LinkedList Removal is O(n) with a linked list O(n) when using ArrayList or Vector

16 Chapter 6: Queues 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

17 Chapter 6: Queues Implementing a Queue Using a Circular Array (continued)

18 Chapter 6: Queues Implementing a Queue Using a Circular Array (continued)

19 Chapter 6: Queues Implementing a Queue Using a Circular Array (continued)

20 Chapter 6: Queues Implementing a Queue Using a Circular Array (continued)

21 Chapter 6: Queues 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

22 Chapter 6: 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

23 Chapter 6: Queues 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

24 Chapter 6: Queues Simulate a Strategy for Serving Airline Passengers

25 Chapter 6: Queues Simulate a Strategy for Serving Airline Passengers (continued)

26 Chapter 6: Queues Simulate a Strategy for Serving Airline Passengers (continued)

27 Chapter 6: Queues Simulating Waiting Lines Using Queues (continued)

28 Chapter 6: Queues Simulating Waiting Lines Using Queues (continued)

29 Chapter 6: Queues Simulating Waiting Lines Using Queues (continued)

30 Chapter 6: Queues Simulating Waiting Lines Using Queues (continued)

31 Chapter 6: Queues Chapter Review Queue is an abstract data type with a first-in, first-out structure (FIFO) The Queue interface declares methods insert, remove, peek, size, and isEmpty Three ways to implement the Queue interface: single- linked list, circular array, and as an adapter of the LinkedList class 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 "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:"

Similar presentations


Ads by Google