2016-12-06 127 views
2

我正在閱讀關於java中的鏈表,我編寫了這個代碼,當我運行它時,我得到了3作爲輸出,它應該打印出插入數字的反向順序如下:10 7 3 ..我的代碼有什麼問題?在java中實現一個鏈表在

public class List { 
    private class ListNode { 
     private int contents; 
     private ListNode next; 

     ListNode(int contents) { 
      this.contents = contents; 
      this.next = null; 
     } 
    } 

    private ListNode head; 

    public List() { 
     head = null; 
    } 

    public void insert(int contents) { 

     ListNode newNode = new ListNode(contents); 
     if (head == null) { 
      head = newNode; 
     } 

     ListNode current = head; 
     while (current != null) { 
      current = current.next; 
     } 

     current = newNode; 

    } 

    public void reverse() { 

     ListNode current = head; 
     ListNode nodeNext = current.next; 

     while (nodeNext != null) { 
      current.next = null; 
      nodeNext.next = current; 
      current = nodeNext; 
     } 

     head = current; 
    } 

    public String toString() { 

     String s = ""; 
     ListNode current = head; 
     while (current != null) { 
      s = current.contents + s; 
      current = current.next; 
     } 

     return s; 
    } 

    public static void main(String[] args) { 

     List l = new List(); 
     l.insert(3); 
     l.insert(7); 
     l.insert(10); 
     l.reverse(); 
     System.out.println(l.toString()); 

    } 
} 

感謝

+1

你從來沒有設置'插入時next'值。使用調試器,您很快就會發現這一點。 – jhamon

+0

你的意思是這樣的'public void insert(int contents,ListNode next){' –

+0

你是怎麼計劃這個的?你有沒有寫出你的想法,或者你先在紙上做過這些? – AxelH

回答

1
private ListNode head; 
private ListNode tail; 
public void insert(int contents) { 

    ListNode newNode = new ListNode(contents); 
    if (head == null) { 
     head = newNode; 
     tail = newNode; 
     return; 
    } 
    tail.next = newNode; 
    tail = newNode; 
} 

保持爲O(1)插入一個尾節點參考。

reverse()方法是有點不對:

// this loop will run forever if there are more than 1 nodes 
while (nodeNext != null) { 
    current.next = null; 
    nodeNext.next = current; // you lose the reference to the entire list here 
    current = nodeNext; 
} 

重寫功能:

public void reverse() { 
    ListNode cursor = head; 
    ListNode newHead = null; 

    while (cursor != null) { 
     ListNode next = cursor.next; 
     cursor.next = newHead; 
     newHead = cursor; 
     cursor = next; 
    } 
    head = newHead; 
} 

cursor.next = newHead,失去原有的參考cursor.next。所以你需要採取臨時變量的cursor.next參考:ListNode next = cursor.next;

打印功能

public void print() { 
    ListNode cursor = head; 
    while (cursor != null) { 
     System.out.println(cursor.contents); 
     cursor = cursor.next; 
    } 
} 
+0

謝謝..爲什麼當我寫'System.out.println(l。reverse());'在主要方法eclipse中說:'PrintStream類型中的方法println(boolean)不適用於參數(void)' –

+0

@senshinakamora因爲我寫的反向方法不會返回任何東西。該類型是'void'。您正將'void'類型(不是類型)傳遞給'println()',並且PrintStream中沒有任何方法需要void參數。 Eclipse採用了第一種使用布爾值的println,因此它給出了錯誤消息 – rafid059

+0

我已經添加了打印功能。調用'l.reverse()',然後調用'l.print()'。 – rafid059

4

insert法新節點無法連接到現有列表的最後一個節點。你必須到新節點分配給現有列表的最後一個節點的node.next

public void insert(int contents) { 

    ListNode newNode = new ListNode(contents); 
    if (head == null) { 
     head = newNode; 
     return; 
    } 

    ListNode current = head; 
    while (current.next != null) { 
     current = current.next; 
    } 

    current.next = newNode; 

} 
+1

:'空指針訪問:變量當前只能爲null在這個位置'代碼不起作用謝謝 –

+1

@senshinakamora你有沒有注意到我把循環的條件改成'while(current.next!= null)'? – Eran

+0

不,我沒有抱歉..現在當我運行它,我沒有看到任何東西在輸出中..有什麼問題,我的'toString'方法?謝謝 –