Problem of the Day  Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)

Slides:



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

Chapter 23 Organizing list implementations. This chapter discusses n The notion of an iterator. n The standard Java library interface Collection, and.
Linked Lists Linear collections.
AITI Lecture 19 Linked List Adapted from MIT Course 1.00 Spring 2003 Lecture 26 and Tutorial Note 9 (Teachers: Please do not erase the above note)
COSC 1P03 Data Structures and Abstraction 10.1 The List If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping.
Double-Linked Lists and Circular Lists
The Singleton Pattern II Recursive Linked Structures.
CSC 212 – Data Structures Lecture 22: PositionList.
CSC 212 – Data Structures. Using Stack Stack Limitations  Great for Pez dispensers, JVMs,& methods  All of these use most recent item added only 
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
CSC 212 – Data Structures.  Fri., Dec. 17 th from 8AM – 10AM in OM 200  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP 
Problem of the Day  What do you get when you cross a mountain climber and a grape?
LECTURE 38: ORDERED DICTIONARY CSC 212 – Data Structures.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Iterators Chapter 7. Chapter Contents What is an Iterator? A Basic Iterator Visits every item in a collection Knows if it has visited all items Doesn’t.
Cmput Lecture 15 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from the book:
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
CS2110 Recitation 07. Interfaces Iterator and Iterable. Nested, Inner, and static classes We work often with a class C (say) that implements a bag: unordered.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Problem Of The Day  Decipher the following phrase: STANDS 0 _
CSC 213 – Large Scale Programming. Why Do We Test?
Problem of the Day  Rich old man tells his 2 children he will hold a race to decide who gets his fortune. SLOWEST  Winner is one who owns SLOWEST horse.
Humorous Asides “A journey begins with single step”
Problem of the Day  What do you get when you cross a mountain climber and a grape?
LECTURE 37: ORDERED DICTIONARY CSC 212 – Data Structures.
LECTURE 36: DICTIONARY CSC 212 – Data Structures.
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.
LECTURE 26: QUEUES CSC 212 – Data Structures. Using Stack.
CSC 212 – Data Structures Lecture 37: Course Review.
LECTURE 24: STACK ADTS CSC 212 – Data Structures.
CSC 212 – Data Structures.  Fri., Dec. 14 th from 2:45 – 4:45PM in SH1028  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP.
2014-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
CSC 212 – Data Structures Lecture 17: Stacks. Question of the Day Move one matchstick to produce a square.
2013-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
18-1 Queues Data Structures and Design with Java and JUnit © Rick Mercer.
CSC 212 – Data Structures Lecture 26: Hash Tables.
Problem of the Day  Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)
Lecture Objectives  Linked list data structures:  Singly-linked (cont.)  Doubly-linked  Circular  Implementing the List interface as a linked list.
Problem Of The Day  Two missiles speed directly toward each other  One goes 9,000 miles per hour  Other goes 21,000 miles per hour.  If they start.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 8 Lists, Iterators, and Doubly Linked Lists.
CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18.
CSC 212 – Data Structures Lecture 23: Iterators. Question of the Day Thieves guild states it will sell to members: lock picking kits  $0.67 each 40’
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.
CSC 212 Sequences & Iterators. Announcements Midterm in one week  Will cover through chapter 5 of book  Midterm will be open book, open note (but, closed.
CS 367 Introduction to Data Structures Lecture 5.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2.
What is Iterator Category: Behavioral Generic Way to Traverse Collection Not Related to Collection Implementation Details Not Related to the direction/fashion.
Iterators ITI 1121 N. El Kadri. Motivation Given a (singly) linked-list implementation of the interface List, defined as follows, public interface List.
Lecture 8: Advanced OOP Part 2. Overview Review of Subtypes Interfaces Packages Sorting.
CSC 212 – Data Structures Lecture 31: Last Word On Dictionaries.
Iteration Abstraction SWE Software Construction Fall 2009.
Question of the Day  What three letter word completes the first word and starts the second one: DON???CAR.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
2015-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 9 Doubly Linked Lists and Ordered Lists Lecture.
CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost.
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
Recursive Objects Singly Linked List (Part 2) 1. Operations at the head of the list  operations at the head of the list require special handling because.
Problem of the Day  On the next slide I wrote today’s problem of the day. It has 3 possible answers. Can you guess which 1 of the following is the solution?
CSC 212 – Data Structures Lecture 28: More Hash and Dictionaries.
Iterators. Iterator  An iterator is any object that allows one to step through each element in a list (or, more generally, some collection).
Objectives After studying this chapter you should understand the following: the role of iterators in container classes; various implementations of the.
EECE 310: Software Engineering
Lecture 15: More Iterators
Conditional Statements
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Web Design & Development Lecture 6
Presentation transcript:

Problem of the Day  Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)

Problem of the Day  Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z) (x - a) * (x - b) * (x - c) * …*(x – x)*…*(x - z)

Problem of the Day  Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z) (x - a) * (x - b) * (x - c) * …*(x – x)*…*(x - z) (x - a) * (x - b) * (x - c) * …* 0 *…*(x - z) = 0

CSC 212 – Data Structures

List ADT  Collection which we can access all elements  Add element before an existing one  Return the Collection’s 3 rd element  Loop over all elements without removing them  L IST ADTs differ in how they provide access  I NDEX L IST uses indices for absolution positioning  Can only use relative positions in N ODE L IST

List ADT

Oops…

