[TAP:PMUHE] Stack vs Queue

Similar documents
About This Lecture. Container Traversal. Container Traversal. Outline. Array Traversal. List Traversal inside the List class

Outline. iterator review iterator implementation the Java foreach statement testing

COMP 250. Lecture 32. interfaces. (Comparable, Iterable & Iterator) Nov. 22/23, 2017

COMP 250. Lecture 29. interfaces. Nov. 18, 2016

Java Comparable interface

Computer Science 62. Midterm Examination

Lecture 8: Iterators and More Mutation

Recursive Objects. Singly Linked List (Part 2)

CS231 - Spring 2017 Linked Lists. ArrayList is an implementation of List based on arrays. LinkedList is an implementation of List based on nodes.

2IP15 Programming Methods

CSC 172 Data Structures and Algorithms. Lecture #9 Spring 2018

Lists and Iterators. CSSE 221 Fundamentals of Software Development Honors Rose-Hulman Institute of Technology

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

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

Programming Languages and Techniques (CIS120)

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

CS 151. Exceptions & Javadoc. slides available on course website. Sunday, September 9, 12

What is an Iterator? An iterator is an abstract data type that allows us to iterate through the elements of a collection one by one

CS61BL Summer 2013 Midterm 2

CS 3 Introduction to Software Engineering. 5: Iterators

Abstract Data Types (ADTs) Example ADTs. Using an Abstract Data Type. Class #08: Linear Data Structures

ITI Introduction to Computing II

Doubly LinkedList is Symmetrical! LinkedList Efficiency. Monday, April 8, 13. insert insert remove remove remove walk

CS 310: Array-y vs Linky Lists

Introduction to Computer Science II (ITI 1121) Final Examination

Java Review: Objects

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

Announcements/Follow-ups

Collections and Iterators. Collections

The Java Collections Framework. Chapters 7.5

Lecture 4. The Java Collections Framework

CSCI 136 Data Structures & Advanced Programming. Lecture 22 Spring 2018 Profs Bill & Jon

Class 26: Linked Lists

Java Collection Framework

HEAPS & PRIORITY QUEUES

Java classes cannot extend multiple superclasses (unlike Python) but classes can implement multiple interfaces.

Programming Languages and Techniques (CIS120)

EXAMINATIONS 2012 Trimester 1, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

CISC 3115 TY3. C24a: Lists. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 11/15/2018 CUNY Brooklyn College

Programming Languages and Techniques (CIS120)

Announcements HEAPS & PRIORITY QUEUES. Abstract vs concrete data structures. Concrete data structures. Abstract data structures

CSC 1052 Algorithms & Data Structures II: Lists

Logistics. Half of the people will be in Wu & Chen The other half in DRLB A1 Will post past midterms tonight (expect similar types of questions)

CS 171: Introduction to Computer Science II. Linked List. Li Xiong

9/26/2018 Data Structure & Algorithm. Assignment04: 3 parts Quiz: recursion, insertionsort, trees Basic concept: Linked-List Priority queues Heaps

Abstract vs concrete data structures HEAPS AND PRIORITY QUEUES. Abstract vs concrete data structures. Concrete Data Types. Concrete data structures

COS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism

Computer Science E-119 Fall Problem Set 4. Due prior to lecture on Wednesday, November 28

Part 1: Excep-ons, part 2 Part 2: Iterator. Lecture 11 COMP 401, Spring /3/2015

Principles of Software Construction: Objects, Design, and Concurrency. Part 1: Design for reuse. Design patterns for reuse

CITS1001 week 4 Grouping objects lecture 2

Converting Collections to Arrays. A Bad Approach to Array Conversion. A Better Approach to Array Conversion. public Object[] toarray();

Implementing Hash and AVL

Basic Data Structures 1 / 24

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

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

COMP-202: Foundations of Programming. Lecture 13: Recursion Sandeep Manjanna, Summer 2015

Computer Science Foundation Exam. Dec. 19, 2003 COMPUTER SCIENCE I. Section I A. No Calculators! KEY

Software Construction

CS : Data Structures Michael Schatz. Sept Lecture 5: Iterators

Linked List Nodes (reminder)

(f) Given what we know about linked lists and arrays, when would we choose to use one data structure over the other?

חומר עזר לבחינה מבוא למדעי המחשב

JAVA COLLECTION FRAMEWORK & SETS

ITI Introduction to Computing II

Principles of Programming Languages

Programming Language Concepts: Lecture 6

CS61B Lecture #19. Last modified: Mon Oct 13 12:02: CS61B: Lecture #19 1

CS 314 Midterm 2 Spring 2013

CS32 Discussion Week 3

CS Introduction to Data Structures Week 4, 2017

Design Patterns. James Brucker

CS Week 14. Jim Williams, PhD

Basic Data Structures

COMP-202. Generics. COMP Generics, 2013 Jörg Kienzle and others

CSE 326 Team. Today s Outline. Course Information. Instructor: Steve Seitz. Winter Lecture 1. Introductions. Web page:

Programming Languages and Techniques (CIS120)

Question 1. (2 points) What is the difference between a stream and a file?

Recursion. Recursion [Bono] 1

Programming Languages and Techniques (CIS120)

Iterators and Sequences

Linked lists. Insert Delete Lookup Doubly-linked lists. Lecture 6: Linked Lists

CS18000: Problem Solving And Object-Oriented Programming

