【问题标题】:LinkedList Deep copy javaLinkedList 深拷贝java
【发布时间】:2016-10-08 09:01:49
【问题描述】:

我试图做我的链接列表的深层副本,称为DictionaryNode,但我无法在显示方法中显示它的内容,因为它始终为空。为什么 DictinaryNode temp 始终为 null ?如果我尝试分配 temp = head 工作,但 temp = copy 没有。

public class ListOfNodes {

public class DictionaryNode {
    protected String word;
    private int level;
    private DictionaryNode next;
    private int space = 0;

    public void displayCopy() {
        DictionaryNode temp = copy.next;
        while( temp != null ) {
            System.out.println(temp.word)
                temp = temp.next;
        }
    }


    public DictionaryNode( String word, int level ) {
        this.word = word;
        this.level = level;
        next = null;
    }
}

private DictionaryNode head = null;
public DictionaryNode copy  = null;

//used to do deep copy
public void Clone() {
    DictionaryNode temp =  head.next;

    while( temp != null ) {
        copy = new DictionaryNode( temp.word , temp.level );
        copy  = copy.next;
        temp = temp.next;
    }
}

public void displayCopy() {
    DictionaryNode temp = copy.next;
    while( temp != null ) {
        Sytem.out.println(temp.word)
            temp = temp.next;
    }
}

【问题讨论】:

  • 你永远不会给head赋值。另外,我认为您的克隆方法应该返回对克隆字典的引用。
  • head 是通过用户从文件中读取来初始化的,但问题是如果我尝试分配 head 的 temp 引用它可以工作并显示所有内容但是如果我尝试 temp = copy 不起作用
  • 如果是这样,那么您发布的代码并不是您实际运行的代码。

标签: java linked-list clone


【解决方案1】:

该程序将演示如何对列表进行深层复制。它比您的具体示例更通用,因此希望对其他人也有帮助。

public class Java_Practice {

private static class LinkedListTest {

    private String data;
    private LinkedListTest next;

    public LinkedListTest(String data) {
        super();
        this.data = data;
    }

    public String getData() {
        return data;
    }

    public LinkedListTest getNext() {
        return next;
    }

    public void setNext(LinkedListTest next) {
        this.next = next;
    }

    @Override
    public String toString() {
        return "LinkedListTest [data=" + data + ", next=" + next + "]";
    }

}

// Do a deep copy
private static LinkedListTest copyLlt(LinkedListTest original) {

    LinkedListTest copy = new LinkedListTest(original.getData() + " copied");

    LinkedListTest nextCopy = original.getNext();
    LinkedListTest current = copy;

    while (nextCopy != null) {

        LinkedListTest newCopy = new LinkedListTest(nextCopy.getData() + " copied");
        newCopy.setNext(nextCopy.getNext());

        current.setNext(newCopy);

        current = newCopy;
        nextCopy = newCopy.getNext();
    }

    return copy;
}

public static void main(String[] args) {

    LinkedListTest firstLlt = new LinkedListTest("First");
    LinkedListTest secondLlt = new LinkedListTest("Second");
    LinkedListTest thirdLlt = new LinkedListTest("Thrid");

    firstLlt.setNext(secondLlt);
    secondLlt.setNext(thirdLlt);

    LinkedListTest copiedLlt = copyLlt(firstLlt);

    // Data should say First, Second, Third
    System.out.println("Original LinkedListTest: " + firstLlt.toString());

    // Data should say First Copied, Second Copied, Third Copied
    System.out.println("Copied LinkedListTest: " + copiedLlt.toString());
}

}

【讨论】:

    【解决方案2】:

    在您的Clone 方法中,您永远不会为复制的内容分配next 字段。您需要这样做才能在副本中拥有多个连接的节点。此外,您还需要复制头部。此外,除头部副本外,不得使用任何内容覆盖copy:

    copy = new DictionaryNode(null, head.level);
    DictionaryNode temp =  head.next;
    DictionaryNode current = copy;
    
    while( temp != null) {
        DictionaryNode nn = new DictionaryNode( temp.word , temp.level);
        current.next = nn;
        current = nn;
        temp = temp.next;
    }
    

    【讨论】:

    • 但是 copy = new DictionaryNode(null, head.level);为什么用 null 而不是 word ?
    • @Anny:头部不只是第一个元素之前的一个元素吗?我不知道你名单的细节。如果这不是第一个元素之前的元素而是第一个元素本身,请注意避免NullPointerException。 (并使用head.word 而不是null)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-14
    • 2011-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-12
    相关资源
    最近更新 更多