structure
Class SinglyLinkedList<ELTTYPE>

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

public class SinglyLinkedList<ELTTYPE>
extends AbstractList<ELTTYPE>

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

This class is a basic implementation of the List interface. Operations accessing or modifying the head of the list execute in constant time. Operations accessing or modifying the tail of the list execute in a time proportional to the length of the list. Singly linked lists are space-efficient, but tail-related operations may be more costly than with doubly linked lists.

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

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

See Also:
DoublyLinkedList, CircularList

Constructor Summary
SinglyLinkedList()
          Construct an empty list.
 
Method Summary
 void add(ELTTYPE value)
          Add an object to tail of list.
 void add(int i, ELTTYPE o)
          Insert value at location.
 void addFirst(ELTTYPE value)
          Add a value to head of list.
 void addLast(ELTTYPE value)
          Add a value to tail of list.
 void clear()
          Remove all values from list.
 boolean contains(ELTTYPE value)
          Check to see if a value is in list.
 ELTTYPE get(int i)
          Get value at location i.
 ELTTYPE getFirst()
          Fetch first element of list.
 ELTTYPE getLast()
          Fetch last element of list.
 int indexOf(ELTTYPE value)
          Determine first location of a value in list.
 java.util.Iterator<ELTTYPE> iterator()
          Returns an iterator traversing list from head to tail.
 int lastIndexOf(ELTTYPE value)
          Determine last location of a value in list.
 ELTTYPE remove(ELTTYPE value)
          Remove a value from list.
 ELTTYPE remove(int i)
          Remove and return value at location i.
 ELTTYPE removeFirst()
          Remove a value from first element of list.
 ELTTYPE removeLast()
          Remove last value from list.
 ELTTYPE set(int i, ELTTYPE o)
          Set value stored at location i to object o, returning old value.
 int size()
          Determine number of elements in list.
 java.lang.String toString()
          Construct a string representing list.
 
Methods inherited from class structure.AbstractList
get, isEmpty, 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

SinglyLinkedList

public SinglyLinkedList()
Construct an empty list.

Post:
generates an empty list
Method Detail

add

public void add(ELTTYPE value)
Add an object to tail of list.

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

addFirst

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

Specified by:
addFirst in interface List<ELTTYPE>
Overrides:
addFirst in class AbstractList<ELTTYPE>
Parameters:
value - The value to be added to head of list.
Post:
value is added to beginning of list

removeFirst

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

Specified by:
removeFirst in interface List<ELTTYPE>
Overrides:
removeFirst in class AbstractList<ELTTYPE>
Returns:
The value actually removed.
Pre:
list is not empty
Post:
removes and returns value from beginning of list

addLast

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

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

removeLast

public ELTTYPE removeLast()
Remove last value from list.

Specified by:
removeLast in interface List<ELTTYPE>
Overrides:
removeLast in class AbstractList<ELTTYPE>
Returns:
The value actually removed.
Pre:
list is not empty
Post:
removes last value from list

getFirst

public ELTTYPE getFirst()
Fetch first element of list.

Specified by:
getFirst in interface List<ELTTYPE>
Overrides:
getFirst in class AbstractList<ELTTYPE>
Returns:
A reference to first element of list.
Pre:
list is not empty
Post:
returns first value in list

getLast

public ELTTYPE getLast()
Fetch last element of list.

Specified by:
getLast in interface List<ELTTYPE>
Overrides:
getLast in class AbstractList<ELTTYPE>
Returns:
A reference to last element of list.
Pre:
list is not empty
Post:
returns last value in list

contains

public boolean contains(ELTTYPE value)
Check to see if a value is in list.

Specified by:
contains in interface List<ELTTYPE>
Specified by:
contains in interface Structure<ELTTYPE>
Overrides:
contains in class AbstractList<ELTTYPE>
Parameters:
value - The value sought.
Returns:
True if value is within list.
Pre:
value is not null
Post:
returns true iff value is found in list

remove

public ELTTYPE remove(ELTTYPE value)
Remove a value from list. At most one value will be removed.

Parameters:
value - The value to be removed.
Returns:
The actual value removed.
Pre:
value is not null
Post:
removes first element with matching value, if any

size

public int size()
Determine number of elements in list.

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

clear

public void clear()
Remove all values from list.

Post:
removes all elements from 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 the (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 the (0-origin) index of value, or -1 if value is not found

iterator

public java.util.Iterator<ELTTYPE> iterator()
Returns an iterator traversing list from head to tail.

Returns:
An iterator to traverse list.
See Also:
AbstractIterator, Iterator, Enumeration, Structure.elements()
Post:
returns enumeration allowing traversal of list

toString

public java.lang.String toString()
Construct a string representing list.

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