18-1 Queues Data Structures and Design with Java and JUnit © Rick Mercer.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
queues1 Queues Data structures that wait their turn.
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
5/4/2015ITK 2751 Queue a FIFO (First In First Out) efbh front rear poll, remove offer peek, element capacity size.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
CHAPTER 4 Queues. Queue  The queue, like the stack, is a widely used data structure  A queue differs from a stack in one important way  A stack is.
CHAPTER 4 Queues MIDTERM THURSDAY, OCTOBER 17 IN LAB.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
Queues Chapter 6. Chapter Objectives  To learn how to represent a waiting line (queue) and how to use the methods in the Queue interface for insertion.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Queues Chapter 6. Chapter 6: Queues2 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in the Queue interface.
Fall 2007CS 2251 Queues Chapter 6. Fall 2007CS 2252 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in.
Queue, Deque, and Priority Queue Implementations Chapter 11 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
CSE 143 Lecture 7 Stacks and Queues reading: Stuart Reges notes on website slides created by Marty Stepp
Building Java Programs
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
CSE 143 Lecture 7 Stacks and Queues reading: "Appendix Q" (see course website) slides created by Marty Stepp and Hélène Martin
COMP 121 Week 14: Queues. Objectives Learn how to represent a queue Learn how to use the methods in the Queue interface Understand how to implement the.
Topic 3 The Stack ADT.
Queues Tonga Institute of Higher Education. Definitions Queue - A data structure that stores a number of items. The first item entered into a queue is.
Fall 2007CS 2251 Queues Chapter 6. Fall 2007CS 2252 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in.
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture6.
Data Structures Using Java1 Chapter 7 Queues. Data Structures Using Java2 Chapter Objectives Learn about queues Examine various queue operations Learn.
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.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Queue Definition A queue is a data structure with elements of the same type. Queue elements can only be entered at one end of the queue, called the back,
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Iterators, Iterator, and Iterable 2015-T2 Lecture 8 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Thomas Kuehne.
Topic 13 Iterators. 9-2 Motivation We often want to access every item in a data structure or collection in turn We call this traversing or iterating over.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Queues Another Linear ADT Copyright © 2009 Curt Hill.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Queues CS 367 – Introduction to Data Structures. Queue A queue is a data structure that stores data in such a way that the last piece of data stored,
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Click to edit Master text styles Stacks Data Structure.
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
Building Java Programs
Stacks and Queues.
CSE 373: Data Structures and Algorithms
Stacks and Queues.
Stacks and Queues.
Stacks and Queues.
Building Java Programs
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
THURSDAY, OCTOBER 17 IN LAB
Queues, Deques and Priority Queues
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Stacks and Queues.
Building Java Programs
Building Java Programs
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Stacks and Queues CLRS, Section 10.1.
slides created by Marty Stepp
Chapter 4 Queues.
Stacks and Queues.
Presentation transcript:

18-1 Queues Data Structures and Design with Java and JUnit © Rick Mercer

18-2 A Queue ADT First in first out access  A Queue is another name for waiting line  Example: Submit jobs to a network printer  What gets printed first?  Print the least recently submitted no priorities given  Add new print jobs at the end of the queue  Queues provide First In First Out (FIFO) access to elements could also say Last In Last Out (LILO)

18-3 The Java Queue class?  Some languages have a Queue class or queue is part of a library that works with the language  Java 1.4 used class LinkedList to offer FIFO functionality by adding methods addLast(Object) and Object(getFirst)  Java 1.5 added a Queue interface and several collection classes: ArrayBlockingQueue and LinkedBlockingQueue  Outline of what we'll do  Specify a Queue ADT as a Java interface  Show a difficult to implement array based implementation

18-4 Designing a Queue Interface  Queues typically provide these operations  add adds an element at the end of the queue  peek returns a reference to the element at the front of the queue  remove removes the element from the front of the queue and returns a reference to the front element  isEmpty returns false if there is at least one element in the queue

