JavaRush /Java 博客 /Random-ZH /自己实现的双向链接LinkedList,深度克隆。
alexnjc
第 31 级

自己实现的双向链接LinkedList,深度克隆。

已在 Random-ZH 群组中发布
任务是创建您自己的单链或双向链表对象的实现,但您不能使用 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