structure
Class CircularList

java.lang.Object
  |
  +--structure.AbstractStructure
        |
        +--structure.AbstractList
              |
              +--structure.CircularList
All Implemented Interfaces:
List, Structure
Direct Known Subclasses:
AppendableList

public class CircularList
extends AbstractList

An implementation of lists using circularly linked elements, similar to that of java.util.LinkedList.

This class is an implementation of the List interface. Operations accessing or modifying either the head or the tail of the list execute in constant time. Circular lists are as space-efficient as singly linked lists, but tail-related operations are less costly.

Example usage: To place a copy of every unique parameter passed to a program into a CircularList, we would use the following:

 public static void main(String[] arguments)
 {
     CircularList argList = new CircularList();
     for (int i = 0; i < arguments.length; i++){
	   if (!argList.contains(arguments[i])){
	       argList.add(arguments[i]);
         }
    }
    System.out.println(argList);
 }
 

See Also:
SinglyLinkedList, DoublyLinkedList

Field Summary
protected  int count
          Number of elements within circular list.
protected  SinglyLinkedListElement tail
          A reference to tail of list.
 
Constructor Summary
CircularList()
          Construct an empty circular list.
 
Method Summary
 void add(int i, java.lang.Object o)
          Insert value at location.
 void add(java.lang.Object value)
          Add an element to head of circular list.
 void addFirst(java.lang.Object value)
          Add an element to head of list.
 void addLast(java.lang.Object value)
          Add a value to tail of circular list.
 void clear()
          Remove elements of list.
 boolean contains(java.lang.Object value)
          Check if a list contains an element.
 java.lang.Object get(int i)
          Get value at location i.
 java.lang.Object getFirst()
          Determine if a list is empty.
 java.lang.Object getLast()
          Peek at last element of list.
protected  SinglyLinkedListElement getTail()
          Accessor method for tail field
 int indexOf(java.lang.Object value)
          Determine first location of a value in list.
 boolean isEmpty()
          Determine if a list is empty.
 java.util.Iterator iterator()
          Construct an iterator over elements of list.
 int lastIndexOf(java.lang.Object value)
          Determine last location of a value in list.
 java.lang.Object remove(int i)
          Remove and return value at location i.
 java.lang.Object remove(java.lang.Object value)
          Remove a value from a list.
 java.lang.Object removeFirst()
          Remove a value from head of list.
 java.lang.Object removeLast()
          Remove a value from tail of list.
 java.lang.Object set(int i, java.lang.Object o)
          Set value stored at location i to object o, returning old value.
 int size()
          Determine size of list.
 java.lang.String toString()
          Generate a string representation of list.
 
Methods inherited from class structure.AbstractList
get, remove
 
Methods inherited from class structure.AbstractStructure
elements, hashCode, values
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface structure.Structure
elements, values
 

Field Detail

tail

protected SinglyLinkedListElement tail
A reference to tail of list. tail points to head.


count

protected int count
Number of elements within circular list.

Constructor Detail

CircularList

public CircularList()
Construct an empty circular list.

Method Detail

add

public void add(java.lang.Object value)
Add an element to head of circular list.

Specified by:
add in interface List
Overrides:
add in class AbstractList
Parameters:
value - value to be added to list.
See Also:
AbstractList.addLast(java.lang.Object)

addFirst

public void addFirst(java.lang.Object value)
Add an element to head of list.

Specified by:
addFirst in interface List
Overrides:
addFirst in class AbstractList
Parameters:
value - value added to head of list.

addLast

public void addLast(java.lang.Object value)
Add a value to tail of circular list.

Specified by:
addLast in interface List
Overrides:
addLast in class AbstractList
Parameters:
value - value to be added.

getFirst

public java.lang.Object getFirst()
Determine if a list is empty.

Specified by:
getFirst in interface List
Overrides:
getFirst in class AbstractList
Returns:
True if there are no elements within list.

getLast

public java.lang.Object getLast()
Peek at last element of list.

Specified by:
getLast in interface List
Overrides:
getLast in class AbstractList
Returns:
value of last element of list.

removeFirst

public java.lang.Object removeFirst()
Remove a value from head of list.

Specified by:
removeFirst in interface List
Overrides:
removeFirst in class AbstractList
Returns:
value removed.

removeLast

public java.lang.Object removeLast()
Remove a value from tail of list.

Specified by:
removeLast in interface List
Overrides:
removeLast in class AbstractList
Returns:
value removed.

contains

public boolean contains(java.lang.Object value)
Check if a list contains an element.

Specified by:
contains in interface List
Overrides:
contains in class AbstractList
Parameters:
value - value sought.
Returns:
True iff value is within list.

remove

public java.lang.Object remove(java.lang.Object value)
Remove a value from a list.

Parameters:
value - value sought.
Returns:
value removed from list.

size

public int size()
Determine size of list.

Returns:
number of elements in list.

get

public java.lang.Object get(int i)
Get value at location i.

Parameters:
i - position of value to be retrieved.
Returns:
value retrieved from location i (returns null if i invalid)

getTail

protected SinglyLinkedListElement getTail()
Accessor method for tail field


set

public java.lang.Object set(int i,
                            java.lang.Object o)
Set value stored at location i to object o, returning old value.

Parameters:
i - location of entry to be changed.
o - new value
Returns:
former value of ith entry of list.

add

public void add(int i,
                java.lang.Object o)
Insert value at location.

Parameters:
i - index of this new value
o - value to be stored

remove

public java.lang.Object remove(int i)
Remove and return value at location i.

Parameters:
i - position of value to be retrieved.
Returns:
value retrieved from location i (returns null if i invalid)

indexOf

public int indexOf(java.lang.Object value)
Determine first location of a value in list.

Parameters:
value - value sought.
Returns:
index (0 is first element) of value, or -1

lastIndexOf

public int lastIndexOf(java.lang.Object value)
Determine last location of a value in list.

Parameters:
value - value sought.
Returns:
index (0 is first element) of value, or -1

iterator

public java.util.Iterator iterator()
Construct an iterator over elements of list. Elements are traversed from head to tail.

Returns:
iterator associated with list.

isEmpty

public boolean isEmpty()
Determine if a list is empty.

Specified by:
isEmpty in interface List
Overrides:
isEmpty in class AbstractList
Returns:
True iff list is empty.

clear

public void clear()
Remove elements of list.


toString

public java.lang.String toString()
Generate a string representation of list.

Overrides:
toString in class java.lang.Object
Returns:
String representing list.