structure
Class SinglyLinkedList

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

public class SinglyLinkedList
extends AbstractList

Singly linked lists have elements connected by a single reference. They are space-efficient, but tail-related operations may be more costly than with doubly linked lists.

See Also:
DoublyLinkedList

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

Method Detail

add

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

Specified by:
add in interface List
Specified by:
add in interface Structure
Overrides:
add in class AbstractList
Parameters:
value - The value to be added to tail of list.
See Also:
#addToHead

addFirst

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

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

removeFirst

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

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

addLast

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

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

removeLast

public Object removeLast()
Remove last value from list.

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

getFirst

public Object getFirst()
Fetch first element of list.

Specified by:
getFirst in interface List
Overrides:
getFirst in class AbstractList
Returns:
A reference to first element of list.

getLast

public Object getLast()
Fetch last element of list.

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

contains

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

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

remove

public Object remove(Object 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.

size

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

Returns:
The number of elements in list.

clear

public void clear()
Remove all values from list.


get

public 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)

set

public Object set(int i,
                  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,
                Object o)
Insert value at location.

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

remove

public 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(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(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 Iterator iterator()
Returns an iterator traversing list from head to tail.

Returns:
An iterator to traverse list.
See Also:
AbstractIterator, Iterator, Enumeration, Structure.elements()

toString

public String toString()
Construct a string representing list.

Overrides:
toString in class Object
Returns:
A string representing list.