JavaRush /Java Blog /Random EN /Own implementation of doubly linked LinkedList, deep clon...
alexnjc
Level 31

Own implementation of doubly linked LinkedList, deep cloning.

Published in the Random EN group
The task is to create your own implementation of a singly or doubly linked list of objects, but you cannot use standard implementations of the LinkedList type. Write methods for deep cloning of this list, check how the leaf is cloned. You cannot use the class constructor to create a copy. When deleting elements from a copy of a list, the original list should not change, that is, the copies should be completely independent of each other. What methods will be optimal? I wrote a bunch of code, everything seems to work except deep cloning. There are a lot of glitches when changing the list :-( //List as FILO stack public class MyLinkedList implements Cloneable, Serializable { private Node objects; // top of stack private Node first; // bottom of stack ... Inner class 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;} ...
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION