2
votes

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!

1
Are you certain it says the inner class has a copy constructor...? I agree that doesn't make much sense but your implementation is correct.OldCurmudgeon
Yeah I'm 100% sure, it asked to write the copy constructor and clone method for both the LinkedList class and the inner Node class. I was confused because I never saw that before but seeing as your the second person to try and clarify that, the TA or prof probably just made a mistake. I didn't get a chance to clarify with the TA during class time. Thanks for replying quickly btw!D222

1 Answers

0
votes

It looks like you are being asked to implement the copy constructor and clone method on the Linked List class... not the Node class.

When specified to make a deep copy, your cloned Linked List object should copy all the nodes in the original Linked List.

I suggest you start with:

public MyLinkedList(MyLinkedList originalNode)