This is my first question on stackoverflow so if I didn't ask correctly please give me some insight on how to ask more appropriately.
I'm a bit confused on how to make a copy constructor and a clone method in a Node class. I understand how to make a copy and a clone (without implementing cloneable) for an object in general but I'm still new at LinkedLists.
Here's part of the code I have so far for the Node class (inner class of a LinkedList class)
private class Node
{
private Foo fooObject;
private Node link;
public Node()
{
fooObject= null;
link = null;
}
public Node(Foo fooObject, Node link )
{
this.fooObject = fooObject;
this.setLink(link);
}
public Node(Node originalNode)
{
this(originalNode.getCourse(), originalNode.getLink());
}
public Node clone()
{
return new Node(this);
}
public Foo getFoo()
{
return fooObject;
}
public void setFoo(Foo fooObject)
{
this.fooObject = fooObject;
}
public Node getLink()
{
return link;
}
public void setLink(Node link)
{
this.link = link;
}
}
Making a copy of a Node object still doesn't make much sense to me. Wouldn't this copy a node that is already linked to another node when you'd be using a Node object practically? The copy and the original would be linked to the same node, except the copy wouldn't have a node linking to it? Please help me understand in case I'm just confused, I don't understand how this would be used.
Just for clarification, the problem I'm trying to solve was asked at school in a lab. "Write a LinkedList class with an inner class... the inner class has a copy constructor and a clone method...make sure your copy constructor and clone method produce deep copies..."
*edit Just want to clarify something, being asked to write a copy constructor and clone method in BOTH the LinkedList class and the inner Node class were asked. I'm 100% sure of this, I didn't have time to ask the TA for clarification because too many people were asking her questions! *
Thank you!