Queues. Stacks and Queues

Similar documents
CSE 143. Lecture 4: Stacks and Queues

CSE 373 Data Structures and Algorithms. Lecture 2: Queues

Building Java Programs

Stacks and Queues. Introduction to abstract data types (ADTs) Stack ADT. Queue ADT. Time permitting: additional Comparator example

CSE 373. Java Collection Framework. reading: Weiss Ch. 3, 4.8. slides created by Marty Stepp

Data Abstraction and Specification of ADTs

Adam Blank Lecture 5 Winter 2015 CSE 143. Computer Programming II

Advanced Java Concepts Unit 3: Stacks and Queues

Basic Data Structures

Project 2 (Deques and Randomized Queues)

Basic Data Structures 1 / 24

csci 210: Data Structures Stacks and Queues

Queues Fall 2018 Margaret Reid-Miller

CMSC 132: Object-Oriented Programming II. Stack and Queue

Stacks and Queues. David Greenstein Monta Vista

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

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

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

More Data Structures (Part 1) Stacks

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

Stacks and Queues. Chapter Stacks

Queues and Unit Testing

CSE 143 Sample Midterm Exam #1

Stacks and Queues

Name Section Number. CS210 Exam #2 *** PLEASE TURN OFF ALL CELL PHONES*** Practice

Stacks. Stacks. Main stack operations. The ADT Stack stores arbitrary objects. Insertions and deletions follow the last-in first-out (LIFO) principle.

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12

Plan for Week. What is a linear structure? Stacks Queues and Deques, Oh My!

Interfaces & Generics

CSE 143 SAMPLE MIDTERM SOLUTION

Plan for Week. Linear structures: Stack, Queue. Friday: More examples of recursion and alternatives. Guide to using practice-it for APT-like problems

infix expressions (review)

ITI Introduction to Computing II

