weiss.util
Class LinkedList<AnyType>

java.lang.Object
  extended by weiss.util.AbstractCollection<AnyType>
      extended by weiss.util.LinkedList<AnyType>
All Implemented Interfaces:
java.io.Serializable, java.lang.Iterable<AnyType>, Collection<AnyType>, List<AnyType>, Queue<AnyType>

public class LinkedList<AnyType>
extends AbstractCollection<AnyType>
implements List<AnyType>, Queue<AnyType>

LinkedList class implements a doubly-linked list.

See Also:
Serialized Form

Constructor Summary
LinkedList()
          Construct an empty LinkedList.
LinkedList(Collection<? extends AnyType> other)
          Construct a LinkedList with same items as another Collection.
 
Method Summary
 boolean add(AnyType x)
          Adds an item to this collection, at the end.
 void add(int idx, AnyType x)
          Adds an item to this collection, at specified position.
 void addFirst(AnyType x)
          Adds an item to this collection, at front.
 void addLast(AnyType x)
          Adds an item to this collection, at end.
 void clear()
          Change the size of this collection to zero.
 boolean contains(java.lang.Object x)
          Tests if some item is in this collection.
 AnyType element()
          Returns the front item in the list.
 AnyType get(int idx)
          Returns the item at position idx.
 AnyType getFirst()
          Returns the first item in the list.
 AnyType getLast()
          Returns the last item in the list.
 Iterator<AnyType> iterator()
          Obtains an Iterator object used to traverse the collection.
 ListIterator<AnyType> listIterator(int idx)
          Obtains a ListIterator object used to traverse the collection bidirectionally.
 AnyType remove()
          Removes the front item in the queue.
 AnyType remove(int idx)
          Removes an item from this collection.
 boolean remove(java.lang.Object x)
          Removes an item from this collection.
 AnyType removeFirst()
          Removes the first item in the list.
 AnyType removeLast()
          Removes the last item in the list.
 AnyType set(int idx, AnyType newVal)
          Changes the item at position idx.
 int size()
          Returns the number of items in this collection.
 
Methods inherited from class weiss.util.AbstractCollection
equals, hashCode, isEmpty, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface weiss.util.Collection
isEmpty, toArray, toArray
 

Constructor Detail

LinkedList

public LinkedList()
Construct an empty LinkedList.


LinkedList

public LinkedList(Collection<? extends AnyType> other)
Construct a LinkedList with same items as another Collection.

Method Detail

clear

public void clear()
Change the size of this collection to zero.

Specified by:
clear in interface Collection<AnyType>
Overrides:
clear in class AbstractCollection<AnyType>

size

public int size()
Returns the number of items in this collection.

Specified by:
size in interface Collection<AnyType>
Returns:
the number of items in this collection.

contains

public boolean contains(java.lang.Object x)
Tests if some item is in this collection.

Specified by:
contains in interface Collection<AnyType>
Overrides:
contains in class AbstractCollection<AnyType>
Parameters:
x - any object.
Returns:
true if this collection contains an item equal to x.

add

public boolean add(AnyType x)
Adds an item to this collection, at the end.

Specified by:
add in interface Collection<AnyType>
Overrides:
add in class AbstractCollection<AnyType>
Parameters:
x - any object.
Returns:
true.

add

public void add(int idx,
                AnyType x)
Adds an item to this collection, at specified position. Items at or after that position are slid one position higher.

Parameters:
x - any object.
idx - position to add at.
Throws:
java.lang.IndexOutOfBoundsException - if idx is not between 0 and size(), inclusive.

addFirst

public void addFirst(AnyType x)
Adds an item to this collection, at front. Other items are slid one position higher.

Parameters:
x - any object.

addLast

public void addLast(AnyType x)
Adds an item to this collection, at end.

Parameters:
x - any object.

element

public AnyType element()
Returns the front item in the list.

Specified by:
element in interface Queue<AnyType>
Returns:
the front item of null if the queue is empty.
Throws:
NoSuchElementException - if the list is empty.

getFirst

public AnyType getFirst()
Returns the first item in the list.

Throws:
NoSuchElementException - if the list is empty.

getLast

public AnyType getLast()
Returns the last item in the list.

Throws:
NoSuchElementException - if the list is empty.

get

public AnyType get(int idx)
Returns the item at position idx.

Specified by:
get in interface List<AnyType>
Parameters:
idx - the index to search in.
Throws:
java.lang.IndexOutOfBoundsException - if index is out of range.

set

public AnyType set(int idx,
                   AnyType newVal)
Changes the item at position idx.

Specified by:
set in interface List<AnyType>
Parameters:
idx - the index to change.
newVal - the new value.
Returns:
the old value.
Throws:
java.lang.IndexOutOfBoundsException - if index is out of range.

remove

public AnyType remove()
Removes the front item in the queue.

Specified by:
remove in interface Queue<AnyType>
Returns:
the front item.
Throws:
NoSuchElementException - if the list is empty.

removeFirst

public AnyType removeFirst()
Removes the first item in the list.

Returns:
the item was removed from the collection.
Throws:
NoSuchElementException - if the list is empty.

removeLast

public AnyType removeLast()
Removes the last item in the list.

Returns:
the item was removed from the collection.
Throws:
NoSuchElementException - if the list is empty.

remove

public boolean remove(java.lang.Object x)
Removes an item from this collection.

Specified by:
remove in interface Collection<AnyType>
Overrides:
remove in class AbstractCollection<AnyType>
Parameters:
x - any object.
Returns:
true if this item was removed from the collection.

remove

public AnyType remove(int idx)
Removes an item from this collection.

Parameters:
idx - the index of the object.
Returns:
the item was removed from the collection.

iterator

public Iterator<AnyType> iterator()
Obtains an Iterator object used to traverse the collection.

Specified by:
iterator in interface java.lang.Iterable<AnyType>
Specified by:
iterator in interface Collection<AnyType>
Returns:
an iterator positioned prior to the first element.

listIterator

public ListIterator<AnyType> listIterator(int idx)
Obtains a ListIterator object used to traverse the collection bidirectionally.

Specified by:
listIterator in interface List<AnyType>
Parameters:
idx - the index to start the iterator. Use size() to do complete reverse traversal. Use 0 to do complete forward traversal.
Returns:
an iterator positioned prior to the requested element.
Throws:
java.lang.IndexOutOfBoundsException - if idx is not between 0 and size(), inclusive.