Date post: | 21-Dec-2015 |
Category: |
Documents |
Upload: | gervase-frank-allison |
View: | 219 times |
Download: | 2 times |
Queue, Deque, and Priority Queue Implementations
Chapter 11
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Contents
• A Linked Implementation of a Queue
• An Array-Based Implementation of a Queue A Circular Array A Circular Array with One Unused Location
• A Vector-Based Implementation of a Queue
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Contents
• Circular Linked Implementations of a Queue A Two-Part Circular Linked Chain
• Java Class Library: The Class AbstractQueue
• A Doubly Linked Implementation of a Deque
• Possible Implementations of a Priority Queue
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Objectives
• Implement ADT queue by using chain of linked nodes, an array, or vector
• Add or delete nodes at either end of chain of doubly linked nodes
• Implement ADT deque by using chain of doubly linked nodes
• Implement ADT priority queue by using array or chain of linked nodes
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Linked Implementation of a Queue
• Consider chain of linked nodes Head reference insufficient Must also have tail reference
• Which should be front of queue? Head easier to be front of queue for entry
removal Adding entries at tail/back of queue easily
done
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Figure 11-1 A chain of linked nodes that implements a queue
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Linked Implementation of a Queue
• Note code for linked Implementation Listing 11-1
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Figure 11-2 (a) Before adding a new node to an empty chain; (b) after adding it
Note: Code listing filesmust be in same folder
as PowerPoint filesfor links to work
Note: Code listing filesmust be in same folder
as PowerPoint filesfor links to work
Figure 11-3 (a) Before, (b) during, and (c) after adding a new node to the end of a nonempty chain that has a tail reference
Copyright ©2012 by Pearson Education, Inc. All rights reserved
FIGURE 11-4 (a) A queue of more than one entry; (b) after removing the entry at the front of the queue
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Figure 11-5 (a) A queue of one entry; (b) after removing the entry at the front of the queue
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Array-Based Implementation of a Queue
• Listing 11-2 : Array named queue, queue[0] is front frontIndex, backIndex are indices of front
and back of queue
• Now to decide … With queue[0] always as front, must shift
elements Instead, move frontIndex Then we run off the end of the array!?
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Figure 11-6 An array that represents a queue without moving any entries: (a) initially;
(b) after removing the entry at the front twice;Copyright ©2012 by Pearson Education, Inc. All rights reserved
Figure 11-6 An array that represents a queue without moving any entries: (c) after several more additions and removals;
(d) after two additions that wrap around to the beginning of the array;
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Array-Based Implementation of a Queue
• Solution: “Circular” array First location follows last Increment pointers with modulo operator
backIndex = (backIndex + 1) % queue.length;frontIndex = (frontIndex + 1) % queue.length;
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Figure 11-6 An array that represents a queue without moving any entries: (a) initially; (b) after removing
the entry at the front twice;Copyright ©2012 by Pearson Education, Inc. All rights reserved
Figure 11-6 An array that represents a queue without moving any entries: (c) after several more additions and removals;
(d) after two additions that wrap around to the beginning of the arrayCopyright ©2012 by Pearson Education, Inc. All rights reserved
Figure 11-7 A circular array that represents a queue: (a) when full; (b) after removing two entries;
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Figure 11-7 A circular array that represents a queue: (c) after removing three more entries; (d) after removing all but one entry;
(e) after removing the remaining entryCopyright ©2012 by Pearson Education, Inc. All rights reserved
Circular Array with One Unused Element
• Allows detection of empty vs. full queue Examine frontIndex, backIndex
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Figure 11-8 A seven-location circular array that contains at most six entries of a queue
Figure 11-8 A seven-location circular array that contains at most six entries of a queue
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Figure 11-8 A seven-location circular array that contains at most six entries of a queue
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Figure 11-9 An array-based queue: (a) initially; (b) after removing its front entry by incrementing frontIndex;
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Figure 11-9 An array-based queue(c) after removingits front entry by setting queue[frontIndex] to null and then
incrementing frontIndexCopyright ©2012 by Pearson Education, Inc. All rights reserved
Vector-Based Implementation of a Queue
• Front of queue at beginning of vector
• Vector add method used at back of queue
• Remove from front of queue Vector takes care of moving elements No indices needed
• Vector manages additional space as needed
• View Listing 11-3
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Figure 11-11 A vector that represents a queue
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Circular Linked Implementations of a Queue
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Figure 11-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
Two-Part Circular Linked Chain
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Figure 11-13 A two-part circular linked chain that represents both a queue and the nodes available to the queue
Figure 11-14 A two-part circular linked chain that represents a queue: (a) when it is empty; (b) after adding one entry;
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Figure 11-14 A two-part circular linked chain that represents a queue: (c) after adding three more entries;
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Figure 11-14 A two-part circular linked chain that represents a queue: (d) after removing the front entry;
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Figure 11-14 A two-part circular linked chain that represents a queue: (e) after adding one more entry
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Two-Part Circular Linked Chain
• Outline of the class, Listing 11-4
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Figure 11-15 A chain that requires a new node for an addition to a queue: (a) before the addition;
Figure 11-15 A chain that requires a new node for an addition to a queue: (b) after the addition
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Figure 11-16 (a) A chain with nodes available for additions to a queue;
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Figure 11-16 (b) the chain after one addition;
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Figure 11-16 (c) the chain after another addition;
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Java Class Library: The Class AbstractQueue
• Methods provided public boolean add(T newEntry) public boolean offer(T newEntry) public T remove() public T poll() public T element() public T peek() public boolean isEmpty() public void clear() public int size()
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Doubly Linked Implementation of a Deque
• Problem: each node in a chain references only the next node. We need to be able to move backward from a
node
• This suggests a doubly linked chain
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Doubly Linked Implementation of a Deque
• View implementation, Listing 11-5
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Figure 11-17 A doubly linked chain with head, tail references
Figure 11-18 Adding to the back of a nonempty deque: (a) after the new node is allocated;
(b) after the addition is completeCopyright ©2012 by Pearson Education, Inc. All rights reserved
Figure 11-19 (a) A deque containing at least two entries; (b) after removing the first node and obtaining a reference to the
deque’s new first entryCopyright ©2012 by Pearson Education, Inc. All rights reserved
Possible Implementations of a Priority Queue
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Figure 11-20 Two possible implementations of a priority queue using (a) an array; (b) a chain of linked nodes