DataStructures
Class LinkedList<E>

java.lang.Object
  extended by DataStructures.LinkedList<E>
Type Parameters:
E - the type of elements held in this list
All Implemented Interfaces:
java.lang.Iterable<E>

public class LinkedList<E>
extends java.lang.Object
implements java.lang.Iterable<E>

A simple implementation of singly linked lists.

Lists are created empty. Items can be inserted at the front of the list by

   list.addFirst(item) 
 
where item is any item of type E.

Items can be removed from the front of the list by

   list.removeFirst();
 
which removes the first item from the list. Lists can be traversed using an iterator, for example:
   for (Integer i : list) {
       System.out.println(i);
   }
 

Author:
Peter Williams
See Also:
Iterable, Iterator

Constructor Summary
LinkedList()
          Constructs an empty list suitable for holding items of type E.
 
Method Summary
 void addFirst(E item)
          Inserts an item at the front of the list.
 boolean contains(E item)
          Tests whether an item is present in the list.
 boolean equals(java.lang.Object other)
          Implements the equals method.
 E firstItem()
          Returns the first item in the list without removing it.
 boolean isEmpty()
          Tests if the list is empty.
 java.util.Iterator<E> iterator()
          Iterates over the items in the list.
 void removeFirst()
          Removes the first item from the list without returning it.
 void reverse()
          Reverses the list.
 int size()
          Returns the length of the list
 java.lang.String toString()
          Converts the list to a string.
 
Methods inherited from class java.lang.Object
getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

LinkedList

public LinkedList()
Constructs an empty list suitable for holding items of type E.

Method Detail

isEmpty

public boolean isEmpty()
Tests if the list is empty.

Returns:
the status of the list.

size

public int size()
Returns the length of the list

Returns:
the length of the list

addFirst

public void addFirst(E item)
Inserts an item at the front of the list.

Parameters:
item - the item of type E to be inserted.

firstItem

public E firstItem()
Returns the first item in the list without removing it.

Returns:
the item at the front of the list
Throws:
java.util.NoSuchElementException - if the list is empty.

removeFirst

public void removeFirst()
Removes the first item from the list without returning it.

Throws:
java.util.NoSuchElementException - if the list is empty.

contains

public boolean contains(E item)
Tests whether an item is present in the list.

Parameters:
item - the item of type E to be searched for.
Returns:
true if the item is present, false otherwise

reverse

public void reverse()
Reverses the list.


iterator

public java.util.Iterator<E> iterator()
Iterates over the items in the list.

Specified by:
iterator in interface java.lang.Iterable<E>
Returns:
the iterator.
Throws:
java.util.NoSuchElementException - if the next method is called when there is no next element
java.lang.IllegalStateException - if the next method has not yet been called, or the remove method has already been called after the last call to the next method.

equals

public boolean equals(java.lang.Object other)
Implements the equals method.

Overrides:
equals in class java.lang.Object
Parameters:
other - the second linked list.
Returns:
true if the lists are equal, false otherwise.

toString

public java.lang.String toString()
Converts the list to a string.

Overrides:
toString in class java.lang.Object
Returns:
the string representation.