Presentation is loading. Please wait.

Presentation is loading. Please wait.

Queue, Deque, and Priority Queue Implementations

Similar presentations


Presentation on theme: "Queue, Deque, and Priority Queue Implementations"— Presentation transcript:

1 Queue, Deque, and Priority Queue Implementations
Chapter 11 Adapted from Pearson Education, Inc.

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 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 Adapted from Pearson Education, Inc.

3 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 Adapted from Pearson Education, Inc.

4 Consider Queue Operations
enqueue(newEntry) dequeue() getFront() Ask yourself: Is the head reference sufficient? What would the Big Oh of enqueuing be? Answer: Better to have tail as well so that enqueueing can be O(1) Two operations Operating at front Of the queue B X G A X Adapted from Pearson Education, Inc.

5 A Linked Implementation of the ADT Queue
public class LinkedQueue <T> implements QueueInterface <T> { private Node firstNode; // references front of queue private Node lastNode; // references back of queue public LinkedQueue () { firstNode = null; lastNode = null; } // end default constructor … ADT Queue methods go here … private class Node // private inner class { private T data; // entry in bag private Node next; // link to next node … } // end Node }// end LinkedQueue Adapted from Pearson Education, Inc.

6 Adding first item to an empty queue
Figure 11-2 Before adding a new node to an empty chain after adding it BOTH firstNode AND lastNode need adjustment Adapted from Pearson Education, Inc.

7 Adding an item to a non-empty queue
public void enqueue (T newEntry) { Node newNode = new Node (newEntry, null); if ( isEmpty()) firstNode = newNode; else lastNode.setNextNode(newNode); Adapted from Pearson Education, Inc.

8 Adding an item to a non-empty queue
public void enqueue (T newEntry) { Node newNode = new Node (newEntry, null); if ( isEmpty()) firstNode = newNode; else lastNode.setNextNode(newNode); lastNode = newNode; } // end enqueue ONLY lastNode needs adjustment Adapted from Pearson Education, Inc.

9 Removing an item from a queue with more than one item
ONLY firstNode needs adjustment Adapted from Pearson Education, Inc.

10 Removing last item from a queue
public T dequeue () { T front = null; if (!isEmpty ()) front = firstNode.data; firstNode = firstNode.next; if (firstNode == null) lastNode = null; } // end if return front; } // end dequeue BOTH firstNode AND lastNode need adjustment Adapted from Pearson Education, Inc.

11 An Array Implementation of the ADT Queue
public class ArrayQueue < T > implements QueueInterface < T > { private T [] queue; private int frontIndex; private int backIndex; private static final int DEFAULT_INITIAL_CAPACITY = 50; public ArrayQueue () { this (DEFAULT_INITIAL_CAPACITY); } // end default constructor public ArrayQueue (int initialCapacity) { T [] tempQueue = (T []) new Object [initialCapacity + 1]; queue = tempQueue; frontIndex = 0; backIndex = initialCapacity; } // end constructor …… ADT Queue methods go here … }// end ArrayQueue Adapted from Pearson Education, Inc.

12 An empty Queue [0] [1] [2] [3] [4] 4 Enqueue at back 
Front Back 4 Enqueue at back  incr back then store item The queue is EMPTY ( Back + 1 ) % size = ( ) % = 0 == Front = 0 Adapted from Pearson Education, Inc.

13 enQueue A [0] A [1] [2] [3] [4] Front Back The queue is not EMPTY
Back The queue is not EMPTY ( Back + 1 ) % size = ( ) % = 1 =/= Front = 0 Adapted from Pearson Education, Inc.

14 enQueue X [0] A [1] X [2] [3] [4] 1 Front Back The queue is not EMPTY
Back 1 The queue is not EMPTY ( Back + 1 ) % size = ( ) % = 2 =/= Front = 0 Adapted from Pearson Education, Inc.

15 enQueue G, B, and Z – it’s full now
Same As When Queue Was empty [0] A [1] X [2] G [3] B [4] Z Front Back 4 Dequeue from Front Serve front then incr front The queue is FULL BUT ( Back + 1 ) % size = ( ) % = 0 == Front = 0 Adapted from Pearson Education, Inc.

16 deQueue [0] [1] X [2] G [3] B [4] Z 1 4 Front Back
The queue is not Empty And not full ( Back + 1 ) % size = ( ) % = 0 =/= Front = 1 Adapted from Pearson Education, Inc.

17 deQueue [0] [1] [2] G [3] B [4] Z 2 4 Let’s enqueue 2 more: W and K
Front 2 Back 4 Let’s enqueue 2 more: W and K The queue is not Empty ( Back + 1 ) % size = ( ) % = 0 =/= Front = 2 Adapted from Pearson Education, Inc.

18 enQueue W [0] W [1] [2] G [3] B [4] Z 2 Front Back
The queue is not Empty ( Back + 1 ) % size = ( ) % = 1 =/= Front = 2 Adapted from Pearson Education, Inc.

19 enQueue K – it’s full again
[0] W [1] K [2] G [3] B [4] Z Front 2 Back 1 How can we avoid the condition for empty and full being the same? The queue is FULL BUT ( Back + 1 ) % size = ( ) % = 2 == Front = 2 Adapted from Pearson Education, Inc.

20 Start with an empty Queue again
[0] [1] [2] [3] [4] Front Back 4 One Location remains unused The queue is EMPTY ( Back + 1 ) % size = ( ) % = 0 == Front = 0 Adapted from Pearson Education, Inc.

21 One Location remains unused
enQueue A [0] A [1] [2] [3] [4] Front Back One Location remains unused The queue is not EMPTY ( Back + 1 ) % size = ( ) % = 1 =/= Front = 0 Adapted from Pearson Education, Inc.

22 One Location remains unused
enQueue X [0] A [1] X [2] [3] [4] Front Back 1 One Location remains unused The queue is not EMPTY ( Back + 1 ) % size = ( ) % = 2 =/= Front = 1 Adapted from Pearson Education, Inc.

23 enQueue G, and B – it’s full now
Different From When Queue Was empty [0] A [1] X [2] G [3] B [4] Front Back 3 One Location remains unused The queue is FULL ( Back + 2 ) % size = ( ) % = 0 == Front = 0 Adapted from Pearson Education, Inc.

24 One Location remains unused
deQueue [0] [1] X [2] G [3] B [4] Front 1 Back 3 One Location remains unused The queue is not Empty And not full ( Back + 2 ) % size = ( ) % = 0 =/= Front = 1 Adapted from Pearson Education, Inc.

25 One Location remains unused
deQueue [0] [1] [2] G [3] B [4] Front 2 Back 3 One Location remains unused Let’s enqueue 2 more: W and K The queue is not Empty ( Back + 1 ) % size = ( ) % = 4 =/= Front = 2 Adapted from Pearson Education, Inc.

26 One Location remains unused
enQueue W [0] [1] [2] G [3] B [4] W Front 2 Back 4 One Location remains unused The queue is not Empty ( Back + 1 ) % size = ( ) % = 0 =/= Front = 2 Adapted from Pearson Education, Inc.

27 One Location remains unused
enQueue K [0] K [1] [2] G [3] B [4] W Front 2 Back One Location remains unused Let’s enqueue 1 more: Z The queue is not Empty ( Back + 1 ) % size = ( ) % = 1 =/= Front = 2 Adapted from Pearson Education, Inc.

28 enQueue Z – can’t do it… already full
[0] K [1] [2] G [3] B [4] W Front 2 Back One Location remains unused The queue is FULL ( Back + 2 ) % size = ( ) % = 2 == Front = 2 Adapted from Pearson Education, Inc.

29 Methods enqueue and dequeue
public void enqueue (T newEntry) { ensureCapacity (); backIndex = (backIndex + 1) % queue.length; queue [backIndex] = newEntry; } // end enqueue public T dequeue () { T front = null; if (!isEmpty ()) { front = queue [frontIndex]; queue [frontIndex] = null; frontIndex = (frontIndex + 1) % queue.length; } // end if return front; } // end dequeue Adapted from Pearson Education, Inc.

30 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() Adapted from Pearson Education, Inc.

31 Doubly Linked Implementation of a Deque
public class LinkedDeque <T> implements DequeInterface <T> { private DLNode firstNode; // references front of deque private DLNode lastNode; // references back of deque public LinkedDeque () { firstNode = null; lastNode = null; } // end default constructor … ADT Queue methods go here … private class DLNode { private T data; // deque entry private DLNode next; // link to next node private DLNode previous; // link to previous node … } // end DLNode } // end LinkedDeque Adapted from Pearson Education, Inc.

32 Adding to the back of a non-empty deque
(a) after the new node is allocated; (b) after the addition is complete Adapted from Pearson Education, Inc.

33 Removing the first node from a deque containing at least two entries
Adapted from Pearson Education, Inc.

34 Possible Implementations of a Priority Queue
Figure Two possible implementations of a priority queue using (a) an array; (b) a chain of linked nodes Adapted from Pearson Education, Inc.

35 End Chapter 11 Adapted from Pearson Education, Inc.


Download ppt "Queue, Deque, and Priority Queue Implementations"

Similar presentations


Ads by Google