137.229.99.50 writes:
I can't get the cloning operation to work on a linked list. I've tried writing it different ways, but I keep getting only a shallow copy. Following is part of my code.
class Node extends Object implements Cloneable {
Node() {
element = null;
next = null;
}
Node (Object info) {
element = info;
next = null;
}
public Object clone() {
Node o = null;
try {
o = (Node) super.clone();
} catch (CloneNotSupportedException e) {
System.out.println("MyObject can't clone");
}
o.element = (Node) o.element.clone();
return o;
}
Object element;
Node next;
}
class List implements Cloneable {
List() {
head = new Node();
tail = head;
preTail = head;
}
public Object clone() throws CloneNotSupportedException {
List o = null;
try {
o = (List) super.clone();
} catch (CloneNotSupportedException e) {
System.out.println("MyObject can't clone");
}
Node currentNode = this.head;
while ( currentNode != null ) {
currentNode = (Node)currentNode.clone();
currentNode = currentNode.next;
}
return o;
}
I'd appreciate any help.
Jeff
171.211.118.118 writes:
class Node extends Object implements Cloneable {
Node() {
element = null;
next = null;
}
Node (Object info) {
element = info;
next = null;
}
public Object clone() {
Node o = null;
try {
o = (Node) super.clone();
} catch (CloneNotSupportedException e) {
System.out.println("MyObject can't clone");
}
o.element = (Node) o.element.clone();
/****** don't use clone ... that defeats
the purpose of overidding it and
causes you to get a shallow copy..
also that traversal of a linked list
that you have there seems to have
some logic errors ... replace the
try/catch pair and that other stuff
that follows it with ******/
/*
o = new List();
Node ndClone = this.head;
if (ndClone.element != null)
{
o.head.element = ndClone.element;
}
while (ndClone.next != null)
{
ndClone = ndClone.next;
o.element = ndClone.element;
o.next = new Node();
o = o.next;
}
*/
return o;
}
Object element;
Node next;
}
class List implements Cloneable {
List() {
head = new Node();
tail = head;
preTail = head;
}
public Object clone() throws CloneNotSupportedException {
List o = null;
try {
o = (List) super.clone();
} catch (CloneNotSupportedException e) {
System.out.println("MyObject can't clone");
}
Node currentNode = this.head;
while ( currentNode != null ) {
currentNode = (Node)currentNode.clone();
currentNode = currentNode.next;
}
/****** once again, don't use clone ...
and it seems that you're trying
to traverse through the list elements
here for some reason. 2 things here:
1) your implementation has logic
errors
2) it is unnecessary; all the
traversals should be done
in the cloning of the Node class
replace the try/catch pair and that
other stuff that follows it with
******/
/*
o = new List();
o.head = this.head;
o.tail = this.tail;
o.preTail = this.preTail;
*/
return o;
}
MikeD
152.202.134.36 writes:
//besides the
//suggestions in the previous post having copy-
// and-paste errors, it is not the more //object-oriented design
//sorry that preTail thing threw me off and
//I wasn't thinking correctly ...
//I will be assuming that preTail is the second-
//to-last node in the linked list, and that
//tail is the last node in the list and also
//that you find it useful to keep track of them
//for some reason... since this is not a doubly-
//linked list, there is no need to track "tail"
//and I see little use for preTail, but I'll show
//you how to set them according to my above
//assumptions anyways
class Node extends Object implements Cloneable {
Node() {
element = null;
next = null;
}
Node (Object info) {
element = info;
next = null;
}
public Object clone() {
Node o = null;
/*
try {
o = (Node) super.clone();
} catch (CloneNotSupportedException e) {
System.out.println("MyObject can't clone");
}
o.element = (Node) o.element.clone();
*/
/****** don't use Object.clone() ... that
defeats the purpose of overidding it and
causes you to get a shallow copy..
... replace the try/catch pair and that
clone statement with ******/
o = new Node();
if (this.element != null)
{
o.element = this.element;
}
return o;
}
Object element;
Node next;
}
class List implements Cloneable {
List() {
head = new Node();
tail = head;
preTail = head;
}
public Object clone() throws CloneNotSupportedException {
List o = null;
/*
try {
o = (List) super.clone();
} catch (CloneNotSupportedException e) {
System.out.println("MyObject can't clone");
}
Node currentNode = this.head;
while ( currentNode != null ) {
currentNode = (Node)currentNode.clone();
currentNode = currentNode.next;
}
*/
/****** once again, don't use
Object.clone() ... and it seems that you're
trying to traverse through the list
elements here but your implementation has
logic errors ... replace the try/catch pair
and that other stuff that follows it with
******/
o = new List();
Node ndNewCopy = new Node();
o.head = ndNewCopy;
Node ndClone = this.head;
if (ndClone.element != null)
{
ndNewCopy.element = ndClone.element;
}
while (ndClone.next != null)
{
preTail = ndClone;
ndClone = ndClone.next;
tail = ndClone;
ndNewCopy.next = new Node();
ndNewCopy = ndNewCopy.next;
ndNewCopy = (Node) ndClone.clone();
// I can use clone since I overwrote
// it with a deep-copy implementation
// in the Node class
}
return o;
}
/*********** I assume that you declare the
class variables down here somewhere and
that it got cut off ************/
MikeD