Bad News Bearers Lab

Class CircularLLIterator

java.lang.Object
  |
  +--CircularLLIterator
All Implemented Interfaces:
java.util.Iterator, java.util.ListIterator

public class CircularLLIterator
extends java.lang.Object
implements java.util.ListIterator

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 iterate through that many times. (The latter technique will not work if you are adding or removing elements as you iterate.)

Version:
13 October 2002
See Also:
LinkedList

Constructor Summary
CircularLLIterator(java.util.LinkedList list)
          Constructs a circular iterator for the specified list.
 
Method Summary
 void add(java.lang.Object 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.
static void main(java.lang.String[] args)
          Runs test suite for the CircularLLIterator class.
 java.lang.Object 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.
 java.lang.Object 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(java.lang.Object obj)
          Replaces the last element returned by next or previous with the specified element.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CircularLLIterator

public CircularLLIterator(java.util.LinkedList 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.ListIterator
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
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

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

next

public java.lang.Object 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.ListIterator
Returns:
the next element in the list
Throws:
NoSuchElementException - if the list is empty

previous

public java.lang.Object 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
Returns:
the previous element in the list
Throws:
NoSuchElementException - if the list is empty

add

public void add(java.lang.Object 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
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.ListIterator
Throws:
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(java.lang.Object 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
Parameters:
obj - the element with which to replace the last element returned by next or previous

main

public static void main(java.lang.String[] args)
Runs test suite for the CircularLLIterator class.
Parameters:
args - standard parameter; not used

Bad News Bearers Lab

Copyright 2002 Alyce Faulstich Brady