|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectfi.joensuu.cs.tra.TraLinkedList<E>
public class TraLinkedList<E>
TraLinkedList is a simple
linked list composed of
fi.joensuu.cs.tra.Listnodes.
TraLinkedList acts as a wrapper that keeps track of the first
and last ListNodes and provides methods for manipulating the
list.
Some methods like
next(fi.joensuu.cs.tra.ListNode
and
previous(fi.joensuu.cs.tra.ListNode
are basically just wrappers for
ListNode.next()
and
ListNode.previous()
.
EOL
is user to mark the end of
the list, and belongs to one list only. It cannot be
operated upon except when used in inserting a
ListNode at the end of the
list. Inserting a ListNode
at the beginning of the list is done by calling insert with
the first node as the position. In other words, the nodes are
always inserted before the specified node.
For example, reversing a list using two lists
could be done like this
TraLinkedListOf note is that the element references are the same here even though the node references are not. That means that modifying the elements in one list modifies those of the other one too, unless of course the elements are immutable (primitive wrappers, Strings et cetera.) Using the the same nodes in two different lists should be impossible and even if it is not, is highly unadvisable.originalList = new TraLinkedList (); for (int i = 20; --i >= 0; ) originalList.insert(originalList.EOL, new Integer(i)); ListNode tmp; TraLinkedList reversedList = new TraLinkedList (); tmp = originalList.last(); while (tmp != null) { reversedList.insert(reversedList.EOL, tmp.getElement()); tmp = reversedList.previous(tmp); }
Nested Class Summary | |
---|---|
class |
TraLinkedList.ElementIteratorWrapper
|
class |
TraLinkedList.NodeIteratorWrapper
|
Field Summary | |
---|---|
ListNode<E> |
EOL
Denotes the end of the list. |
Constructor Summary | |
---|---|
TraLinkedList()
Creates an empty list. |
|
TraLinkedList(String elements)
Creates an empty list and uses elements instead of "nodes" in iterator exceptions. |
Method Summary | |
---|---|
Iterator<E> |
elementIterator()
Returns an Iterator over the elements of this list. |
Iterable<E> |
elements()
Returns an Iterable covering the elements of this list for use with the foreach-construct. |
ListNode<E> |
find(ListNode<E> node,
E element)
Returns the first ListNode with the specified
element starting from the specified
ListNode or null if none is found. |
ListNode<E> |
first()
Returns the first node of this list. |
ListNode<E> |
insert(ListNode<E> node,
E element)
Inserts a new ListNode with the specified
element in front of the specified
ListNode . |
boolean |
isEmpty()
Returns true if this list is empty. |
Iterator<E> |
iterator()
Returns an Iterator over the elements of this list. |
ListNode<E> |
last()
Returns the last node of this list. |
ListNode<E> |
next(ListNode<E> node)
Returns the specified ListNode 's next
ListNode or EOL if none exists. |
Iterator<ListNode<E>> |
nodeIterator()
Returns an Iterator over the nodes of this list. |
Iterable<ListNode<E>> |
nodes()
Returns an Iterable covering the nodes of this list for use with the foreach-construct. |
ListNode<E> |
previous(ListNode<E> node)
Returns the specified ListNode 's previous
ListNode or null if none exists. |
E |
remove(ListNode<E> node)
Removes a ListNode from this
TraLinkedList . |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public final ListNode<E> EOL
Constructor Detail |
---|
public TraLinkedList()
public TraLinkedList(String elements)
elements
- name of elements for use in exceptions, plural.Method Detail |
---|
public boolean isEmpty()
ListNode
s.public ListNode<E> first()
ListNode
of this list or
EOL if this list is empty.public ListNode<E> last()
ListNode
of this list or
EOL if this list is empty.public ListNode<E> insert(ListNode<E> node, E element)
ListNode
with the specified
element in front of the specified
ListNode
. If node is
EOL
,
the ListNode
will be inserted at the end of
the TraLinkedList
.
node
- the ListNode
in front of which
newNode will be inserted. If node is
EOL
, node
will be inserted at the end of the
TraLinkedList
.element
- the element for the new ListNode
ListNode
TraLinkedListException
- if node is null.public E remove(ListNode<E> node)
ListNode
from this
TraLinkedList
.
node
- the ListNode
to be removed.
ListNode
's
element.
TraLinkedListException
- if node is null.
TraLinkedListException
- if node is
EOL
.public ListNode<E> next(ListNode<E> node)
ListNode
's next
ListNode
or EOL if none exists.
node
- the ListNode
the next
ListNode
of which is to be returned.
ListNode
of node
or EOL if none exists.
TraLinkedListException
- if node is null.public ListNode<E> previous(ListNode<E> node)
ListNode
's previous
ListNode
or null if none exists.
node
- the ListNode
the previous
ListNode
of which is to be returned.
ListNode
of node
or null if none exists.
TraLinkedListException
- if node is null.public ListNode<E> find(ListNode<E> node, E element)
ListNode
with the specified
element starting from the specified
ListNode
or null if none is found.
node
- the node to start searching from.element
- the element to search for.
ListNode
with the
specified element or null if none is found.
TraLinkedListException
- if node is null
or EOL
.public Iterable<E> elements()
public Iterable<ListNode<E>> nodes()
public Iterator<E> iterator()
iterator
in interface Iterable<E>
public Iterator<E> elementIterator()
public Iterator<ListNode<E>> nodeIterator()
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |