Queue, Deque, and Priority Queue Implementations

Slides:



Advertisements
Similar presentations
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Advertisements

Inserting a Node into a Specified Position of a Linked List To create a node for the new item newNode = new Node(item); To insert a node between two nodes.
Stacks, Queues, and Linked Lists
queues1 Queues Data structures that wait their turn.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
CS Data Structures II Review COSC 2006 April 14, 2017
List Representation - Chain Fall 2010 CSE, POSTECH.
Completing the Linked Implementation of a List Chapter 7 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Queue, Deque, and Priority Queue Implementations Chapter 14.
Queues, Deques, and Priority Queues Chapter Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes.
Queue, Deque, and Priority Queue Implementations Chapter 24 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
List Implementations That Link Data Chapter 6. 2 Chapter Contents Linked Data Forming a Chains The Class Node A Linked Implementation Adding to End of.
CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test.
Queue, Deque, and Priority Queue Implementations.
Queue, Deque, and Priority Queue Implementations Chapter 11 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Queues, Deques and Priority Queues Chapter 10 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
Stack Implementations Chapter 6 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
CS 367 Introduction to Data Structures Lecture 5.
Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack)
Chapter 7 Queues Introduction Queue applications Implementations.
Queue, Deque, and Priority Queue Implementations Chapter 23.
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
Lists and Sorted Lists: various implementations Slides by Nadia Al-Ghreimil adopted from Steve Armstrong LeTourneau University Longview, TX  2007, 
1 Lecture 15: Big O Notation (Wednesday) Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science Test See Web for Details Don’t be deleted!
Queue ADT for lining up politely. COSC 2006 queue2 Queue – simple collection class  first-in first-out (FIFO) structure insert new elements at one end.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
Queues and Priority Queue Implementations Chapter 14 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Linked Data Structures
18 Chapter Stacks and Queues
Chapter 18: Stacks and Queues.
Queues Rem Collier Room A1.02
Doubly Linked List Review - We are writing this code
Thought for the Day “Without leaps of imagination, or dreaming, we lose the excitement of possibilities. Dreaming, after all, is a form of planning.”
Priority Queue.
CMSC 341 Lecture 5 Stacks, Queues
Queues, Deques and Priority Queues
Queue, Deque, and Priority Queue Implementations
A Bag Implementation that Links Data
Queues, Deques and Priority Queues
Stacks, Queues, and Deques
Chapter 19: Stacks and Queues.
Stack Implementations
Queues.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Queue and Priority Queue Implementations
Chapter 14: Queue and Priority Queue Implementations
Recitation 5 CS0445 Data Structures
A List Implementation That Links Data
Queues CSC212.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
A Heap Implementation Chapter 26 Adapted from Pearson Education, Inc.
ADT Queue (Array Implementation)
Queues Definition of a Queue Examples of Queues
Queues cont. Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Stacks, Queues, and Deques
Queues and Priority Queue Implementations
Getting queues right … finally (?)
A List Implementation That Links Data
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stack Implementations
A List Implementation that Links Data
Queue, Deque, and Priority Queue Implementations
Presentation transcript:

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

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.

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.

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.

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.

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.

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.

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.

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

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.

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.

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 = ( 4 + 1 ) % 5 = 0 == Front = 0 Adapted from Pearson Education, Inc.

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

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 = ( 1 + 1 ) % 5 = 2 =/= Front = 0 Adapted from Pearson Education, Inc.

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 = ( 4 + 1 ) % 5 = 0 == Front = 0 Adapted from Pearson Education, Inc.

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 = ( 4 + 1 ) % 5 = 0 =/= Front = 1 Adapted from Pearson Education, Inc.

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 = ( 4 + 1 ) % 5 = 0 =/= Front = 2 Adapted from Pearson Education, Inc.

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

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 = ( 1 + 1 ) % 5 = 2 == Front = 2 Adapted from Pearson Education, Inc.

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 = ( 4 + 1 ) % 5 = 0 == Front = 0 Adapted from Pearson Education, Inc.

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 = ( 0 + 1 ) % 5 = 1 =/= Front = 0 Adapted from Pearson Education, Inc.

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 = ( 1 + 1 ) % 5 = 2 =/= Front = 1 Adapted from Pearson Education, Inc.

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 = ( 3 + 2 ) % 5 = 0 == Front = 0 Adapted from Pearson Education, Inc.

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 = ( 3 + 2 ) % 5 = 0 =/= Front = 1 Adapted from Pearson Education, Inc.

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 = ( 3 + 1 ) % 5 = 4 =/= Front = 2 Adapted from Pearson Education, Inc.

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 = ( 4 + 1 ) % 5 = 0 =/= Front = 2 Adapted from Pearson Education, Inc.

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 = ( 0 + 1 ) % 5 = 1 =/= Front = 2 Adapted from Pearson Education, Inc.

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 = ( 0 + 2 ) % 5 = 2 == Front = 2 Adapted from Pearson Education, Inc.

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.

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.

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.

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.

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

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

End Chapter 11 Adapted from Pearson Education, Inc.