CSE 143 Lecture 26. Advanced collection classes. (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, ,

CMSC 132, Object-Oriented Programming II Summer Lecture 9:

[2:3] Linked Lists, Stacks, Queues

Lecture 2: Stacks and Queues

CSC 321: Data Structures. Fall 2012

CS350: Data Structures Stacks

1. Introduction. Lecture Content

Keeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10

Stacks and Queues. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG

CS61B Spring 2016 Guerrilla Section 2 Worksheet

Since it is an interface, we need a concrete class during its declaration. There are many ways to initialize a Queue object, most common being-

Queue rear tail front head FIFO

INFO1x05 Tutorial 6. Exercise 1: Heaps and Priority Queues

1/22/13. Queue? CS 200 Algorithms and Data Structures. Implementing Queue Comparison implementations. Photo by David Jump 9. Colorado State University

IT 4043 Data Structures and Algorithms. Budditha Hettige Department of Computer Science

1/22/13. Queue. Create an empty queue Determine whether a queue is empty Add a new item to the queue

CMSC 341. Linked Lists, Stacks and Queues. CMSC 341 Lists, Stacks &Queues 1

CSE 143 SAMPLE MIDTERM

CSE 143, Winter 2009 Sample Midterm Exam #2

Queue? Part 4. Queues. Photo by David Jump. Create an empty queue Items leave a queue from its front.

Examination Questions Midterm 1

Algorithms. Algorithms 1.3 STACKS AND QUEUES. stacks resizing arrays queues generics iterators applications ROBERT SEDGEWICK KEVIN WAYNE.

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

LIFO : Last In First Out

COL106: Data Structures and Algorithms. Ragesh Jaiswal, IIT Delhi

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

BlockingArrayQueue.java. Page 1

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Lecture 3: Queues, Testing, and Working with Code

Computer Science 9608 (Notes) Chapter: 4.1 Computational thinking and problem-solving

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue

CSC 321: Data Structures. Fall 2013

C Sc 127B Practice Test 2 Section Leader Your Name 100pts

Announcements. CS18000: Problem Solving And Object-Oriented Programming

CS24 Week 4 Lecture 2

final int a = 10; for(int i=0;i<a;i+=2) { for(int j=i;j<a;j++) { System.out.print("*"); } System.out.println(); }

STACKS AND QUEUES. Problem Solving with Computers-II

Lecture 2: Implementing ADTs

Points off Total off Net Score. CS 314 Final Exam Spring 2016

COMP 213 Advanced Object-oriented Programming Lecture 8 The Queue ADT (cont.)

CT 229 Object-Oriented Programming Continued

Lecture 3: Stacks & Queues

Stacks (5.1) Abstract Data Types (ADTs) CSE 2011 Winter 2011

CSEN 301 Data Structures and Algorithms

Stacks Fall 2018 Margaret Reid-Miller

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

Stacks and Queues III

CSE 2123: Collections: Priority Queues. Jeremy Morris

Queues. Data structures that wait their turn. queues 1

CS 61B Spring 2017 Guerrilla Section 3 Worksheet. 25 February 2017

== isn t always equal?

EXAMINATIONS 2016 TRIMESTER 2

CSE 143 Sample Midterm Exam #4

Please note that if you write the mid term in pencil, you will not be allowed to submit a remark request.

Stacks and Queues. CSE 373 Data Structures Lecture 6

tree nonlinear Examples

Chapter 2. Stack & Queues. M hiwa ahmad aziz

CSE 143 Lecture 14. Interfaces; Abstract Data Types (ADTs) reading: 9.5, 11.1; 16.4

All the above operations should be preferably implemented in O(1) time. End of the Queue. Front of the Queue. End Enqueue

CSE 143 Section Handout #13 Practice Midterm #5

The Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified.

Lecture 6. COMP1006/1406 (the OOP course) Summer M. Jason Hinek Carleton University

Programming II (CS300)

Queues. CITS2200 Data Structures and Algorithms. Topic 5

CSC 1052 Algorithms & Data Structures II: Queues

LECTURE OBJECTIVES 6-2

CSE 373 SEPTEMBER 29 STACKS AND QUEUES

Transcription:

Queues Reading: RS Chapter 14 Slides are modified from those provided by Marty Stepp www.buildingjavaprograms.com 1 Stacks and Queues Sometimes a less powerful, but highly optimized collection is useful. Two optimized collections: Stack: retrieves elements in the reverse of the order they were added. Queue: retrieves elements in the same order they were added. queue stack 1

Abstract Data Types (ADTs) Abstract Data Type (ADT): specification of a collection or data and the operations that can be performed on the data Describes what a collection does Not how the collection does it As a client, we don t need to know the internal workings We just need to understand the idea of the collection and what operations it can perform. Understanding the runtime efficiency is also important for choosing a collection Queues Queue: retrieves elements in the order they were added. 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. 2

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) Programming with Queues 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<Integer> q = new LinkedList<Integer>(); 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. Why? 3

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? Exercise What is the output produced when the following method is passed various queues: public static void mystery3(queue<integer> q) { int size = q.size(); for (int i = 0; i < size; i++) { int n = q.remove(); if (n > 0) { a.add(-n); System.out.println(q); 4

Mixing stacks and queues We often mix stacks and queues to achieve certain effects. Example: Reverse the order of the elements of a queue. Queue<Integer> q = new LinkedList<Integer>(); q.add(1); q.add(2); q.add(3); // [1, 2, 3] Stack<Integer> s = new Stack<Integer>(); while (!q.isempty()) { // Q -> S s.push(q.remove()); while (!s.isempty()) { // S -> Q q.add(s.pop()); System.out.println(q); // [3, 2, 1] Delegation Example - Queue Consider java.util.queue How many methods does the Queue interface have? Queue defines 6 methods Queue inherits 18 methods from Collection Queue inherits 1 method from Iterable Queue inherits 7 methods from Object What is the main functionality of a Queue? Some of the methods in java.util.queue allow for functionality that a pure Queue wouldn t allow Remove an arbitrary element 5

More About java.lang.queue Provides insertion, extraction, and inspection operations One set throws exceptions if precondition isn t met One set returns a special value if cannot meet post condition Throws Exception Returns Special Value Insert add(e) offer(e) Remove remove() poll() Examine element() peek() 11 A Pure Queue Why would we want a pure Queue? One that only allows for add(), remove(), peek(), and isempty() How could we get a pure Queue? Implement Queue and override methods we don t want with empty methods / throw exception Create our own Queue Use delegation (via an association relationship) Write a class that has an instance of the Queue (as LinkedList) Provide the interface you want, and then pass those tasks on to your Queue instance 6

Delegation Example public class MyQueue<E> { private java.util.queue<e> q; public MyQueue() { q= new LinkedList<E>(); public void add(e e) { q.add(q.size()-1, e); public E remove() { return q.remove(0, e); public E peek() { return q.get(0); public boolean isempty() { return q.isempty(); MyQueue push(e: E): void pop(): E peek(): E isempty(): boolean java.util.queue<e> Exercise From Sedgewick s and Wayne s Introduction to Programming in Java In the Josephus problem from antiquity, N people are in dire straits and agree to the following strategy to reduce the population. They arrange themselves in a circle (as positions number from 0 to N-1) and proceed around the circle, eliminating every Mth person until only one person is left. Legend has it that Josephus figured out where to sit to avoid being eliminated. Write a Queue client Josephus that takes N and M from the command line and prints out the order in which people are eliminated (and thus would show Josephus where to sit in the circle). % java Josephus 7 2 1 3 5 0 4 2 6 7