2015-11-28 62 views
-1

我不明白爲什麼printList()在調用時無限循環。我試圖編寫一個堆棧鏈表並打印列表,而不使用java中的內置堆棧方法。爲什麼我的打印方法無限循環,我該如何糾正這個問題?堆棧推送方法無限循環Java

public class LinkedListStack{ 
    private String item; 
    private Node next; 
    private Node top = null; 

    public LinkedListStack(){ 
    } 

    public void push(String item){ 
     top = new Node(item, top); 
    } 

    public void printList(){ 
     Node currentNode = top; 
     for(currentNode = top; currentNode.getItem()!= null; currentNode = currentNode.getNext()){ 
      System.out.println(currentNode.getItem()); 
     } 
    } 

    public class Node{ 
     public Node(String newItem, Node nextNode){ 
      item = newItem; 
      next = nextNode; 
     } 

     public Node(String newItem){ 
      item = newItem; 
      next = null; 
     } 

     //to set the value of the next field 
     public void setNext(Node nextNode){ 
      next = nextNode; 
     } 

     //read the value of the next field 
     public Node getNext(){ 
      return(next); 
     } 

     //to set the value of the item field 
     public String setItem(String newItem){ 
      item = newItem; 
      return(item); 
     } 

     //read the value of the item field 
     public String getItem(){ 
      return(item); 
     } 
    } 

    public static void main(String args[]){ 
     LinkedListStack newList = new LinkedListStack(); 
     newList.push("hello"); 
     newList.push("goodbye"); 
     newList.printList(); 
    } 
} 
+0

爲什麼你在循環之前申報的printList「currentNode」(),如果你要創建一個本地變量,在環頭相同的名稱? – Shondeslitch

+0

您是否嘗試過調試? – meriton

回答

1

問題是itemnextLinkedListStack領域和所有Node實例之間共享。當您創建另一個Node並設置該項目時,您將更改全部節點。要解決它,只需將字段聲明移至Node內部類。 除此之外,printList方法中的循環條件是錯誤的:下一個節點爲空,而不是其項目。 這裏是一個工作示例:

public class LinkedListStack { 
    private Node top = null; 

    public LinkedListStack() { 
    } 

    public void push(final String item) { 
     top = new Node(item, top); 
    } 

    public void printList() { 
     Node currentNode = top; 
     for (currentNode = top; currentNode != null; currentNode = currentNode.getNext()) { 
      System.out.println(currentNode.getItem()); 
     } 
    } 

    public class Node { 
     private String item; 
     private Node next; 

     public Node(final String newItem, final Node nextNode) { 
      item = newItem; 
      next = nextNode; 
     } 

     public Node(final String newItem) { 
      item = newItem; 
      next = null; 
     } 

     // to set the value of the next field 
     public void setNext(final Node nextNode) { 
      next = nextNode; 
     } 

     // read the value of the next field 
     public Node getNext() { 
      return next; 
     } 

     // to set the value of the item field 
     public String setItem(final String newItem) { 
      item = newItem; 
      return item; 
     } 

     // read the value of the item field 
     public String getItem() { 
      return item; 
     } 
    } 

    public static void main(final String args[]) { 
     final LinkedListStack newList = new LinkedListStack(); 
     newList.push("hello"); 
     newList.push("goodbye"); 
     newList.printList(); 
    } 
} 
+0

非常感謝您的幫助! – Ashley