Cloning

"clone" - this name comes from the method in the Object class in Java, the top of the class hierarchy in Java, so there is pretty much nothing to be gained from complaining about this word. We can and should understand how this method works, and how overrides of it can be used to implement a desired level of cloning data structures. What makes this concept complicated in languages that support dynamic memory (such as pretty much all modern languages) is that each data structure (class instance) can have pointers to other structures, so the interpretation of cloning gets quite complicated.

Let's start with this original linked list:

Here's what happens if the List object (list) is cloned, without doing any other cloning. 

 

Here's the cloning of the list of Nodes, but notice that the underlying objects (a, b, c, d, e) are not duplicated. I would call this a 2nd-level clone. This cloning would be done in the Node class with code something like:

    // clone in List:
Object clone () {
Node head = head.clone();
return this;
} // end clone in List

// clone in Node
clone () {
Node n = next.clone();
return this;
} // end clone in Node

Here's a full clone of the entire data structure:


ND.