ICOM 4035 Data Structures

Fall 2007

 

Laboratory 6: List Iterators

 

1.    Objectives

 

  1. Understand the usage of iterators
  2. Use them to run across a collection

 

2.    Overview

 

This laboratory focuses on iterators. These objects allow manipulating collections in a secure and easy way, without worrying about the implementation of the collection itself. They also allow using instructions such as the foreach cycle in a new designed list, acting as a cursor to each element inside the collection. All you need to do in your code is create the iterator which implements the interface Iterator<E> as an inner class of the collection class and implement the interface Iterable<E> in the collection class itself.

 

Contrasting with indexing

In procedural languages it is common to use indexing based on a loop counter to loop through all the elements in a sequence such as an array. Although indexing may also be used with some object-oriented containers, the use of iterators may have advantages.

3.    Practice

 

Download the file lab6.tar.gz and in it you will find several files. You are asked to implement the iterator found at the end of the DLList.java file. Implement such iterator in a manner where all operations in worst case scenario take time O(1). Also create the remove method in a manner where you can only remove one node per call to the next method.