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.
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
Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part.
Written by: Dr. JJ Shepherd
CSC 212 – Data Structures. Using Stack Stack Limitations  Great for Pez dispensers, JVMs,& methods  All of these use most recent item added only 
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.
Bag implementation Add(T item) – Enlarge bag if necessary; allocate larger array Remove(T item) – Reduce bag if necessary; allocate smaller array Iterator.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
CSC 212 – Data Structures Lecture 13: Linked Lists.
Iterators CS 367 – Introduction to Data Structures.
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.
COMP 103 Iterators and Iterable. RECAP  Maps and Queues TODAY  Queue Methods  Iterator and Iterable 2 RECAP-TODAY.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
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 _
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.
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.
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.
CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
CSC 212 – Data Structures Lecture 37: Course Review.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
LECTURE 24: STACK ADTS CSC 212 – Data Structures.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
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.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 13 Implementing.
2013-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.
Question of the Day  Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.
Lecture Objectives  Linked list data structures:  Singly-linked (cont.)  Doubly-linked  Circular  Implementing the List interface as a linked list.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 8 Lists, Iterators, and Doubly Linked Lists.
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.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
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.
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.
Iterators ITI 1121 N. El Kadri. Motivation Given a (singly) linked-list implementation of the interface List, defined as follows, public interface List.
Iterators, Iterator, and Iterable 2015-T2 Lecture 8 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Thomas Kuehne.
Lecture 8: Advanced OOP Part 2. Overview Review of Subtypes Interfaces Packages Sorting.
Question of the Day  What three letter word completes the first word and starts the second one: DON???CAR.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
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.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Topic 13 Iterators. 9-2 Motivation We often want to access every item in a data structure or collection in turn We call this traversing or iterating over.
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.
Problem of the Day  Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)
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.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Iterators. Iterator  An iterator is any object that allows one to step through each element in a list (or, more generally, some collection).
EECE 310: Software Engineering
Lecture 15: More Iterators
Lecture 23: Doubly Linked List
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
Dynamic Data Structures and Generics
CSE 143 Lecture 21 Advanced List Implementation
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 after an existing one  Collection can remove any element it contains  Loop over all elements without removing them  List ADTs differ in how they provide access  ArrayList’s indices give quick access to specific position  Good at working at relative location with LinkedList

List ADT

Iterators  Provides data access without knowing more  As an ADT, methods independent of data storage  Access elements 1-by-1 by calling next() method  Like Comparator ; does not store data it uses  Iterator class specific to instance holding data  But means that code using Iterator independent  Why is this important?

Iterators  Provides data access without knowing more  As an ADT, methods independent of data storage  Access elements 1-by-1 by calling next() method  Like Comparator ; does not store data it uses  Iterator class specific to instance holding data  But means that code using Iterator independent  Why is this important?  Breaks up code into separate class for each function  Each part replaceable when better approach found

Perfect Example of Perfect Design

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

Iterator Needs a 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

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  Moves forward item-by-item only, cannot jump around

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  Moves forward item-by-item only, cannot jump around

Limits to Iterator  Iterator ’s methods are very, very limited  Iterator instance has no data; limits possible uses  While iterating, can be tempting to modify data  Requires actual instance, I TERATOR does not store data  If underlying instance changes, results undefined  May work, may crash, or may do both; caveat emptor

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

Iterable Usage  Occasionally use Iterator ; Iterable preferable  Many ADT definitions include Iterable in definition  Makes it possible to interchange ADT when processing  Iterable support built-in to Java language  Like Comparable, interface in java.lang package  For-each loops through Iterable data directly  Translates code to use Iterator (without extra code)

For-Each for the Win  Slightly different loop than normal for loop for (type variableName : IterableVar) List idx; OrderedList profFirst; ArrayList whyNot; // Lots of code is here including assignments to our lists for (Integer i : idx ) { … } for (Costume cost : profFirst) { … } for (Scanner stupid : whyNot) { … }

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  A RRAY L IST & L INKED L IST are examples; many will follow

What Type Should Cursor Be?

Implementing Iterator  Very implementation specific issues for cursor  To iterate over an A RRAY L IST, cursor is index  Cursor is node for L INKED 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

What Type Should Cursor Be?

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 Iterator (Start) public class ALIterator { private ArrayList theList; private int cursor; public ALIterator(ArrayList list) { theList = list; cursor = 0; } } public class LLIterator { private LinkedList theList; private DNode cursor; public LLIterator(LinkedList list) { theList = list; cursor = } }

Implementing Iterator (Start) public class ALIterator { private ArrayList theList; private int cursor; public ALIterator(ArrayList list) { theList = list; cursor = 0; } } public class LLIterator { private LinkedList theList; private DNode cursor; public LLIterator(LinkedList list) { theList = list; cursor = } }

Implementing the Iterator  Iterator class needs access to List’s internal fields  Implementationless ADT concept must be violated  Code specific to implementation and not reusable  All CSC212 design concepts get thrown out window

Implementing the Iterator  Iterator class needs access to List’s internal fields  Implementationless ADT concept must be violated  Code specific to implementation and not reusable  All CSC212 design concepts get thrown out window

Implementing for ArrayList L IST

Implementing for ArrayList L IST ALIterator cursor  0 theList

Implementing for ArrayList L IST ALIterator cursor  1 theList

Implementing for ArrayList L IST ALIterator cursor  2 theList

L IST Implementing for ArrayList ALIterator cursor  3 theList

L IST Implementing for ArrayList ALIterator

L IST Implementing for ArrayList ALIterator cursor  4 theList

Implementing for LinkedList  cursor is next node in LinkedList  Need to know class specifics to use node types  No methods in LinkedList provide required info head tail LLIterator cursor theList

Implementing for LinkedList  cursor is next node in LinkedList  Need to know class specifics to use node types  No methods in LinkedList provide required info head tail LLIterator cursor theList

Implementing for LinkedList  cursor is next node in LinkedList  Need to know class specifics to use node types  No methods in LinkedList provide required info head tail LLIterator cursor theList

Implementing for LinkedList  cursor is next node in LinkedList  Need to know class specifics to use node types  No methods in LinkedList provide required info head tail LLIterator cursor theList

Implementing for LinkedList  cursor is next node in LinkedList  Need to know class specifics to use node types  No methods in LinkedList provide required info head tail LLIterator cursor theList

Comparing Implementations ArrayList Iterator LinkedList Iterator  Check if cursor valid: cursor != theList.size ()  Get element at cursor : theList.get ( cursor ) or array[cursor]  Advancing cursor : cursor += 1  Check if cursor valid: cursor != null  Get element at cursor : cursor.getElement ()  Advancing cursor : cursor = cursor. getNext ()

Your Turn  Get into your groups and complete activity

For Next Lecture  Read 8.1 – 8.4 before Friday’s lecture  What is recursion & how does it work?  What happens to locals if method called again?  Why did we spend so much time on tracing methods?  Week #11 assignment available on Angel  No class on Monday  No class on Monday; I will be out of town  As a result, I am delaying quiz until Wednesday