20Iterator - ITI 1121. Introduction to Computing II Marcel...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
ITI 1121. Introduction to Computing II * Marcel Turcotte School of Information Technology and Engineering Version of March 20, 2011 Abstract Iterator 1 (part I) * These lecture notes are meant to be looked at on a computer screen. Do not print them unless it is necessary. 1 A great opportunity to discuss time complexity (asymptotic analysis, big-O), encapsulation, object-oriented programming, interface, and more.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Motivation Given a (singly) linked list implementation, called LinkedList , of the interface List , defined as follows. public interface List<E> { public abstract boolean add( E obj ); public abstract E get( int index ); public abstract boolean remove( E obj ); public abstract int size(); } Consider writing statements to traverse the list. Compare writing the code inside and outside of the implementation. The difficulties would be the same for a doubly linked list.
Background image of page 2
Traversing a list inside/outside of the class List<String> colors; colors = new LinkedList<String>(); colors.add( "bleu" ); colors.add( "blanc" ); colors.add( "rouge" ); colors.add( "jaune" ); colors.add( "vert" ); colors.add( "orange" );
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
A — traversing a linked list inside of the class Being inside of the implementation, the nodes can be accessed directly. Node<E> p = head; while ( p != null ) { System.out.println( p.value ); p = p.next; }
Background image of page 4
B — traversing a linked list outside of the class Traversing a list without having access to the underlying implementation requires using E get( int pos ) . for ( int i=0; i < colors.size(); i++ ) { System.out.println( colors.get( i ) ); } How efficient, w.r.t. time, this code is compared to writing it within the class LinkedList ? Much faster, faster, same, slower, much slower?
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Timing results Here are the timing results for 20 , 000 , 40 , 000 and 80 , 000 nodes lists, averaged over 5 runs (times in milliseconds). # nodes A B 20,000 2 20,644 40,000 5 94,751 80,000 12 407,059 For 80 , 000 nodes it takes 6.5 minutes traversing the list using get( pos ) vs 12 ms for the other approach. What can explain this huge difference?
Background image of page 6
for ( int i=0; i< names.size(); i++ ) { System.out.println( names.get( i ) ); } Call Number of nodes visited get( 0 ) 1 get( 1 ) 2 get( 2 ) 3 get( 3 ) 4 . . . . . . get( n-1 ) n n X i =1 i = n ( n + 1) 2 n 2 Implementation A visits n nodes, implemenation B visits n 2 nodes!
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
# nodes A B 20,000 2 20,644 40,000 5 94,751 80,000 12 407,059
Background image of page 8
Executing the following statements (implementation B). for ( int i=0; i< names.size(); i++ ) { System.out.println( names.get( i ) ); } It is as if implementation A had been written like this. for ( int i=0; i< size(); i++ ) {
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 40

20Iterator - ITI 1121. Introduction to Computing II Marcel...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online