Queue, Deque, and Priority Queue Implementations Chapter 11 Copyright ©2012 by Pearson Education, Inc. All rights reserved.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 4.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
Dictionaries Chapter Chapter Contents Specifications for the ADT Dictionary Entries and methods Using the ADT Dictionary English Dictionary Telephone.
A Bag Implementation that Links Data Chapter 3 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
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. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
Queue, Deque, and Priority Queue Implementations Chapter 24 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Stack Implementations Chapter 22 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Queues Cmput Lecture 19 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from.
Queue, Deque, and Priority Queue Implementations.
Queues, Deques and Priority Queues Chapter 10 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues (slightly modified by Dan Fleck)
Bags Chapter 1 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
Stacks And Queues Chapter 18.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Stack Implementations Chapter 6 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Queues Chapter 5 Queue Definition A queue is an ordered collection of data items such that: –Items can be removed only at one end (the front of the queue)
© 2011 Pearson Addison-Wesley. All rights reserved 8 B-1 Chapter 8 (continued) Queues.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Queue, Deque, and Priority Queue Implementations Chapter 23.
Queues Another Linear ADT Copyright © 2009 Curt Hill.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
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.
18 Chapter Stacks and Queues
Csc 2720 Data structure Instructor: Zhuojun Duan
A Bag Implementation that Links Data
Queues, Deques and Priority Queues
Queue, Deque, and Priority Queue Implementations
Queue, Deque, and Priority Queue Implementations
Queues, Deques and Priority Queues
Linked Lists: Implementation of Queue & Deque
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Queue and Priority Queue Implementations
Chapter 14: Queue and Priority Queue Implementations
List Implementations that Use Arrays
A List Implementation That Links Data
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stack Implementations
Copyright ©2012 by Pearson Education, Inc. All rights reserved
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
A Heap Implementation Chapter 26 Adapted from Pearson Education, Inc.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Queues cont. Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Queues and Priority Queue Implementations
A List Implementation That Links Data
Copyright ©2012 by Pearson Education, Inc. All rights reserved
List Implementations that Use Arrays
Stack Implementations
A List Implementation that Links Data
Queue, Deque, and Priority Queue Implementations
Presentation transcript:

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 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

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,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

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 array Copyright ©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 entry Copyright ©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

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 removing its front entry by setting queue[frontIndex] to null and then incrementing frontIndex Copyright ©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-3Listing 11-3 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 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 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 A two-part circular linked chain that represents both a queue and the nodes available to the queue

Figure 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 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 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 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-4Listing 11-4 Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure A chain that requires a new node for an addition to a queue: (a) before the addition;

Figure 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 (a) A chain with nodes available for additions to a queue; Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure (b) the chain after one addition; Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure (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-5Listing 11-5 Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure A doubly linked chain with head, tail references

Figure 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

Figure (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

Possible Implementations of a Priority Queue Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure Two possible implementations of a priority queue using (a) an array; (b) a chain of linked nodes

End Chapter 11 Copyright ©2012 by Pearson Education, Inc. All rights reserved