Need for Iterators O(n2) this is a common application

Slides:



Advertisements
Similar presentations
Double-Linked Lists and Circular Lists
Advertisements

Section 2.5 Single-Linked Lists. A linked list is useful for inserting and removing at arbitrary locations The ArrayList is limited because its add and.
Interfaces CSC 171 FALL 2004 LECTURE 14. Project 1 review public class Rational { private int numerator, denominator; public Rational(int numerator, int.
Ics202 Data Structures. hh tail head (b) LinkedList head tail Element datum next 3 Integer Element datum next 1 Integer Element datum next 4 Integer.
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
Chapter 7 Iterators Modified. Chapter Scope The purpose of an iterator The Iterator and Interable interfaces The concept of fail-fast collections Using.
COMP 103 Linked Stack and Linked Queue.
OOP in Java – Inner Classes Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
OOP in Java – Inner Classes Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
OOP in Java – Inner Classes Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.
Chapter 101 Dynamic Data Structures and Generics Chapter 10.
Interfaces and Inner Classes. What is an Interface?  What is “presented to the user”?  The public part of a class?  What is the substance of an interface?
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.
IMPLEMENTING ARRAYLIST – Part 2 COMP 103. RECAP  Abstract Classes – overview, details in 2 nd year  Implementing the ArrayList: size(), get(), set()
LinkedList Many slides from Horstmann modified by Dr V.
CMSC 341 Linked Lists, Stacks and Queues. 8/3/2007 UMBC CMSC 341 LSQ 2 Implementing Your Own Linked List To create a doubly linked list as seen below.
Queue FIFO (First In First Out) Java Doc peek, element offer poll, add
CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Information and Computer Sciences University of Hawaii, Manoa
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 13 Implementing.
Lecture Objectives  Linked list data structures:  Singly-linked (cont.)  Doubly-linked  Circular  Implementing the List interface as a linked list.
12/18/2015ITK 2751 CollectionList A bstractSequentialList Vector Stack LinkedList {interface} AbstractList {abstract} ArrayList Java Provides a List interface.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 8 Lists, Iterators, and Doubly Linked Lists.
An Advanced Code Pattern: Inner Classes CSE301 University of Sunderland Harry R. Erwin, PhD Half Lecture.
Lists and Iterators Copyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming.
COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201.
Iterators ITI 1121 N. El Kadri. Motivation Given a (singly) linked-list implementation of the interface List, defined as follows, public interface List.
LINKED LIST’S EXAMPLES Salim Malakouti. Linked List? 523 Pointer Node ValuePointer.
Iteration Abstraction SWE Software Construction Fall 2009.
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.
Iterator Summary Slides by Entesar Al-Mosallam adopted from Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
Iterators. Iterator  An iterator is any object that allows one to step through each element in a list (or, more generally, some collection).
Iterators. Chapter Scope The purpose of an iterator The Iterator and Interable interfaces The concept of fail-fast collections Using iterators to solve.
1 Iterators & the Collection Classes. 2 » The Collection Framework classes provided in the JAVA API(Application Programmer Interface) contains many type.
CSCI 62 Data Structures Dr. Joshua Stough September 23, 2008.
Linked Data Structures
Iterators.
The List ADT.
Java Methods Lists and Iterators Object-Oriented Programming
Queue FIFO (First In First Out) Java Doc peek, element poll, remove
Single-Linked Lists.
Week 4 - Friday CS221.
Linked Lists Chapter 5 (continued)
Chapter 16 Iterators.
Top Ten Words that Almost Rhyme with “Peas”
Iteration Abstraction
MyList<T> It’s a generic type, <T> is a type parameter
Chapter 7 Iterators.
null, true, and false are also reserved.
Data Type (how to view it)
(Java Collections Framework) AbstractSequentialList
Copyright ©2012 by Pearson Education, Inc. All rights reserved
slides adapted from Marty Stepp
Linked Lists [AJ 15].
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Iteration Abstraction
Example: LinkedSet<T>
JavaScript Reserved Words
Linked Lists Chapter 5 (continued)
Programming II (CS300) Chapter 07: Linked Lists
Iterators Dan Fleck.
Part of the Collections Framework
Data Structures and Algorithms 2/2561
Linked Lists Chapter 5 (continued)
Java Generics & Iterators
Presentation transcript:

Need for Iterators O(n2) this is a common application 7 2 T get(int at) 6 1 4 for (int i=0; i< L.size(); i++) { ..... ..... L.get(i)..... } 3 5 this is a common application 8 Suppose L.size() = n 1+2+3+.....+n = (1+n)*n/2 O(n2) O(n) ? We cannot improve this, because the internal information and structures are protected. But, 12/1/2018 IT 179

RandomAccess vs Linked List static int countEven(MyList<Integer> x) { int count=0; if (x instanceof RandomAccess) { for (int i=0;i<x.size();i++) if (x.get(i)%2 == 0) count++; } else { Iterator<Integer> xi = x.iterator(); while (xi.hasNext()) if (xi.next()%2 == 0) count++; } return count; 12/1/2018 IT 179

Iterable Another inner class that defines our iterator. Iterable<T> {interface} + Iterator<T> iterator(); MyList<T> {interface} (see the java document) …. package myUtil; import java.util.Iterator; import java.util.NoSuchElementException; public class MySLinkedList <T> implements MyList<T>{ { ..... public Iterator<T> iterator() return new myIterator(); } Another inner class that defines our iterator. 12/1/2018 IT 179

API java.util.Iterator<E> Iterator<T> {interface} + boolean hasNext(); + T next(); + void remove(); API java.util.Iterator<E> head 1 2 3 4 5 6 nextNode Point to the node that will be returned by next call of next() seenNode Point to the node that is seen (may be removed later) prevNode Point to the node before the seen node. 12/1/2018 IT 179

myIterator inner class package myUtil; import java.util.Iterator; import java.util.NoSuchElementException; public class MySLinkedList<T> implements MyList<T>, Iterable<T>{ ..... /***** This is an inner class for myIterator ************/ public class myIterator implements Iterator<T> { private Node<T> prevNode=null, seenNode=null, nextNode=head; // current node to be seen //by next() public boolean hasNext() { return (nextNode != null); } … /***** This is the end of inner class myIterator ************/ myIterator inner class 12/1/2018 IT 179

myIterator (countined) /*****This is an inner class for myIterator ************/ public class myIterator implements Iterator<T> { ........ public T next() { if (! hasNext()) throw new NoSuchElementException("NoSuchElementException: "); if (seenNode != null) // The seen node is not removed prevNode = seenNode; seenNode = nextNode; nextNode = nextNode.next; return seenNode.data; } ....... /***** This is the end of inner class myIterator ************/ 12/1/2018 IT 179

myIterator (countined) /*****This is an inner class for myIterator ************/ public class myIterator implements Iterator<T> { ........ public void remove() { if (seenNode == null) throw new IllegalStateException("IllegalStateException: "); if (seenNode == head) { head = seenNode.next; } else { prevNode.next = nextNode; seenNode = null; // it's gone size--; ....... /***** This is the end of inner class myIterator ************/ 12/1/2018 IT 179

Anonymous Class public Iterator<T> iterator() { return new myIterator(); } internal class Anonymous Class public Iterator<T> iterator() { return new Iterator<T>() { /* Return an Anonymous class as an iterator ***/ …… /* same as the body the myIterator */ /* This is the end of the Anonymous class *****/ }; /** don't forget ";" **/ } /* end of iterator */ 12/1/2018 IT 179

A Marker Interface static <T extends RandomAccess> void swap(T x, int i, int j) { if (!(x instanceof List)) return; T temp = x.get(i); x.set(i,x.get(j)); x.set(j,temp); } public static void main(String[] args) { ArrayList<Object> A = new ArrayList<Object>(); LinkedList<Object> B = new LinkedList<Object>(); List<Object> C; ... ... swap(A,30,95); swap(B,30,95); C = B swap(C,30,95); 12/1/2018 IT 179

Single Linked List O(n2) 1+2+3+.....+n = (1+n)*n/2 It is very common to add new items to the end of the list public void add(T item) { // append item to the end of the list add(size, item); } head Easy Solution: tail 52 Can Iterator help? No! 12 14 16 23 45 48 1+2+3+.....+n = (1+n)*n/2 O(n2) for (int i=0; i<n; i++) { something = ... ... list.add(something); } Add to the tail (append) 12/1/2018 IT 179

API java.util.ListIterator<T> {interface} + boolean hasNext(); + boolean hasPrevious(); + T next(); + T previous(); + void remove(); + int nextIndex() + int previousIndex(); + void add(T item); + void set(T item); Tail doesn’t help head tail 1 2 3 4 nextNode Point to the node that will be returned by next call of next() seenNode Point to the node that is seen (may be removed later) prevNode Point to the node before the seen node. 12/1/2018 IT 179

Single Linked List O(n2) 1+2+3+.....+n = (1+n)*n/2 It is very common to add new items to the end of the list public void add(T item) { // append item to the end of the list add(size, item); } head Easy Solution: tail 52 Can Iterator help? No! 12 14 16 23 45 48 1+2+3+.....+n = (1+n)*n/2 O(n2) for (int i=0; i<n; i++) { something = ... ... list.add(something); } Add to the tail (append) 12/1/2018 IT 179

API java.util.ListIterator<T> {interface} + boolean hasNext(); + boolean hasPrevious(); + T next(); + T previous(); + void remove(); + int nextIndex() + int previousIndex(); + void add(T item); + void set(T item); Tail doesn’t help head tail 1 2 3 4 nextNode Point to the node that will be returned by next call of next() seenNode Point to the node that is seen (may be removed later) prevNode Point to the node before the seen node. 12/1/2018 IT 179

API java.util.ListIterator<E> ListIterator<T> {interface} + boolean hasNext(); + boolean hasPrevious(); + T next(); + T previous(); + void remove(); + int nextIndex() + int previousIndex(); + void add(T item); + void set(T item); head tail 1 2 3 4 nextNode Point to the node that will be returned by next call of next() seenNode Point to the node that is seen (may be removed later) 12/1/2018 IT 179