2016-10-08 20 views
0

DictionaryNodeというリンクリストのディープコピーを作成しようとしていましたが、常にnullであるため、表示方法でコンテンツを表示できませんでした。なぜDictinaryNodeのtempは常にnullですか?私はtemp = head workを割り当てようとしますが、temp = copyは割り当てません。あなたがコピーされたコンテンツのためnextフィールドを割り当てることはありませんあなたのClone方法でLinkedListディープコピーjava

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

「ヘッド」には決して値を割り当てません。また、私はあなたのクローンメソッドは、クローンの辞書への参照を返す必要がありますと思う。 –

+0

ヘッドはユーザーからのファイル読み込みで初期化されますが、ヘッドの温度参照を割り当ててすべてのコンテンツを表示しようとすると問題が発生しますが、テンポラリを試してみるとコピーは機能しません – Anny

+0

もしそうなら、あなたが実際に走っていることはありません。 –

答えて

0

。コピー内に複数のノードが接続されている場合は、これを行う必要があります。さらに、頭をコピーする必要があります。また、ヘッドのコピー以外のもので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; 
} 
+0

私は次の値が常にnullであった理由を得た – Anny

+0

しかし、copy = new DictionaryNode(null、head.level);なぜ単語の代わりにヌル? – Anny

+0

@Anny:頭は最初の要素の前の要素ではありませんか?私はあなたのリストの詳細を知らない。これが最初の要素の前の要素ではなく最初の要素自体である場合は、 'NullPointerException'を避けるように注意してください。 (そして、 'null'の代わりに' head.word'を使います) – fabian

0

このプログラムは、リスト上の深いコピーを行う方法を説明します。あなたの具体的な例よりも一般的ですので、うまくいけば他の人にも役立ちます。

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

} 
関連する問題