2016-10-08 10 views
0

DictionaryNode으로 알려진 연결된 목록의 전체 복사본을 만들려고했으나 항상 null이므로 표시 방법에 내용을 표시 할 수 없었습니다. DictinaryNode temp가 항상 null 인 이유는 무엇입니까? 그리고 만약 temp = head work를 지정하려고 시도하지만 temp = copy는 지정하지 마십시오. 당신의 Clone 방법에서LinkedList 딥 (deep) copy 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

'head'에 값을 할당하지 마십시오. 또한, 귀하의 복제 방법은 복제 된 사전에 대한 참조를 반환해야한다고 생각합니다. –

+0

머리글은 파일에서 읽는 사용자를 통해 초기화되지만 문제가 있다면 머리글의 임시 참조를 지정하면 모든 내용이 표시되지만 임시 파일을 복사하면 작동하지 않습니다 – Anny

+0

그렇다면 게시 한 코드가 isn이 아닙니다. 네가 실제로하고있는 일이 아니야. –

답변

0

당신은 복사 된 내용에 대한 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; 
} 
+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()); 
} 

} 
관련 문제