public class TraLinkedList<E>
extends java.lang.Object
implements java.lang.Iterable<E>
next(fi.uef.cs.tra.ListNode<E>)
and
previous(fi.uef.cs.tra.ListNode<E>)
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); }
Modifier and Type | Class and Description |
---|---|
class |
TraLinkedList.ElementIteratorWrapper |
class |
TraLinkedList.NodeIteratorWrapper |
Modifier and Type | Field and Description |
---|---|
ListNode<E> |
EOL
Denotes the end of the list.
|
Constructor and Description |
---|
TraLinkedList()
Creates an empty list.
|
TraLinkedList(java.lang.String elements)
Creates an empty list and uses elements instead of
"nodes" in iterator exceptions.
|
Modifier and Type | Method and Description |
---|---|
java.util.Iterator<E> |
elementIterator()
Returns an Iterator over the elements of this list.
|
java.lang.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)
|
ListNode<E> |
first()
Returns the first node of this list.
|
ListNode<E> |
insert(ListNode<E> node,
E element)
|
boolean |
isEmpty()
Returns true if this list is empty.
|
java.util.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)
|
java.util.Iterator<ListNode<E>> |
nodeIterator()
Returns an Iterator over the nodes of this list.
|
java.lang.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)
|
E |
remove(ListNode<E> node)
Removes a
ListNode from this
TraLinkedList . |
public TraLinkedList()
public TraLinkedList(java.lang.String elements)
elements
- name of elements for use in exceptions, plural.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)
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)
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 java.lang.Iterable<E> elements()
public java.lang.Iterable<ListNode<E>> nodes()
public java.util.Iterator<E> iterator()
iterator
in interface java.lang.Iterable<E>
public java.util.Iterator<E> elementIterator()