JavaRush /จาวาบล็อก /Random-TH /การใช้งาน LinkedList ที่เชื่อมโยงแบบทวีคูณด้วยตนเองและการ...
alexnjc
ระดับ

การใช้งาน LinkedList ที่เชื่อมโยงแบบทวีคูณด้วยตนเองและการโคลนแบบลึก

เผยแพร่ในกลุ่ม
ภารกิจคือการสร้างการใช้งานของคุณเองสำหรับรายการอ็อบเจ็กต์ที่เชื่อมโยงแบบเดี่ยวหรือแบบทวีคูณ แต่คุณไม่สามารถใช้การใช้งานมาตรฐานของประเภท LinkedList ได้ เขียนวิธีการสำหรับการโคลนแบบลึกของรายการนี้ ตรวจสอบว่าการโคลนแบบลีฟเป็นอย่างไร คุณไม่สามารถใช้ตัวสร้างคลาสเพื่อสร้างสำเนาได้ เมื่อลบองค์ประกอบออกจากสำเนาของรายการ รายการดั้งเดิมไม่ควรเปลี่ยนแปลง กล่าวคือ สำเนาควรเป็นอิสระจากกันโดยสมบูรณ์ วิธีการใดจะเหมาะสมที่สุด? ฉันเขียนโค้ดจำนวนมาก ดูเหมือนว่าทุกอย่างจะทำงานได้ ยกเว้นการโคลนแบบลึก มีข้อผิดพลาดมากมายเมื่อเปลี่ยนรายการ :-( //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;} ...
ความคิดเห็น
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION