JavaRush /Java Blog /Random-KO /이중 링크 LinkedList의 자체 구현, 심층 복제.
alexnjc
레벨 31

이중 링크 LinkedList의 자체 구현, 심층 복제.

Random-KO 그룹에 게시되었습니다
작업은 단일 또는 이중 연결 개체 목록의 자체 구현을 만드는 것이지만 LinkedList 유형의 표준 구현을 사용할 수는 없습니다. 이 목록의 심층 복제 방법을 작성하고 리프가 복제되는 방법을 확인하세요. 클래스 생성자를 사용하여 복사본을 만들 수 없습니다. 목록 복사본에서 요소를 삭제할 때 원본 목록은 변경되어서는 안 됩니다. 즉, 복사본은 서로 완전히 독립적이어야 합니다. 어떤 방법이 최적일까요? 나는 많은 코드를 작성했는데, 딥 클로닝을 제외한 모든 것이 작동하는 것 같습니다. 목록을 변경할 때 많은 결함이 있습니다 :-( //List as FILO stack public class MyLinkedList implements Cloneable, Serializable { private Node objects; // top of stack private Node first; // bottom of stack ... 내부 클래스 노드 // 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