structure
Class CircularList<ELTTYPE>

java.lang.Object
  extended by structure.AbstractStructure<ELTTYPE>
      extended by structure.AbstractList<ELTTYPE>
          extended by structure.CircularList<ELTTYPE>
All Implemented Interfaces:
java.lang.Iterable<ELTTYPE>, List<ELTTYPE>, Structure<ELTTYPE>

public class CircularList<ELTTYPE>
extends AbstractList<ELTTYPE>

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

Constructor Summary
CircularList()
          Construct an empty circular list.
 
Method Summary
 void add(ELTTYPE value)
          Add an element to head of circular list.
 void add(int i, ELTTYPE o)
          Insert value at location.
 void addFirst(ELTTYPE value)
          Add an element to head of list.
 void addLast(ELTTYPE value)
          Add a value to tail of circular list.
 void clear()
          Remove elements of list.
 boolean contains(ELTTYPE value)
          Check if a list contains an element.
 ELTTYPE get(int i)
          Get value at location i.
 ELTTYPE getFirst()
          Determine if a list is empty.
 ELTTYPE getLast()
          Peek at last element of list.
 int indexOf(ELTTYPE value)
          Determine first location of a value in list.
 boolean isEmpty()
          Determine if a list is empty.
 java.util.Iterator<ELTTYPE> iterator()
          Construct an iterator over elements of list.
 int lastIndexOf(ELTTYPE value)
          Determine last location of a value in list.
 ELTTYPE remove(ELTTYPE value)
          Remove a value from a list.
 ELTTYPE remove(int i)
          Remove and return value at location i.
 ELTTYPE removeFirst()
          Remove a value from head of list.
 ELTTYPE removeLast()
          Remove a value from tail of list.
 ELTTYPE set(int i, ELTTYPE 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
equals, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface structure.Structure
elements, values
 

Constructor Detail

CircularList

public CircularList()
Construct an empty circular list.

Pre:
constructs a new circular list
Method Detail

add

public void add(ELTTYPE value)
Add an element to head of circular list.

Specified by:
add in interface List<ELTTYPE>
Specified by:
add in interface Structure<ELTTYPE>
Overrides:
add in class AbstractList<ELTTYPE>
Parameters:
value - value to be added to list.
See Also:
AbstractList.addLast(ELTTYPE)
Post:
adds value to beginning of list

addFirst

public void addFirst(ELTTYPE value)
Add an element to head of list.

Specified by:
addFirst in interface List<ELTTYPE>
Overrides:
addFirst in class AbstractList<ELTTYPE>
Parameters:
value - value added to head of list.
Pre:
value non-null
Post:
adds element to head of list

addLast

public void addLast(ELTTYPE value)
Add a value to tail of circular list.

Specified by:
addLast in interface List<ELTTYPE>
Overrides:
addLast in class AbstractList<ELTTYPE>
Parameters:
value - value to be added.
Pre:
value non-null
Post:
adds element to tail of list

getFirst

public ELTTYPE getFirst()
Determine if a list is empty.

Specified by:
getFirst in interface List<ELTTYPE>
Overrides:
getFirst in class AbstractList<ELTTYPE>
Returns:
True if there are no elements within list.
Pre:
!isEmpty()
Post:
returns value at head of list

getLast

public ELTTYPE getLast()
Peek at last element of list.

Specified by:
getLast in interface List<ELTTYPE>
Overrides:
getLast in class AbstractList<ELTTYPE>
Returns:
value of last element of list.
Pre:
!isEmpty()
Post:
returns value at tail of list

removeFirst

public ELTTYPE removeFirst()
Remove a value from head of list.

Specified by:
removeFirst in interface List<ELTTYPE>
Overrides:
removeFirst in class AbstractList<ELTTYPE>
Returns:
value removed.
Pre:
!isEmpty()
Post:
returns and removes value from head of list

removeLast

public ELTTYPE removeLast()
Remove a value from tail of list.

Specified by:
removeLast in interface List<ELTTYPE>
Overrides:
removeLast in class AbstractList<ELTTYPE>
Returns:
value removed.
Pre:
!isEmpty()
Post:
returns and removes value from tail of list

contains

public boolean contains(ELTTYPE value)
Check if a list contains an element.

Specified by:
contains in interface List<ELTTYPE>
Specified by:
contains in interface Structure<ELTTYPE>
Overrides:
contains in class AbstractList<ELTTYPE>
Parameters:
value - value sought.
Returns:
True iff value is within list.
Pre:
value != null
Post:
returns true if list contains value, else false

remove

public ELTTYPE remove(ELTTYPE value)
Remove a value from a list.

Parameters:
value - value sought.
Returns:
value removed from list.
Pre:
value != null
Post:
removes and returns element equal to value, or null

size

public int size()
Determine size of list.

Returns:
number of elements in list.
Post:
returns number of elements in list

get

public ELTTYPE 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)
Pre:
0 <= i < size()
Post:
returns object found at that location

set

public ELTTYPE set(int i,
                   ELTTYPE 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.
Pre:
0 <= i < size()
Post:
sets ith entry of list to value o, returns old value

add

public void add(int i,
                ELTTYPE o)
Insert value at location.

Parameters:
i - index of this new value
o - value to be stored
Pre:
0 <= i <= size()
Post:
adds ith entry of list to value o

remove

public ELTTYPE 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)
Pre:
0 <= i < size()
Post:
removes and returns object found at that location

indexOf

public int indexOf(ELTTYPE value)
Determine first location of a value in list.

Parameters:
value - value sought.
Returns:
index (0 is first element) of value, or -1
Pre:
value is not null
Post:
returns (0-origin) index of value, or -1 if value is not found

lastIndexOf

public int lastIndexOf(ELTTYPE value)
Determine last location of a value in list.

Parameters:
value - value sought.
Returns:
index (0 is first element) of value, or -1
Pre:
value is not null
Post:
returns (0-origin) index of value, or -1 if value is not found

iterator

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

Returns:
iterator associated with list.
See Also:
AbstractIterator, Iterator, Enumeration, Structure.elements()
Post:
returns iterator to traverse list elements

isEmpty

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

Specified by:
isEmpty in interface List<ELTTYPE>
Specified by:
isEmpty in interface Structure<ELTTYPE>
Overrides:
isEmpty in class AbstractList<ELTTYPE>
Returns:
True iff list is empty.
Post:
returns true if no elements in list

clear

public void clear()
Remove elements of list.

Post:
removes all elements from list

toString

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

Overrides:
toString in class java.lang.Object
Returns:
String representing list.
Post:
returns a string representing list