edu.colorado.nodes
Class DoubleNode

java.lang.Object
  extended by edu.colorado.nodes.DoubleNode

public class DoubleNode
extends java.lang.Object

A DoubleNode provides a node for a linked list with double data in each node.

See Also:
Java Source Code for this class (www.cs.colorado.edu/~main/edu/colorado/nodes/DoubleNode.java) , Node, BooleanNode, ByteNode, CharNode, 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
DoubleNode(double initialData, DoubleNode initialLink)
          Initialize a node with a specified initial data and link to the next node.
 
Method Summary
 void addNodeAfter(double item)
          Modification method to add a new node after this node.
 double getData()
          Accessor method to get the data from this node.
 DoubleNode getLink()
          Accessor method to get a reference to the next node after this node.
static DoubleNode listCopy(DoubleNode source)
          Copy a list.
static DoubleNode[] listCopyWithTail(DoubleNode source)
          Copy a list, returning both a head and tail reference for the copy.
static int listLength(DoubleNode head)
          Compute the number of nodes in a linked list.
static DoubleNode[] listPart(DoubleNode start, DoubleNode end)
          Copy part of a list, providing a head and tail reference for the new copy.
static DoubleNode listPosition(DoubleNode head, int position)
          Find a node at a specified position in a linked list.
static DoubleNode listSearch(DoubleNode head, double 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(double newData)
          Modification method to set the data in this node.
 void setLink(DoubleNode 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

DoubleNode

public DoubleNode(double initialData,
                  DoubleNode 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(double item)
Modification method to add a new node after this node.

Parameters:
item - 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 item. 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 DoubleNode.

getData

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

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

getLink

public DoubleNode 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 DoubleNode listCopy(DoubleNode 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 DoubleNode[] listCopyWithTail(DoubleNode 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 int listLength(DoubleNode 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 DoubleNode[] listPart(DoubleNode start,
                                    DoubleNode 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 are not references to nodes on the same list.
java.lang.NullPointerException - Indicates that start is null.
java.lang.OutOfMemoryError - Indicates that there is insufficient memory for the new list.

listPosition

public static DoubleNode listPosition(DoubleNode 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 not positive.

listSearch

public static DoubleNode listSearch(DoubleNode head,
                                    double 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 piece of data to search for
Returns:
The return value is a reference to the first node that contains the specified target. If there is no such node, 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(double 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.

setLink

public void setLink(DoubleNode 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.