Problem Of The Day  Decipher the following phrase: STANDS 0 _ 2 3 4 5 6 7 8 9.

Slides:



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

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
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
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.
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:
©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 21: IndexList a/k/a Vector, ArrayList.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
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.
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.
1 Lecture 09 Iterators and Enumerations Reading for these lectures: Weiss, Section Iterator Interface. Much better is: ProgramLive, Section 12.3.
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.
CSC 212 – Data Structures Lecture 37: Course Review.
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.
2013-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Problem of the Day  At low tide, a ship docks in the harbor  Ship is 9’ above the water line when it docks  Over the side hangs a rope ladder w/ rungs.
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.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
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.
Iterators, Iterator, and Iterable 2015-T2 Lecture 8 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Thomas Kuehne.
CSC 212 – Data Structures Lecture 31: Last Word On Dictionaries.
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.
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.
©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 Example: LinkedStack LinkedStack UML Class Diagram LinkedStack Class LinkedStack Attributes/Constructor LinkedStack Methods LinkedStack iterator method.
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
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).
Lecture 15: More Iterators
ArraySet Methods and ArrayIterator
CSE 143 Lecture 27: Advanced List Implementation
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Example: LinkedSet<T>
CSE 143 Lecture 21 Advanced List Implementation
Presentation transcript:

Problem Of The Day  Decipher the following phrase: STANDS 0 _

Problem Of The Day  Decipher the following phrase: STANDS 0 _  I just knew that No one understands

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

 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 Using Iterator

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

 Maintain a cursor showing where they work  Value at cursor returned when next() called  Very implementation specific issues for cursor  To iterate over an I NDEX L IST, cursor must be index  Cursor is P OSITION for P OSITION L IST ’s I TERATOR How Iterators Work

 Maintain a cursor showing where they work  Value at cursor returned when next() called  Very implementation specific issues for cursor  To iterate over an I NDEX L IST, cursor must be index  Cursor is P OSITION for P OSITION L IST ’s I TERATOR How Iterators Work L IST

 Maintain a cursor showing where they work  Value at cursor returned when next() called  Very implementation specific issues for cursor  To iterate over an I NDEX L IST, cursor must be index  Cursor is P OSITION for P OSITION L IST ’s I TERATOR How Iterators Work Iterator Cursor  0 L IST

 Maintain a cursor showing where they work  Value at cursor returned when next() called  Very implementation specific issues for cursor  To iterate over an I NDEX L IST, cursor must be index  Cursor is P OSITION for P OSITION L IST ’s I TERATOR How Iterators Work L IST Iterator Cursor  0

 Maintain a cursor showing where they work  Value at cursor returned when next() called  Very implementation specific issues for cursor  To iterate over an I NDEX L IST, cursor must be index  Cursor is P OSITION for P OSITION L IST ’s I TERATOR How Iterators Work L IST Iterator Cursor  1

 Maintain a cursor showing where they work  Value at cursor returned when next() called  Very implementation specific issues for cursor  To iterate over an I NDEX L IST, cursor must be index  Cursor is P OSITION for P OSITION L IST ’s I TERATOR How Iterators Work L IST Iterator Cursor  2

 Maintain a cursor showing where they work  Value at cursor returned when next() called  Very implementation specific issues for cursor  To iterate over an I NDEX L IST, cursor must be index  Cursor is P OSITION for P OSITION L IST ’s I TERATOR How Iterators Work L IST Iterator Cursor  3

 Maintain a cursor showing where they work  Value at cursor returned when next() called  Very implementation specific issues for cursor  To iterate over an I NDEX L IST, cursor must be index  Cursor is P OSITION for P OSITION L IST ’s I TERATOR How Iterators Work L IST Iterator Cursor  ???

 Maintain a cursor showing where they work  Value at cursor returned when next() called  Very implementation specific issues for cursor  To iterate over an I NDEX L IST, cursor must be index  Cursor is P OSITION for P OSITION L IST ’s I TERATOR How Iterators Work L IST Iterator Cursor  5

Iterator for I NDEX L IST public class IndexListIterator { private IndexList theList; private int cursor; public IndexListIterator(IndexList list) { theList = list; cursor = 0; } public boolean hasNext() { return cursor != theList.size(); } // More goes here…

Limit of Iterator  Defines limited set of methods  Cannot add or change elements in Collection  Changes to data elsewhere invalidates 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

P OSITION with Benefits  cursor is next Position in PositionList  Needing to know class specifics avoided  Rely on P OSITION L IST ’s methods instead head tail elements Iterator Cursor

P OSITION with Benefits  cursor is next Position in PositionList  Needing to know class specifics avoided  Rely on P OSITION L IST ’s methods instead head tail elements Iterator Cursor

P OSITION with Benefits  cursor is next Position in PositionList  Needing to know class specifics avoided  Rely on P OSITION L IST ’s methods instead head tail elements Iterator Cursor

P OSITION with Benefits  cursor is next Position in PositionList  Needing to know class specifics avoided  Rely on P OSITION L IST ’s methods instead head tail elements Iterator Cursor

P OSITION with Benefits  cursor is next Position in PositionList  Needing to know class specifics avoided  Rely on P OSITION L IST ’s methods instead head tail elements Iterator Cursor

Iterator for P OSIION L IST private PositionList theList; private Position cursor; public boolean hasNext() { return cursor != null; } public E next() throws NoSuchElementException { if (cursor == null) { throw new NoSuchElementException(); } E retVal = cursor.element(); if (cursor != theList.last()) { cursor = theList.next(cursor); } else { cursor = null; } return retVal; }

Why Should You Care?

Iterable Interface  So simple makes Iterator look complex

Iterable Interface  So simple makes Iterator look complex  Get ready for this – it is really big!

Iterable Interface  So simple makes Iterator look complex  Get ready for this – it is really big!  Rocks your world and makes code easy to write

Iterable Interface  So simple makes Iterator look complex  Get ready for this – it is really big!  Rocks your world and makes code easy to write  Java’s prettiest feature relies on this interface

Iterable Interface  So simple makes Iterator look complex  Get ready for this – it is really big!  Rocks your world and makes code easy to write  Java’s prettiest feature relies on this interface

Iterable Interface  So simple makes Iterator look complex  Get ready for this – it is really big!  Rocks your world and makes code easy to write  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) { … }

Integer findLargest(Iterable able) { Integer retVal = Integer.MIN_VALUE; for (Integer datum : able) { if (datum > retVal) { retVal = datum; } } return retVal; }  able could be L IST, H ASH M AP, V ERTEX S ET … For-Each Rocks The Hizzy

Your Turn  Get into your groups and complete activity

About the Grading

For Next Lecture  Read GT 6.4 before Wednesday’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 #10 assignment available on Angel  Programming Assignment #2  Programming Assignment #2 due in 12 days