LinkedList深拷贝java

时间:2016-10-08 09:01:49

标签: java linked-list clone

我正在尝试执行我所做的DictionaryNode链接列表的深层复制,但我无法在display方法中显示它的内容,因为它始终为null。为什么DictinaryNode temp总是为空?如果我尝试指定temp = head work但是使用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;
    }
}

2 个答案:

答案 0 :(得分: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());
}

}

答案 1 :(得分:0)

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;
}