CSE 373: Data Structures and Algorithms Lecture 2: Queues.

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

Stack & Queues COP 3502.
Building Java Programs Chapter 14
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.
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 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.
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,
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
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.
CE 221 Data Structures and Algorithms
Queue. Avoid confusion Britain Italy 6 Applications of Queues Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g.,
Queue. The Queue ADT Insertions and deletions follow the first-in first-out scheme Insertions are at the rear of the queue and removals are at the front.
CSE 373 Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
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.
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
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.
CSC 143 Queues [Chapter 7].
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
CSE 373 Data Structures Lecture 6
Lecture 2: Stacks and Queues
Generics, Stack, Queue Based on slides by Alyssa Harding
slides created by Marty Stepp
CSE 373 Data Structures Lecture 6
Stacks and Queues.
Presentation transcript:

CSE 373: Data Structures and Algorithms Lecture 2: Queues

Queue ADT 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 top element.

Queues in computer science 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

Queue idioms 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: Simple 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 ?

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

Circular Array Queue // 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; } bcdef Q: 0size - 1 front back

Exercise: Linked List Queue Implementation 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

Exercise: Linked List Queue Implementation (cont.) /** * 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(); }

Circular Array vs. Linked List Array:List:

Circular Array vs. Linked List Array: – May waste unneeded space or run out of space – Space per element excellent – Operations very simple / fast 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?

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