java - Deep Cloning of Custom Linked List -


i trying implement clone method deep clonning of custom linked list(of rectangle objects). able correct value when there no element or single element in list not when there more 1 element in list. think problem clone method in mylinknode class. can please me this?

note: have shortened code readability purposes

class rectangle implements cloneable {     private double width;     private double height;     // setters , getters these class variables      public object clone() {         try {             object rectclone = super.clone();             return (rectangle) rectclone;         } catch (clonenotsupportedexception c) {             return null;         }     } }   public class mylist implements cloneable {     private mylistnode head;      public object clone() {         try {             object listclone = super.clone();             if (head != null) {                 ((mylist) listclone).head = (mylistnode) head.clone();             }             return (mylist) listclone;         } catch (clonenotsupportedexception c) {             return null;         }     } }   class mylistnode implements cloneable {     private rectangle rectangle;     private mylistnode next;     // getter setter class properties      protected mylistnode clone() {         try {             object nodeclone = super.clone();             ((mylistnode) nodeclone).rectangle = (rectangle) rectangle.clone();             mylistnode temp = this.next;             while (temp != null) {                 ((mylistnode) nodeclone).rectangle = (rectangle) rectangle                         .clone();                 temp = temp.getnext();                 //edited later, posted mistake                 //clone();             }             return (mylistnode) nodeclone;         } catch (clonenotsupportedexception c) {             return null;         }     } } 

---my clone method using recursion(another try)--

protected mylistnode clone() {     try {         object nodeclone = super.clone();         mylistnode temp = this.next;          if( temp == null){         ((mylistnode) nodeclone).rectangle = (rectangle)this.rectangle.clone();         ((mylistnode) nodeclone).next = this.next.clone();         temp = temp.getnext();         }         else{             clone();         }          return  (mylistnode)nodeclone;             }catch (clonenotsupportedexception c) {         return null;     }   } 

--this 1 seems working twice whole thing, although result seems right(correction of previous method)--

protected mylistnode clone() {     try {         object listnodecopy = super.clone();         mylistnode temp = this.next;          if (temp == null) {             ((mylistnode) nodeclone).rectangle = (rectangle) this.rectangle.clone();         } else {             while (temp != null) {                 ((mylistnode) nodeclone).rectangle = (rectangle) this.rectangle.clone();                 ((mylistnode) nodeclone).next = temp.clone();                 temp = temp.getnext();             }         }     return (mylistnode) listnodecopy;     } catch (clonenotsupportedexception c) {         return null;     } } 

any appreciated! new both cloning , custom linked list , have been trying long time :(

your iterative approach wrong. have clone every node have in list class. if try clone on first node when try clone rest clone recursively rest of nodes. first node cloned once, second twice, third 3 times, etc.

i'll try fix recursive approach.

    if( temp == null){ // <-- temp null!! should != null         ((mylistnode) nodeclone).rectangle = (employee)this.rectangle.clone(); <-- employee ?!?!         ((mylistnode) nodeclone).next = this.next.clone();         temp = temp.getnext(); <-- throws npe temp null!! useless anyway. remove line.     } 

this work:

protected mylistnode clone() {     try {         object nodeclone = super.clone();         ((mylistnode) nodeclone).rectangle = (rectangle) this.rectangle.clone();         if (this.next != null) {             ((mylistnode) nodeclone).next = this.next.clone();         }         return (mylistnode) nodeclone;     } catch (clonenotsupportedexception c) {         throw new runtimeexception(c); // <<- best impossible conditions if happen notice     } } 

Comments