LinkedList Deep copy java



我试图做我的链表称为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;
    }
}

这个程序将演示如何对列表进行深度复制。它比你的具体例子更通用,所以希望它也能帮助别人。

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

在您的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;
}

相关内容

  • 没有找到相关文章

最新更新