Iterators  Scans elements in a Collection  Initial use will return first element…  …then second element returned with next call…  …returns the third element next…  …and so on until it moves past the last element  Iterator instance returned by another ADT  Process data without hard-coding ADT into method any  Makes it easy to write code using any C OLLECTION

Iterators  Scans elements in a Collection  Initial use will return first element…  …then second element returned with next call…  …returns the third element next…  …and so on until it moves past the last element  Iterator instance returned by another ADT  Process data without hard-coding ADT into method any  Makes it easy to write code using any C OLLECTION

Using Iterator  Write loops using an Iterator  Iterator can be used to get data from anything  Combine structures’ elements using Iterator  Improves modularity  Classes work with anything providing an Iterator  Improves reuse  Ignore details of how to access elements within ADT  Very simple code leaves hard part to Iterator

Iterator Interface package java.util; public interface Iterator { boolean hasNext(); E next() throws NoSuchElementException; void remove() throws UnsupportedOperationException; }

Iterator Needs a Cursor

Implementing Iterator (Start) public class ILIterator { private IndexList theList; private int cursor; public ILIterator(IndexList list) { theList = list; cursor = 0; } } public class PLIterator { private PositionList theList; private Position cursor; public PLIterator(PositionList list) { theList = list; cursor = list.first(); // TODO: handle empty list correctly } }

Limits to Iterator  I TERATOR ’s cursor differs from cursor in Word. PPT,  Cannot add or modify data; (mostly) used to read data  Instance goes through data once, cursor only advances  Only moves forward item-by-item, cannot jump around  While iterating, can be tempting to modify data  Easy to do, as object being iterated over is not I TERATOR  But result undefined, if data changes, I TERATOR can crash

Implementing Iterator  Very implementation specific issues for cursor  To iterate over an I NDEX L IST, cursor is index  Cursor is P OSITION for P OSITION L IST ’s I TERATOR  Iterator’s methods always use similar algorithm  General outline identical, since interface defines task  But implementation changes since cursor use differs

Implementing Iterator

Same Algorithm, But... Algorithm next() if hasNext() then E retVal = value at cursor’s location Advance cursor to refer to next location return retVal else throw new NoSuchElementException endif end

Same Algorithm, But... Algorithm next() if hasNext() then E retVal = value at cursor’s location Advance cursor to refer to next location return retVal else throw new NoSuchElementException endif end

Implementing for IndexList L IST

Implementing for IndexList ILIterator cursor  0 theList L IST

Implementing for IndexList L IST ILIterator cursor  0 theList

Implementing for IndexList L IST ILIterator cursor  1 theList

Implementing for IndexList L IST ILIterator cursor  2 theList

Implementing for IndexList L IST ILIterator cursor  3 theList

Implementing for IndexList L IST ILIterator cursor  ? theList

Implementing for IndexList L IST ILIterator cursor  5 theList

Implementing for PositionList  cursor is next Position in PositionList  Needing to know class specifics avoided  Rely on P OSITION L IST ’s methods instead head tail PLIterator cursor theList

Implementing for PositionList  cursor is next Position in PositionList  Needing to know class specifics avoided  Rely on P OSITION L IST ’s methods instead head tail PLIterator cursor theList

Implementing for PositionList  cursor is next Position in PositionList  Needing to know class specifics avoided  Rely on P OSITION L IST ’s methods instead head tail PLIterator cursor theList

Implementing for PositionList  cursor is next Position in PositionList  Needing to know class specifics avoided  Rely on P OSITION L IST ’s methods instead head tail PLIterator cursor theList

Implementing for PositionList  cursor is next Position in PositionList  Needing to know class specifics avoided  Rely on P OSITION L IST ’s methods instead head tail PLIterator  cursor theList

Comparing Implementations IndexList Iterator PositionList Iterator  Check if cursor valid: cursor != theList.size ()  Get element at cursor : theList.get ( cursor )  Advancing cursor : cursor += 1  Check if cursor valid: cursor != null  Get element at cursor : cursor.element ()  Advancing cursor : cursor = theList.next ( cursor )

Limit of Iterator  Interface provides remove(), but…  …implementing it is a royal pain in the  Support not required by the interface  Instead throw UnsupportedOperationException  Relying on remove() risky, since it is optional  When in doubt, skip it

Why Should You Care?

Iterable Interface  So simple makes Iterator look complex  Java’s prettiest feature relies on this interface

Iterable Interface  So simple makes Iterator look complex  Java’s prettiest feature relies on this interface

Iterable Interface  So simple makes Iterator look complex  Java’s prettiest feature relies on this interface package java.lang; public interface Iterable { public Iterator iterator(); }

For-each for the Win  Iterable support built-in to Java for free  As is any class in the java.lang package  For-each loops work with any Iterable class  Uses Iterator to loop over each element in class  Slightly different loop than normal for loop for (Type variableName : IterableName) IndexList idx = …; for (Integer i : idx) { … }

For-Each Rocks The Hizzy Integer findSum(Iterable able) { Integer retVal = 0; for (Integer datum : able) { retVal += datum; } return retVal; }  able could be (almost) ANY C OLLECTION class  I NDEX L IST & P OSITION L IST first examples; many follow

Your Turn  Get into your groups and complete activity

For Next Lecture  Read GT 6.4 before Monday’s lecture  What if we want indices & P OSITION s?  Can we handle power to switch between the two?  What implementation issues do S EQUENCE s cause?  Week #11 assignment available on Angel  Programming Assignment #2  Programming Assignment #2 due in 7 days