CSE 373 Data Structures and Algorithms Lecture 2: Queues.

Slides:



Advertisements
Similar presentations
Building Java Programs Chapter 14
Advertisements

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.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
CSC 212 – Data Structures. Using Stack Stack Limitations  Great for Pez dispensers, JVMs,& methods  All of these use most recent item added only 
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. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Queues.
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.
TCSS 342, Winter 2005 Lecture Notes
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
CSE 143 Lecture 7 Stacks and Queues reading: Stuart Reges notes on website slides created by Marty Stepp
Building Java Programs
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.
CSE 143 Lecture 5 Stacks and Queues slides created by Marty Stepp
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,
09-1 Queues and List-Based ADT Implementations Problem Set: PS3 due Wednesday, March 7 Wellesley College CS230 Lecture 09 Monday, February 26 Handout #18.
Queues Chapter 6. Chapter 6: Queues Chapter Objectives To learn how to represent a waiting line (queue) and how to use the five methods in the Queue interface:
1 Chapter 20 Lists, Stacks, Queues Lecture 7 Dr. Musab Zghoul برمجة هيكلية.
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
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.
18-1 Queues Data Structures and Design with Java and JUnit © Rick Mercer.
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.
CE 221 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Queue. Avoid confusion Britain Italy 6 Applications of Queues Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g.,
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
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.
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
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.
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
Queues1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich,
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.
Building Java Programs
Stacks and Queues.
Building Java Programs
CMSC 341 Lecture 5 Stacks, Queues
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Building Java Programs Chapter 14
Queues 11/16/2018 4:18 AM Queues 11/16/2018 4:18 AM Queues.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Queue.
Stacks and Queues.
slides created by Marty Stepp
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Building Java Programs
Building Java Programs
Building Java Programs Chapter 14
Queues 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
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
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Queues Jyh-Shing Roger Jang (張智星)
Lecture 2: Stacks and Queues
Generics, Stack, Queue Based on slides by Alyssa Harding
slides created by Marty Stepp
Stacks and Queues.
Data Structures & Programming
Presentation transcript:

CSE 373 Data Structures and Algorithms Lecture 2: Queues

Queue ADT 2  queue: A list with the restriction that insertions are done at one end and deletions are done at the other  First-In, First-Out ("FIFO”)  Elements are stored in order of insertion but don't have indexes.  Client can only add to the end of the queue, and can only examine/remove the front of the queue.  basic queue operations:  add (enqueue): Add an element to the back.  remove (dequeue): Remove the front element.  peek: Examine the element at the front.

Queues in computer science 3  Operating systems:  queue of print jobs to send to the printer  queue of programs / processes to be run  queue of network data packets to send  Programming:  modeling a line of customers or clients  storing a queue of computations to be performed in order  Real world examples:  people on an escalator or waiting in a line  cars at a gas station (or on an assembly line)

Using Queue s Queue q = new LinkedList (); q.add(42); q.add(-3); q.add(17); // front [42, -3, 17] back System.out.println(q.remove()); // 42  IMPORTANT: When constructing a queue you must use a new LinkedList object instead of a new Queue object. add( value ) places given value at back of queue remove() removes value from front of queue and returns it; throws a NoSuchElementException if queue is empty peek() returns front value from queue without removing it; returns null if queue is empty size() returns number of elements in queue isEmpty() returns true if queue has no elements 4

Queue idioms 5  As with stacks, must pull contents out of queue to view them. while (!q.isEmpty()) { do something with q.remove(); }  another idiom: Examining each element exactly once. int size = q.size(); for (int i = 0; i < size; i++) { do something with q.remove(); (including possibly re-adding it to the queue) }  Why do we need the size variable

Implementing Queue ADT: Array Queue  Keep track of the number of elements in the queue, size.  Enqueue at the back of the array ( size ).  Dequeue at the front of the array (index 0).  what is bad about this implementation?  what if we enqueue at 0 and dequeue at size ? 6

Implementing Queue ADT: Circular Array Queue  Neat trick: use a circular array to insert and remove items from a queue in constant time.  The idea of a circular array is that the end of the array “wraps around” to the start of the array bcdef Q: 0size - 1 front back 7

Circular Array Queue 8 bcdef Q: 0size - 1 front back // Basic idea only! enqueue(x) { Q[back] = x; back = (back + 1) % size } // Basic idea only! dequeue() { x = Q[front]; front = (front + 1) % size; return x; }

Linked List Queue bcdef frontback // Basic idea only! enqueue(x) { back.next = new Node(x); back = back.next; } // Basic idea only! dequeue() { x = front.item; front = front.next; return x; } 9

Queue: Circular Array vs. Linked List 10  Circular Array  May waste unneeded space or run out of space  Space per element excellent  Operations very simple / fast  Linked List  Always just enough space  But more space per element  Operations very simple / fast  If we wanted add the ability to access the kth element to our queue, could both implementations support this?

Exercise: Linked List Queue Implementation 11  Implement a queue class that stores String values using a singly linked list with both nodes to indicate the front and the back of the queue as below. The queue should implement the interface on the next slide. bcdef frontback

String Queue Interface 12 /** * Interface for a queue of Strings. */ public interface StrQueue { /** * Tests if the queue is empty. */ public boolean isEmpty(); /** * Inserts an element at the end of the queue. */ public void enqueue(String str); /** * Deletes and returns the element at the front of the queue. the deleted value; throws NoSuchElementException if empty */ public String dequeue(); }

Generic Queue Interface 13 /** * Interface for a queue. */ public interface Queue { /** * Tests if the queue is empty. */ public boolean isEmpty(); /** * Inserts an element at the end of the queue. */ public void enqueue(E e); /** * Deletes and returns the element at the front of the queue. the deleted value; throws NoSuchElementException if empty */ public E dequeue(); }