JavaRush /Java Blog /Random-TW /自己實作的雙向連結LinkedList,深度克隆。
alexnjc
等級 31

自己實作的雙向連結LinkedList,深度克隆。

在 Random-TW 群組發布
任務是建立您自己的單鍊或雙向鍊錶物件的實現,但您不能使用 LinkedList 類型的標準實作。編寫深度克隆該清單的方法,檢查葉子是如何克隆的。您不能使用類別建構函式來建立副本。從清單副本中刪除元素時,原始清單不應更改,即副本應完全相互獨立。什麼方法是最佳的?我寫了一堆程式碼,除了深度克隆之外,一切似乎都有效。更改清單時有很多故障:-( //List as FILO stack public class MyLinkedList implements Cloneable, Serializable { private Node objects; // top of stack private Node first; // bottom of stack ... 內部類別 Node // FILO class Node implements Cloneable { private Object o; private Node next; private Node prev; public Node(Object o) { if (o !=null){ this.o = o; next = null; prev = null; System.out.println("First element Node created!"); System.out.println("o= "+o +" next= " + next+" prev= "+ prev); } else throw new IllegalArgumentException("Null element not allowed here!"); } public Node(Object o, Node next) { if (o == null) throw new IllegalArgumentException("Null element not allowed here!"); this.o = o; if (next != null) { this.next = next; this.prev = null; this.next.setPrev(this); System.out.println("element Node created!"); System.out.println("o= "+o +" next= " + next+" prev= "+ prev); } else System.out.println("next == null ?!!"); } public Object getO () { return o; } public Node getNext(){ return next;} public Node getPrev(){ return prev;} public void setO (Object o) { this.o = o;} public void setNext(Node next) { this.next = next;} public void setPrev(Node prev) { this.prev = prev;} ...
留言
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION