Head l d b c a current i current j its constructor

This preview shows page 23 - 38 out of 69 pages.

head l D B C A current i current j
Image of page 23

Subscribe to view the full document.

Its constructor initialises current to null , a value which, symbolically, designates being before the start of the list. public class LinkedList<E> implements List<E> { private static class Node<E> { ... } private Node<E> head; private class ListIterator implements Iterator<E> { private Node<E> current; private ListIterator() { current = null; // <--- } ... } ... }
Image of page 24
How to create an iterator? public class LinkedList<E> implements List<E> { private static class Node<E> { ... } private Node<E> head; private class ListIterator implements Iterator<E> { private Node<E> current; private ListIterator() { current = null; } ... } public Iterator<E> iterator() { return new ListIterator(); // <--- } ... } iterator() : is an instance method of an object of the class LinkedList .
Image of page 25

Subscribe to view the full document.

public class LinkedList<E> implements List<E> { private static class Node<E> { ... } private Node<E> head; private class ListIterator implements Iterator<E> { private Node<E> current; private ListIterator() { current = null; } public E next() { if ( current == null ) { current = head; } else { current = current.next; } return current.value; } public boolean hasNext() { ... } } public Iterator<E> iterator() { return new ListIterator<E>(); } ... }
Image of page 26
Usage List<Double> doubles = new LinkedList<Double>(); doubles.add( new Double( 5.1 ) ); doubles.add( new Double( 3.2 ) ); double sum = 0.0; Iterator<Double> i = doubles.iterator(); while ( i.hasNext() ) { Double v = i.next(); sum = sum + v.doubleValue(); } Exercise: trace the execution of the above statements.
Image of page 27

Subscribe to view the full document.

Is it fast? We’ve worked hard to create a mechanism to traverse the list. What is worth it? # nodes inside iterator (ms) (ms) 10,000 2 6 20,000 4 5 40,000 8 11 80,000 14 19 160,000 23 48
Image of page 28
Many iterators List<Double> doubles = new LinkedList<Double>(); for ( int c=0; c<5; c++ ) { doubles.add( new Double( c ) ); } Iterator<Double> i = doubles.iterator(); while ( i.hasNext() ) { Double iVal = i.next(); Iterator<Double> j = doubles.iterator(); while ( j.hasNext() ) { Double jVal = j.next(); System.out.println( "("+iVal+","+jVal+")" ); } } (0.0,0.0), (0.0,1.0), . . . , (0.0,4.0), . . . , (4.0,4.0).
Image of page 29

Subscribe to view the full document.

add( E o ) and remove() Let’s add two methods to the interface Iterator , add( E o ) and remove() .
Image of page 30
add() By definition, i.add( value ) will insert the new value immediately before the next element that would be returned by next() . Therefore, the next call to next() is unaffected!
Image of page 31

Subscribe to view the full document.

current i first last doubles 2.718 7.389 current i first last doubles 2.718 3.142 7.389 Call to i.add( new Double( 3.142 ) ) , adding an element at an intermediate position in the list, notice that i.next() returns the same value with or without the insertion!
Image of page 32
first last doubles 2.718 3.142 7.389 current i current i first last doubles 1.618 2.718 3.142 7.389 Adding an element when the iterator is positioned to the left of the list, i.add( new Double( 1.618 ) ) , again i.next() returns the same value with or without insertion.
Image of page 33

Subscribe to view the full document.

current i first last doubles 1.618 2.718 3.142 7.389 current i first last doubles 1.618 2.718 3.142 7.389 9.806 Adding an element when positioned at the end of the list, i.add( new Double( 1.618 ) ) , notice that hasNext() returns the same value with or without insertion!
Image of page 34
public class LinkedList<E> implements List<E> { private static class Node<E> { ... } private Node<E> first; private class ListIterator implements Iterator<E> { private Node<E> current; private ListIterator() { current = null; } public E next() { ... } public boolean hasNext() { ... } public boolean add(E o) { if ( o == null ) return false; if ( current == null ) { first = new Node<E>( o, first ); current = first; } else { current.next = new Node<E>( o, current.next ); current = current.next; } return true; } public void remove() { ... } }
Image of page 35

Subscribe to view the full document.

public Iterator<E> iterator() { return new ListIterator(); } // ... all the usual methods of LinkedList }
Image of page 36
What would the following test program display?
Image of page 37

Subscribe to view the full document.

Image of page 38
You've reached the end of this preview.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern