DataStructures
Class LinkedList

java.lang.Object
  |
  +--DataStructures.LinkedList

public class LinkedList
extends java.lang.Object

A simple implementation of singly linked lists.

Lists are created empty, for example:

   LinkedList list = new LinkedList();
 
after which items can be inserted at the front of the list by
   list.addFirst(item) 
 
where item is any Object.

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 (Iterator i = list.iterator(); i.hasNext();) {
       System.out.println(e.next());
   {
 

Author:
Peter Williams
See Also:
Iterator

Constructor Summary
LinkedList()
          Constructs an empty list.
 
Method Summary
 void addFirst(java.lang.Object item)
          Inserts an item at the front of the list.
 boolean contains(java.lang.Object item)
          Tests whether an item is present in the list.
 boolean equals(java.lang.Object other)
          Implements the equals method.
 java.lang.Object firstItem()
          Returns the first item in the list.
 boolean isEmpty()
          Tests if the list is empty.
 java.util.Iterator iterator()
          Iterates over the items in the list.
 void removeFirst()
          Removes the first item from the list.
 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
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

LinkedList

public LinkedList()
Constructs an empty list.
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(java.lang.Object item)
Inserts an item at the front of the list.
Parameters:
item - the Object to be inserted.

firstItem

public java.lang.Object firstItem()
Returns the first item in the list.
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.
Throws:
java.util.NoSuchElementException - if the list is empty.

contains

public boolean contains(java.lang.Object item)
Tests whether an item is present in the list.
Parameters:
item - the object 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 iterator()
Iterates over the items in the list.
Returns:
the iterator.
Throws:
java.util.NoSuchElementException - if the next method is called when there is no next element
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.