Queue, Deque, and Priority Queue Implementations
Queue, Deque, and Priority Queue Implementations
Chapter 24
Chapter Contents
A Linked List Implementation of a Queue An Array-Based Implementation of a Queue
A Circular Array A Circular Array with One Unused Location
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Chapter Contents
Java Class Library: The Class AbstractQueue A Doubly Linked Implementation of a Queue Possible Implementations of a Priority Queue Java Class Library: The Class PriorityQueue
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Place front of queue at beginning of chain Place back of queue at end of chain
With references to both
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Fig. 24-2 (a) Before adding a new node to an empty chain; (b) after adding to it.
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Fig. 24-3 (a) Before adding a new node to the end of a chain; (b) after adding it.
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Fig. 24-4 (a) A queue of more than one entry; (b) after removing the queue's front.
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Fig. 24-5 (a) A queue of one entry; (b) after removing the queue's front.
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
and back
Fig. 24-6 An array that represents a queue without shifting its entries: (a) initially; (b) after removing the front twice;
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Fig. 24-6 An array that represents a queue without shifting its entries: (c) after several more additions & removals; (d) after two additions that wrap around to the beginning of the array
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
A Circular Array
When queue reaches end of array
Add subsequent entries to beginning
A Circular Array
Fig. 24-7 A circular array that represents a queue: (a) when full; (b) after removing 2 entries; (c) after removing 3 more entries;
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
A Circular Array
Fig. 24-7 A circular array that represents a queue: (d) after removing all but one entry; (e) after removing remaining entry.
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Fig. 24-9 An array-base queue: (a) initially; (b) after removing its front by incrementing frontIndex;
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Fig. 24-9 An array-base queue: (c) after removing its front by setting queue[frontIndex] to null, then incrementing frontIndex.
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
When remove front element, remaining elements move so new front is at beginning of vector
Indexes at front and back not needed
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
No node contains a null When a class uses circular linked chain for queue
Only one data item in the class The reference to the chain's last node
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Fig. 24-12 A circular linked chain with an external reference to its last node that (a) has more than one node; (b) has one node; (c) is empty.
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Fig. 24-13 A two-part circular linked chain that represents both a queue and the nodes available to the queue.
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Fig. 24-14 A two-part circular linked chain that represents a queue: (a) when it is empty; (b) after adding one entry; (c) after adding three more entries.
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Fig. 24-15 A chain that requires a new node for an addition to a queue: (a) before the addition; (b) after the addition.
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Fig. 24-16 A chain with a node available for an addition to a queue: (a) before the addition; (b) after the addition.
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Fig. 24-17 A doubly linked chain with head and tail references
Click to view source code of linked implementation of ADT deque
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Fig. 24-18 Adding to the back of a non empty deque: (a) after the new node is allocated; (b) after the addition is complete.
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Fig. 24-19 (a) a deque containing at least two entries; (b) after removing first node and obtaining reference to the deque's first entry.
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Fig. 24-20 Two possible implementations of a priority queue using (a) an array; (b) a chain of linked nodes.
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X