Well use the value null to denote the situation where

This preview shows page 22 - 38 out of 40 pages.

We’ll use the value null to denote the situation where the iterator is positioned to the left of the list. The first time next() is called, we’ll move current to the first node and return its value. For each successive call, current is moved one position forward and the value found at that node is returned.
Image of page 22

Subscribe to view the full document.

head l B C A current Iterator i = l.iterator(); head l B C A current after i.next(); head l B C A current after i.next(); head l B C A current after i.next();
Image of page 23
Implementing iterator(). public class LinkedList<E> implements List<E>, Iterator<E> { private static class Node<E> { ... } private Node<E> head; private Node<E> tail; private Node<E> current; // <--- public Iterator<E> iterator() { current = null; return this; } }
Image of page 24

Subscribe to view the full document.

But do we need iterator() ? Couldn’t we simply assign null in the constructor of the list? No, the method is necessary to traverse the list more than one time. int sum = 0; int n = 0; Iterator<Integer> i = l.iterator(); while ( i.hasNext() ) { Integer v = i.next(); sum += v.intValue(); n++; } i = l.iterator(); while ( i.hasNext() ) { Integer v = i.next(); if ( v.intValue() > ( sum / n ) ) { System.out.println( "is greater than average" ); } }
Image of page 25
Implementing next . public class LinkedList<E> implements List<E>, Iterator<E> { private static class Node<E> { ... } private Node<E> head; private Node<E> current; public Iterator<E> iterator() { ... } public E next() { if ( current == null ) { current = head; } else { current = current.next; } if ( current == null ) { throw new NoSuchElementException(); } return current.value; } }
Image of page 26

Subscribe to view the full document.

head l B C A current
Image of page 27
head l B C A current
Image of page 28

Subscribe to view the full document.

head l B C A current
Image of page 29
head l B C A current
Image of page 30

Subscribe to view the full document.

Current limitation Our current implementation does not allow for multiple iterators! while ( i.hasNext() ) { oi = i.next(); while ( j.hasNext() ) { oj = i.next(); // process oi and oj } }
Image of page 31
What’s needed then? We need as many references (variables) as iterators; An iterator has to have access to the implementation (Node) ; An iterator needs access to the instance variables of the class LinkedList .
Image of page 32

Subscribe to view the full document.

Allowing for multiple iterators Suggestions?
Image of page 33
Implementation -1.5- Let’s create the following top-level class (in the same package as LinkedList ), this will require that i) Node is also a top-level class and ii) the variable head should be protected .
Image of page 34

Subscribe to view the full document.

class LinkedListIterator<E> implements Iterator<E> { private Node<E> current; private LinkedList<E> myList; LinkedListIterator( LinkedList<E> myList ) { this.myList = myList; current = null; } public boolean hasNext() { return ( ( current == null && myList.head != null ) || ( current != null && current.next != null) ); } public E next() { if ( current == null ) { current = myList.head; } else { current = current.next; } return current.value; } }
Image of page 35
head l D B C A current i current j myList myList
Image of page 36

Subscribe to view the full document.

The method iterator() of the class LinkedList is defined as follows.
Image of page 37
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