Download presentation
Presentation is loading. Please wait.
Published byGervase Frank Allison Modified over 9 years ago
1
Queue, Deque, and Priority Queue Implementations Chapter 11 Copyright ©2012 by Pearson Education, Inc. All rights reserved
2
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
3
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
4
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
5
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
6
Figure 11-1 A chain of linked nodes that implements a queue Copyright ©2012 by Pearson Education, Inc. All rights reserved
7
Linked Implementation of a Queue Note code for linked Implementation Listing 11-1 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 files must be in same folder as PowerPoint files for links to work Note: Code listing files must be in same folder as PowerPoint files for links to work
8
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
9
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
10
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
11
Array-Based Implementation of a Queue Listing 11-2 : Array named queue,Listing 11-2 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
12
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
13
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
14
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
15
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
16
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
17
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
18
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 entry Copyright ©2012 by Pearson Education, Inc. All rights reserved
19
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
20
Copyright ©2012 by Pearson Education, Inc. All rights reserved
21
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
22
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
23
Figure 11-9 An array-based queue(c) after removing its front entry by setting queue[frontIndex] to null and then incrementing frontIndex Copyright ©2012 by Pearson Education, Inc. All rights reserved
24
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-3Listing 11-3 Copyright ©2012 by Pearson Education, Inc. All rights reserved
25
Figure 11-11 A vector that represents a queue Copyright ©2012 by Pearson Education, Inc. All rights reserved
26
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
27
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
28
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
29
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
30
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
31
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
32
Two-Part Circular Linked Chain Outline of the class, Listing 11-4Listing 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;
33
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
34
Figure 11-16 (a) A chain with nodes available for additions to a queue; Copyright ©2012 by Pearson Education, Inc. All rights reserved
35
Figure 11-16 (b) the chain after one addition; Copyright ©2012 by Pearson Education, Inc. All rights reserved
36
Figure 11-16 (c) the chain after another addition; Copyright ©2012 by Pearson Education, Inc. All rights reserved
37
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
38
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
39
Doubly Linked Implementation of a Deque View implementation, Listing 11-5Listing 11-5 Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 11-17 A doubly linked chain with head, tail references
40
Figure 11-18 Adding to the back of a nonempty deque: (a) after the new node is allocated; (b) after the addition is complete Copyright ©2012 by Pearson Education, Inc. All rights reserved
41
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 entry Copyright ©2012 by Pearson Education, Inc. All rights reserved
42
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
43
End Chapter 11 Copyright ©2012 by Pearson Education, Inc. All rights reserved
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.