Queue, Deque, and Priority Queue Implementations Chapter 23.

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

Stacks, Queues, and Linked Lists
Queues and Linked Lists
Data Structures(I) Circular Linked Lists Circular linked list A list in which every node has a successor; the last element is succeeded by the first element.
1 Array-based Implementation An array Q of maximum size N Need to keep track the front and rear of the queue: f: index of the front object r: index immediately.
Queues. Queue Definition Ordered list with property: All insertions take place at one end (tail) All insertions take place at one end (tail) All deletions.
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.
M180: Data Structures & Algorithms in Java
Queue & List Data Structures & Algorithm Abstract Data Types (ADTs) ADT is a mathematically specified entity that defines a set of its instances,
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.
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
CSC 212 – Data Structures. Using Stack Stack Limitations  Great for Pez dispensers, JVMs,& methods  All of these use most recent item added only 
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
CS 206 Introduction to Computer Science II 10 / 22 / 2008 Instructor: Michael Eckmann.
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 using an array. .head.tail Pointers head and tail always point to the first empty slot before or after elements in the list. Thus, initially they.
Stack Implementations Chapter Chapter Contents A Linked Implementation An Array-Based Implementation A Vector-Based Implementation.
CHAPTER 8 Lists. 2 A list is a linear collection Adding and removing elements in lists are not restricted by the collection structure We will examine.
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.
Main Index Contents 11 Main Index Contents Abstract Model of a List Obj. Abstract Model of a List Obj. Insertion into a List Insertion into a List Linked.
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.
Stack Implementations Chapter Chapter Contents A Linked Implementation An Array-Based Implementation A Vector-Based Implementation.
Queues Cmput Lecture 19 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from.
Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Queue, Deque, and Priority Queue Implementations.
Queue, Deque, and Priority Queue Implementations Chapter 11 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Chapter 3: Arrays, Linked Lists, and Recursion
CS 206 Introduction to Computer Science II 09 / 19 / 2008 Instructor: Michael Eckmann.
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
Chapter 16 Stack and Queues part2
Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
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.
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ LinkedList ◦ Set ◦ Map 2.
Stacks And Queues Chapter 18.
Cousin of the Stack.  An abstract data type (container class) in which items are entered at one end and removed from the other end  First In First.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
Stack Implementations Chapter 6 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Question of the Day  How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented.
Linear Data Structures
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
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,
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Templates 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.
Review Array Array Elements Accessing array elements
18 Chapter Stacks and Queues
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Queue, Deque, and Priority Queue Implementations
Queue, Deque, and Priority Queue Implementations
DATA STRUCTURE QUEUE.
Queue and Priority Queue Implementations
Chapter 14: Queue and Priority Queue Implementations
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Lecture 16 Section 6.2 Thu, Mar 1, 2007
Circular Queues: Implemented using Arrays
Queues and Priority Queue Implementations
Chapter 9 Linked Lists.
Stack Implementations
Queue, Deque, and Priority Queue Implementations
Presentation transcript:

Queue, Deque, and Priority Queue Implementations Chapter 23

2 Chapter Contents A Linked List 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 A Doubly Linked Implementation of a Queue Possible Implementations of a Priority Queue

3 A Linked Implementation of a Queue Use chain of linked nodes for the queue Two ends at opposite ends of chain Accessing last node inefficient Could keep a reference to the tail of the chain Place front of queue at beginning of chain Place back of queue at end of chain With references to both

4 A Linked Implementation of a Queue Fig A chain of linked nodes that implements a queue. Front of queue Back of queue

5 A Linked Implementation of a Queue Fig (a) Before adding a new node to an empty chain; (b) after adding to it.

6 A Linked Implementation of a Queue Fig (a) Before adding a new node to the end of a chain; (b) after adding it.

7 A Linked Implementation of a Queue Fig (a) A queue of more than one entry; (b) after removing the queue's front.

