Jason M
2011-09-27 12:16:51 UTC
Below is my clone() code so far:
public Object clone() {
LinkedList theClone = null;
try {
theClone = (LinkedList) super.clone();
} catch (CloneNotSupportedException e) {
System.out.println("Can't clone LinkedList.\n");
System.exit(1);
}
// Add code to do a "deep copy" (copy each Node in
// the current list.)
}
There is an internal Node class with the following code:
// The following inner class provides the node structure for a simple
// linked list.
private class Node
private Node
private E data; // Reference to data object.
// The following constructor creates a node with fields
// initiallized as specified by the two parameters.
Node(Node
// PRE:
// n -- reference to Node which follows the new one.
// o -- reference to data for the new Node.
// POST:
// "link" and "data" are references matching the parameters.
{
link = n;
data = o;
}
// Accessor methods follow.
public Node
// POST: returns a reference to the cell following the
// current one.
{
return link;
}
public E getData()
// POST: returns a reference to the data object for
// the current cell.
{
return data;
}
// Mutator methods follow.
public void setLink(Node
// PRE: "n" is a new node.
// POST: "link" is set to the value of the parameter.
{
link = n;
}
public void setData(E o)
// PRE: "o" is the new data value for the current node.
// POST: "data" is set to the value of the parameter.
{
data = o;
}
}
}