edu.colorado.nodes
Class Node<E>

java.lang.Object
  extended by edu.colorado.nodes.Node<E>

public class Node<E>
extends java.lang.Object

A Node provides a generic node for a linked list. Each node contains a piece of data (which is a reference to an E object) and a link (which is a reference to the next node of the list). The reference stored in a node can be null.

See Also:
Java Source Code for this class (www.cs.colorado.edu/~main/edu/colorado/nodes/Node.java) , BooleanNode, ByteNode, CharNode, DoubleNode, FloatNode, IntNode, LongNode, ShortNode
Note:
Lists of nodes can be made of any length, limited only by the amount of free memory in the heap. But beyond Integer.MAX_VALUE (2,147,483,647), the answer from listLength is incorrect because of arithmetic overflow.

Constructor Summary
Node(E initialData, Node<E> initialLink)
          Initialize a node with a specified initial data and link to the next node.
 
Method Summary
 void addNodeAfter(E element)
          Modification method to add a new node after this node.
 E getData()
          Accessor method to get the data from this node.
 Node<E> getLink()
          Accessor method to get a reference to the next node after this node.
static
<E> Node<E>
listCopy(Node<E> source)
          Copy a list.
static
<E> Node<E>[]
listCopyWithTail(Node<E> source)
          Copy a list, returning both a head and tail reference for the copy.
static
<E> int
listLength(Node<E> head)
          Compute the number of nodes in a linked list.
static
<E> Node<E>[]
listPart(Node<E> start, Node<E> end)
          Copy part of a list, providing a head and tail reference for the new copy.
static
<E> Node<E>
listPosition(Node<E> head, int position)
          Find a node at a specified position in a linked list.
static
<E> Node<E>
listSearch(Node<E> head, E target)
          Search for a particular piece of data in a linked list.
 void removeNodeAfter()
          Modification method to remove the node after this node.
 void setData(E newData)
          Modification method to set the data in this node.
 void setLink(Node<E> newLink)
          Modification method to set the link to the next node after this node.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Node

public Node(E initialData,
            Node<E> initialLink)
Initialize a node with a specified initial data and link to the next node. Note that the initialLink may be the null reference, which indicates that the new node has nothing after it.

Parameters:
initialData - the initial data of this new node
initialLink - a reference to the node after this new node--this reference may be null to indicate that there is no node after this new node.
Postcondition:
This node contains the specified data and link to the next node.
Method Detail

addNodeAfter

public void addNodeAfter(E element)
Modification method to add a new node after this node.

Parameters:
element - the data to place in the new node
Postcondition:
A new node has been created and placed after this node. The data for the new node is element. Any other nodes that used to be after this node are now after the new node.
Throws:
java.lang.OutOfMemoryError - Indicates that there is insufficient memory for a new Node.

getData

public E getData()
Accessor method to get the data from this node.

Parameters:
- - none
Returns:
the data from this node

getLink

public Node<E> getLink()
Accessor method to get a reference to the next node after this node.

Parameters:
- - none
Returns:
a reference to the node after this node (or the null reference if there is nothing after this node)

listCopy

public static <E> Node<E> listCopy(Node<E> source)
Copy a list.

Parameters:
source - the head of a linked list that will be copied (which may be an empty list in where source is null)
Returns:
The method has made a copy of the linked list starting at source. The return value is the head reference for the copy.
Throws:
java.lang.OutOfMemoryError - Indicates that there is insufficient memory for the new list.

listCopyWithTail

public static <E> Node<E>[] listCopyWithTail(Node<E> source)
Copy a list, returning both a head and tail reference for the copy.

Parameters:
source - the head of a linked list that will be copied (which may be an empty list in where source is null)
Returns:
The method has made a copy of the linked list starting at source. The return value is an array where the [0] element is a head reference for the copy and the [1] element is a tail reference for the copy.
Throws:
java.lang.OutOfMemoryError - Indicates that there is insufficient memory for the new list.

listLength

public static <E> int listLength(Node<E> head)
Compute the number of nodes in a linked list.

Parameters:
head - the head reference for a linked list (which may be an empty list with a null head)
Returns:
the number of nodes in the list with the given head
Note:
A wrong answer occurs for lists longer than Int.MAX_VALUE.

listPart

public static <E> Node<E>[] listPart(Node<E> start,
                                     Node<E> end)
Copy part of a list, providing a head and tail reference for the new copy.

Parameters:
start/end - references to two nodes of a linked list
copyHead/copyTail - the method sets these to refer to the head and tail node of the new list that is created
Precondition:
start and end are non-null references to nodes on the same linked list, with the start node at or before the end node.
Returns:
The method has made a copy of the part of a linked list, from the specified start node to the specified end node. The return value is an array where the [0] component is a head reference for the copy and the [1] component is a tail reference for the copy.
Throws:
java.lang.IllegalArgumentException - Indicates that start and end do not satisfy the precondition.
java.lang.OutOfMemoryError - Indicates that there is insufficient memory for the new list.

listPosition

public static <E> Node<E> listPosition(Node<E> head,
                                       int position)
Find a node at a specified position in a linked list.

Parameters:
head - the head reference for a linked list (which may be an empty list in which case the head is null)
position - a node number
Precondition:
position > 0.
Returns:
The return value is a reference to the node at the specified position in the list. (The head node is position 1, the next node is position 2, and so on.) If there is no such position (because the list is too short), then the null reference is returned.
Throws:
java.lang.IllegalArgumentException - Indicates that position is zero.

listSearch

public static <E> Node<E> listSearch(Node<E> head,
                                     E target)
Search for a particular piece of data in a linked list.

Parameters:
head - the head reference for a linked list (which may be an empty list in which case the head is null)
target - a target to search for
Returns:
The return value is a reference to the first node that contains the specified target. If the target is non-null, then the target.equals method is used to find such a node. The target may also be null, in which case the return value is a reference to the first node that contains a null reference for its data. If there is no node that contains the target, then the null reference is returned.

removeNodeAfter

public void removeNodeAfter()
Modification method to remove the node after this node.

Parameters:
- - none
Precondition:
This node must not be the tail node of the list.
Postcondition:
The node after this node has been removed from the linked list. If there were further nodes after that one, they are still present on the list.
Throws:
java.lang.NullPointerException - Indicates that this was the tail node of the list, so there is nothing after it to remove.

setData

public void setData(E newData)
Modification method to set the data in this node.

Parameters:
newData - the new data to place in this node
Postcondition:
The data of this node has been set to newData. This data is allowed to be null.

setLink

public void setLink(Node<E> newLink)
Modification method to set the link to the next node after this node.

Parameters:
newLink - a reference to the node that should appear after this node in the linked list (or the null reference if there is no node after this node)
Postcondition:
The link to the node after this node has been set to newLink. Any other node (that used to be in this link) is no longer connected to this node.