Node p head while p null systemoutprintln pvalue p

This preview shows page 9 - 18 out of 40 pages.

Node p = head; while ( p != null ) { System.out.println( p.value ); p = p.next; }
Image of page 9

Subscribe to view the full document.

What mechanism could the implementation provide that would allow to efficiently traverse the list? We are outside of the class definition, therefore, we do not have access to the implementation ( p.next and such), yet the need to traverse a data structure is frequent. (!) The concept that we are introducing today will apply to a specific context, TRAVERSING A LIST. WE WILL NOT PROVIDE A GENERAL MECHANISM TO MAKE get( i ) EFFICIENT ALWAYS.
Image of page 10
Iterator: introduction A uniform mechanism to traverse a variety of data structures, such as lists, but also trees and other data structures to be seen in CSI 2114; Gives access to all the elements, one a time. It’s a mechanism that is used by Java’s own collections.
Image of page 11

Subscribe to view the full document.

Concept ‘‘create an iterator’’ (* positioned before the start of the list *) while ( ‘‘has more elements?’’ ) { ‘‘move the iterator forward and return the value’’ ‘‘process the value’’ }
Image of page 12
Concept
Image of page 13

Subscribe to view the full document.

Iterator interface The implementation of the Iterator is separated from its usage by creating an interface for it. public interface Iterator<E> { public abstract boolean hasNext(); public abstract E next(); }
Image of page 14
/** * An iterator for lists that allows the programmer to traverse the * list. */ public interface Iterator<E> { /** * Returns the next element in the list. This method may be called * repeatedly to iterate through the list. * * @return the next element in the list. * @exception NoSuchElementException if the iteration has no next element. */ public abstract E next(); /** * Returns <tt>true</tt> if this list iterator has more elements when * traversing the list in the forward direction. (In other words, returns * <tt>true</tt> if <tt>next</tt> would return an element rather than * throwing an exception.) * * @return <tt>true</tt> if the list iterator has more elements when * traversing the list in the forward direction. */ public abstract boolean hasNext(); }
Image of page 15

Subscribe to view the full document.

Issues Which class will be implementing the interface? How to create and initialize a new iterator? How to move the iterator forward? How to detect the end? This process is modeled on traversing an array and should be familiar.
Image of page 16
Implementation -1- In our first implementation, the class LinkedList developed earlier is modified to implement the interface Iterator . This involves modifying the header of the class, adding “implements Iterator”, and to provide an implementation for each of the methods defined in the interface.
Image of page 17

Subscribe to view the full document.

Image of page 18
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