Programming II (CS300)

CMSC 202H. Containers and Iterators

COMP6700/2140 Abstract Data Types: Lists and Iteration

Introduction to Programming Using Java (98-388)

Lists, Stacks, and Queues

ITI Introduction to Computing II

Java Map and Set collections

Programming Languages and Techniques (CIS120)

CS18000: Programming I

CS61B Spring 2016 Guerrilla Section 2 Worksheet

CMSC 132: Object-Oriented Programming II

LINKED LISTS cs2420 Introduction to Algorithms and Data Structures Spring 2015

Lecture 16. Lecture

Topic 10: The Java Collections Framework (and Iterators)

About this exam review

Information Science 2

Test 6. moodle.yorku.ca CSE 1020

Transcription:

[TAP:PMUHE] Stack vs Queue A Singly Linked List can be used to implement which of the following A. List B. Stack C. Queue D. B and C E. Whatever 1

Administrative Details Midterm and lab scores (4 and 5) will be released on Wednesday Lab 6 is online No partners this week Review before lab; come to lab with design doc Check out the javadoc pages for the 3 provided classes 2

Today s Outline Iterators Iterator interface AbstractIterator abstract class (structure5) Aside: For-each and Iterable interface 3

Traversing a Structure numoccurs() counts the number of times a particular (non-null) Object appears in a List. public int numoccurs (List<E> data, E o) { int count = 0; for (int i = 0; i < data.size(); i++) { if (o.equals(data.get(i))) count++; return count;

Problems with our implemention generality get(i) not defined on some structures efficiency get(i) is slow on some structures

Goals We want a mechanism to traverse data in structures, such that: use same interface for generality data structure-specific implementation for efficiency

Iterator Iterator is a general purpose mechanism for efficiently traversing data (structures) An Iterator: Provides generic methods to dispense values Traversal of elements : Iteration Production of values : Generation Uses different implementations for each structure

Today s Outline Iterators Iterator interface AbstractIterator abstract class (structure5) Aside: For-each and Iterable interface 8

Iterator interface public interface Iterator<E> { boolean hasnext(); E next(); default void remove(); default void foreachremaining(consumer<? super E> action)

Recall: Fibonacci Numbers Definition F 1 = 1 F 2 = 1 F n = F n-1 + F n-2

A FibonacciNumbers Iterator An iterator for the first n Fibonacci numbers. public class FibonacciNumbers implements Iterator<Integer> { private int next= 1, current = 1; private int length= 10; // Default public FibonacciNumbers() { public FibonacciNumbers(int n) { length= n;

Why Is This Cool? (it is) We could calculate the i th Fibonacci number each time, but that would be slow Observation: to find the n th Fib number, we calculate the previous n-1 Fib numbers But by storing some state, we can easily generate the next Fib number in O(1) time Knowledge about the structure of the problem helps us traverse the Fib space efficiently one element at a time Let s do the same for data structures

Iterators for general structures Define an iterator class for the structures, e.g. public class VectorIterator<E> implements Iterator<E>; public class SinglyLinkedListIterator<E> implements Iterator<E>; Provide a method in the structure that returns an iterator public Iterator<E> iterator(){

Iterator Use : numoccurs public int numoccurs (List<E> data, E o) { int count = 0; for (int i = 0; i < data.size(); i++) { if (o.equals(data.get(i))) count++; return count;

Today s Outline Iterators Iterator interface AbstractIterator abstract class (structure5) Aside: For-each and Iterable interface 17

AbstractIterator structure5 defines AbstractIterator AbstractIterator partially implements Iterator interface adds two methods get() peek at (but don t take) next element, and reset() reinitialize iterator for reuse

Implementation : VectorIterator public class VectorIterator<E> extends AbstractIterator<E>{ In Vector.java: public Iterator<E> iterator() { return new VectorIterator<E>(this);

Implementation : SLLIterator public class SinglyLinkedListIterator<E> extends AbstractIterator<E> { protected Node<E> head, current; public SinglyLinkedListIterator(Node<E> head) { this.head = head; reset(); public void reset() { current = head; public E next() { E value = current.value(); current = current.next(); return value; public boolean hasnext() { return current!= null; public E get() { return current.value(); In SinglyLinkedList.java: public Iterator<E> iterator() { return new SinglyLinkedListIterator<E>(head);

Iterator Use : numoccurs AbstractIterator allows the use of get() and reset() (but requires a cast to AbstractIterator) public int numoccurs (List<E> data, E o, E o2) { int count = 0; int count2 = 0; AbstractIterator<E> i = (AbstractIterator<E>) data.iterator(); while(i.hasnext()){ if(o.equals(i.get())) count++; if(o2.equals(i.get())) count2++; i.next(); return count;

More Iterator Examples We can also make specialized iterators ReverseIterator.java SkipIterator.java

Today s Outline Iterators Iterator interface AbstractIterator abstract class (structure5) Aside: For-each and Iterable interface 25

The Iterable Interface For-each construct uses iterators. for( E elt : data ) {... is essentially the same as for(iterator<e> iter = data.iterator(); iter.hasnext();) { E elt = iter.next();... Thus, we can use the for-each if data implements the Iterable interface public interface Iterable<T> public Iterator<T> iterator();

General Rules for Iterators 1. Always call hasnext() before calling next() 2. In general, don t add to structure while iterating 3. Use remove() with caution