8 A Linked Implementation of a Queue Fig (a) A queue of one entry; (b) after removing the queue's front.

9 Array-Based Implementation of a Queue Let queue[0] be the front frontIndex, backIndex are indices of front and back If we insist queue[0] is front Must shift entries when we remove the front Instead move frontIndex Problem then is array can become full But now beginning of array could be empty and available for use

10 Array-Based Implementation of a Queue Fig An array that represents a queue without shifting its entries: (a) initially; (b) after removing the front twice;

11 Array-Based Implementation of a Queue Fig An array that represents a queue without shifting its entries: (c) after several more additions & removals; (d) after two additions that wrap around to the beginning of the array

12 A Circular Array When queue reaches end of array Add subsequent entries to beginning Array behaves as though it were circular First location follows last one Use modulo arithmetic on indices backIndex = (backIndex + 1) % queue.length Note: with circular array frontIndex == backIndex + 1 both when queue is empty and when full

13 A Circular Array Fig A circular array that represents a queue: (a) when full; (b) after removing 2 entries; (c) after removing 3 more entries;

14 A Circular Array Fig A circular array that represents a queue: (d) after removing all but one entry; (e) after removing remaining entry.

15 A Circular Array with One Unused Location Fig A seven-location circular array that contains at most six entries of a queue … continued → Allows us to distinguish between empty and full queue

16 A Circular Array with One Unused Location Fig (ctd.) A seven-location circular array that contains at most six entries of a queue.

17 Array-Based Implementation of a Queue Fig An array-base queue: (a) initially; (b) after removing its front by incrementing frontIndex ;

18 Array-Based Implementation of a Queue Fig An array-base queue: (c) after removing its front by setting queue[frontIndex] to null and then incrementing frontIndex.

19 Vector-Based Implementation of a Queue Maintain front of queue at beginning of vector Use addElement method to add entry at back Vector expands as necessary When remove front element, remaining elements move so new front is at beginning of vector Indexes at front and back not needed

20 Vector-Based Implementation of a Queue Fig A vector that represents a queue.

21 Circular Linked Implementations of a Queue Last node references first node Now we have a single reference to last node And still locate first node quickly No node contains a null When a class uses circular linked chain for queue Only one data item in the class The reference to the chain's last node

22 Circular Linked Implementations of a Queue Fig 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.

23 A Two-Part Linked Chain Linked nodes that form the queue followed by linked nodes available for use in the queue queueNode references front of queue node freeNode references first available node following end of queue In essence we have two chains One for the queue One for available nodes All joined in a circle

24 A Two-Part Linked Chain Fig A two-part circular linked chain that represents both a queue and the nodes available to the queue.

25 A Two-Part Linked Chain Fig A two-part circular linked chain that represents a queue: (a) when it is empty; (b) after adding one entry; (c) after adding three more entries.

26 A Two-Part Linked Chain Fig A two-part circular linked chain that represents a queue: (d) after removing the front; (e) after adding one more entry

27 A Two-Part Linked Chain Fig A chain that requires a new node for an addition to a queue: (a) before the addition; (b) after the addition.

28 A Two-Part Linked Chain Fig A chain with a node available for an addition to a queue: (a) before the addition; (b) after the addition.

29 A Doubly Linked Implementation of a Deque Chain with head reference enables reference of first and then the rest of the nodes Tail reference allows reference of last node but not next-to-last We need nodes that can reference both Previous node Next node Thus the doubly linked chain

30 A Doubly Linked Implementation of a Deque Fig A doubly linked chain with head and tail references

31 A Doubly Linked Implementation of a Deque Fig Adding to the back of a non empty deque: (a) after the new node is allocated; (b) after the addition is complete.

32 A Doubly Linked Implementation of a Deque Fig (a) a deque containing at least two entries; (b) after removing first node and obtaining reference to the deque's first entry.

33 Possible Implementations of a Priority Queue Fig Two possible implementations of a priority queue using (a) an array; (b) a chain of linked nodes.