Class CircularLLIterator<E>

java.lang.Object
  extended by CircularLLIterator<E>
All Implemented Interfaces:
java.util.Iterator<E>, java.util.ListIterator<E>

public class CircularLLIterator<E>
extends java.lang.Object
implements java.util.ListIterator<E>

The CircularLLIterator class provides an iterator through a linear LinkedList object that makes it appear to be circular. The element after the last element in the list, as returned by next, is the first element in the list. The element before the first element in the list, as returned by previous, is the last element in the list.

In a circular iteration, the methods hasNext and hasPrevious will always return true unless the list is empty. This means that the usual iteration pattern of

     while (iterator.hasNext())
         <do something>
will result in an infinite loop. To iterate once through the elements of the list, use the list iterator returned by the linked list's listIterator method or get the size of the list and iterate through that many times. (The latter technique will not work if you are adding or removing elements as you iterate.)

Version:
7 October 2009
Author:
Alyce Brady, Nathan Sprague (Changed to use generics)
See Also:
LinkedList

Constructor Summary
CircularLLIterator(java.util.LinkedList<E> list)
          Constructs a circular iterator for the specified list.
 
Method Summary
 void add(E obj)
          Inserts the specified element into the list.
 boolean hasNext()
          Returns true if this list iterator has more elements when traversing the list in the forward direction.
 boolean hasPrevious()
          Returns true if this list iterator has more elements when traversing the list in the reverse direction.
 E next()
          Returns the next element in the list.
 int nextIndex()
          Returns the index of the element that would be returned by a subsequent call to next.
 E previous()
          Returns the previous element in the list.
 int previousIndex()
          Returns the index of the element that would be returned by a subsequent call to previous.
 void remove()
          Removes from the list the last element that was returned by next or previous.
 void set(E obj)
          Replaces the last element returned by next or previous with the specified element.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CircularLLIterator

public CircularLLIterator(java.util.LinkedList<E> list)
Constructs a circular iterator for the specified list.

Parameters:
list - the linked list through which to iterate
Method Detail

hasNext

public boolean hasNext()
Returns true if this list iterator has more elements when traversing the list in the forward direction. (In other words, returns true if next would return an element rather than throwing an exception.) Since this is a circular iterator, returns true unless the list is empty.

Specified by:
hasNext in interface java.util.Iterator<E>
Specified by:
hasNext in interface java.util.ListIterator<E>
Returns:
true if the list iterator has more elements when traversing the list in the forward direction

hasPrevious

public boolean hasPrevious()
Returns true if this list iterator has more elements when traversing the list in the reverse direction. (In other words, returns true if previous would return an element rather than throwing an exception.) Since this is a circular iterator, returns true unless the list is empty.

Specified by:
hasPrevious in interface java.util.ListIterator<E>
Returns:
true if the list iterator has more elements when traversing the list in the reverse direction

nextIndex

public int nextIndex()
Returns the index of the element that would be returned by a subsequent call to next. (Returns list size if the list is empty.)

Specified by:
nextIndex in interface java.util.ListIterator<E>

previousIndex

public int previousIndex()
Returns the index of the element that would be returned by a subsequent call to previous. (Returns -1 if the list is empty.)

Specified by:
previousIndex in interface java.util.ListIterator<E>

next

public E next()
Returns the next element in the list. This method may be called repeatedly to iterate through the list, or intermixed with calls to previous to go back and forth. (Note that alternating calls to next and previous will return the same element repeatedly.)

Specified by:
next in interface java.util.Iterator<E>
Specified by:
next in interface java.util.ListIterator<E>
Returns:
the next element in the list
Throws:
NoSuchElementException - if the list is empty

previous

public E previous()
Returns the previous element in the list. This method may be called repeatedly to iterate through the list backwards, or intermixed with calls to next to go back and forth. (Note that alternating calls to next and previous will return the same element repeatedly.)

Specified by:
previous in interface java.util.ListIterator<E>
Returns:
the previous element in the list
Throws:
NoSuchElementException - if the list is empty

add

public void add(E obj)
Inserts the specified element into the list. The element is inserted immediately before the next element that would be returned by next, if any, and after the next element that would be returned by previous, if any. (If the list contains no elements, the new element becomes the sole element on the list.) The new element is inserted before the implicit cursor: a subsequent call to next would be unaffected, and a subsequent call to previous would return the new element. (This call increases by one the value that would be returned by a call to nextIndex or previousIndex.)

Specified by:
add in interface java.util.ListIterator<E>
Parameters:
obj - the element to insert

remove

public void remove()
Removes from the list the last element that was returned by next or previous. This call can only be made once per call to next or previous. It can be made only if ListIterator.add has not been called after the last call to next or previous.

Specified by:
remove in interface java.util.Iterator<E>
Specified by:
remove in interface java.util.ListIterator<E>
Throws:
java.lang.IllegalStateException - neither next nor previous have been called, or remove or add have been called after the last call to next or previous

set

public void set(E obj)
Replaces the last element returned by next or previous with the specified element. This call can be made only if neither ListIterator.remove nor ListIterator.add have been called after the last call to next or previous.

Specified by:
set in interface java.util.ListIterator<E>
Parameters:
obj - the element with which to replace the last element returned by next or previous