18-5 Specify an interface  We will use an interface to describe a queue ADT  The interface specifies method names, return types, the type of elements to add, and hopefully comments  interface OurQueue declares we must be able to add and remove any type of element  Collection class must have to make it a generic type

18-6 Interface to specify a FIFO Queue import java.util.NoSuchElementException; public interface OurQueue { // Return true if this queue has 0 elements public boolean isEmpty(); // Store a reference to any object at the end public void add(E newEl); // Return a reference to the object at the // front of this queue public E peek() throws NoSuchElementException; // Remove the reference to the element at the front public E remove() throws NoSuchElementException; }

18-7 Let SlowQueue implement the Queue interface  We need to store an Object[] an array of Object objects  avoids having queue of int and people and cars, and... public class SlowQueue implements OurQueue { private int back; private Object[] data ; //...  Now implement all methods of the OurQueue interface as they are written  plus a constructor with the proper name

18-8 Bad array type queue  Queue as an array could have  the front of the queue is always in data [0] public SlowQueue(int max) { data = new Object[max]; back = -1; } back == -1 null data[0] data[1] data[2] data[3] null So far so good. An empty queue

18-9 First version of add public void add(E element) { // This method will be changed later back++; data[back] = element; }  Send an add message aQueue.add("a"); So far so good. A queue of size 1 null data[0] data[1] data[2] data[3] null"a" back == 0

18-10 add another public void add(E element) { // This method will be changed later back++; data[back] = element; }  Send two more add messages aQueue.add("b"); aQueue.add("c"); So far so good. A Queue of size 3 "c" data[0] data[1] data[2] data[3] "b""a"null back == 2

18-11 Array Implementation of a Queue  During remove, slide all elements left if size were 999, then 998 assignments would be necessary "a""b""c" back "b""c" back Before remove After remove A poor remove algorithm null

18-12 Effect of queue operation using an array with a "floating" front add("a") add("b") add("c") remove() add("d") remove() "a""b""c"? frontback "a""b""c"? frontback "a""b""c""d" front back "a""b""c""d" frontback

18-13 "e" What happens next when back equals array.length? add("e") "a""b""c""d" front back  back indexes the last array index  However, this queue is not full  Where do you place "e"?  data[0] is available  What code would increment back correctly even when back is referencing the last available array index ? _______________________________ ?

18-14 The Circular Queue "c" "d" back front  A "circular queue" implementation uses wraparound The queue has "c" "d" "e"  either increase back by 1 or set back to 0 "a" "b" "e" data[0] add("e") now works in this "circular" queue. It reuses previously used array indexes

18-15 Implementing a Circular Queue  Still have to work with arrays, not circles  In order for the first and last indices to work in a circular manner:  increase by one element at a time  after largest index in the array, go to zero. back =  could contain code you just wrote on previous slide  But what is an empty queue?  What values should be given to front and back when the queue is constructed?

18-16 Problem: A full queue can not be distinguished from an empty queue backfront empty back front 1 in q a back front 3 in q a b c front full q a b c d back What does back == front imply? An empty or full queue? One option is to have the constructor place back one index before front then increment back during add

18-17 Corrected Circular Queue  Use this trick to distinguish between full and empty queues  The element referenced by front never indexes the front element— the “real” front is located at nextIndex(front) private int nextIndex(int index) { // Return an int to indicate next position return (index + 1) % data.length; }  For example, use this during peek() return data[nextIndex(front)];

18-18 Correct Circular Queue Implementation Illustrated front back empty backfront 1 in q "a" back front 2 in q "a" "b" back full q "a" "b" "c" front The front index is always 1 behind the actual front This wastes one array element but it's no big deal

18-19 Correct Circular remove Implementation Illustrated back empty q front back full q "a" "b" "c" front back 2 in q "b" "c" front back 1 in q "c" front remove three times to make this queue empty