2014-01-10 34 views
0

我剛創建了自定義節點類並創建了鏈接列表,但是當我在列表中打印數據時,數據會向後打印。我以爲我將指針設置正確,並在列表的開頭添加新節點。但顯然編譯器認爲不然。我花了一段時間才終於理解了指針,但我想我並沒有像我想的那樣理解。自定義鏈接列表數據打印後語

public class Node { 

public String data; 
public String cat; 
public Node next; 

public Node (String data, String cat, Node next){ 
    this.data=data; 
    this.cat=cat; 
    this.next=next; 
} 

public Node() { 

} 
public String toString() 
{ 
    return "Patient: " + data + " Category: " +cat ; 
} 

}


public class Main { 

public static Node head; 
public static void main (String args []) 
{ 
    add("P1" , "2"); 
    add("P2", "3"); 
    add("P3", "4"); 
    add("P4", "4"); 
printList(); 
} 

// add data to nodes 

public static void add(String d, String c) 
{ 
    Node temp = null; 

    if (head == null) 
    { 
     head = new Node(d, c, null); 
    } 
    else 
    { 
     temp=head; 
     head= new Node(d, c, temp); 
    } 

} 
// print node data 

public static void printList() 
{ 
    while (head != null) 
    { 
     System.out.println(head); 
     head=head.next;  
    } 
} 

}

+0

您需要從尾指針添加到列表中。如果只使用頭指針,它將表現得像一個堆棧。 – vandale

回答

0

你的列表向後打印出來,因爲以後創建節點放置在列表的頭部,而不是在後面。 I.E.

head -> Node1 

成爲

head -> Node2 -> Node1 

然後

head -> Node3 -> Node2 -> Node1 

你在printList迭代是好的。在add中,不是將新的Node置於首位,而是需要找到最後的Node(其中nextnull),並將新的Node置於那裏。即

head -> Node1 

成爲

head -> Node1 -> Node2 

更改else(用於當列表不爲空)到:

else 
{ 
    temp = head; 
    // Get last item 
    while (temp.next != null) 
    { 
     temp = temp.next; 
    } 
    // Point old last item to *new* last item 
    temp.next = new Node(d, c, null); 
} 
+0

我以爲多數民衆贊成我在做什麼... – bimm3rBoy

+0

@ bimm3rBoy我添加了代碼到我的答案添加到列表的末尾。 – rgettman

+0

謝謝,我終於明白了! – bimm3rBoy

0

rgettman是正確的,當你一個元素添加到鏈表你在第一個位置添加

//element1->null 
//add(element2) 
//element2->element1->null 

你可以做一個迭代搜索無效,並在最後一個位置插入,這樣的事情

public static void add(String d, String c) 
{ 
    Node temp = null; 

    if (head == null) 
    { 
     head = new Node(d, c, null); 
    } 
    else 
    { 
     temp=head; 
     while(temp!=null){ //search last element 
      temp=temp.next(); 
     } 
     temp.next= new Node(d, c, null); //the new element it's after the last 
    } 

} 

你也可以創建一個名爲節點lastNode變量,並保存在這裏的最後一個節點,這樣你就不必循環,更高效的算法。是這樣的:

public class Node { 
    public Node lastNode; 
... 
} 

和主類...

public static void add(String d, String c) 

{ 
    Node temp = null; 

    if (head == null) 
    { 
     head = new Node(d, c, null); 
     lastNode=head; 
    } 
    else 
    { 
     Node newnode= new Node(d,c,null); 
     lastNode.next=newnode; 
     lastNode= newnode; 
    } 

} 
0

您所描述的行爲實際上是一個LinkedList究竟是如何預期工作。像rgettman指出的那樣,當您在LinkedList上使用add方法時,您將添加到列表的頭部。問題是,當你通過設置head = head.next從LinkedList中移除時,你也可以從頭部移除。

如果還不清楚,請查看此動畫:http://www.cs.usfca.edu/~galles/visualization/StackLL.html嘗試向堆疊中推入幾個整數,然後彈出它們。這就是你的鏈表是如何工作的。

解決此問題的一種方法是將所有值粘在一起,以便在打印之前將它們放在一起。

public static void printList() 
{ 
    String toPrint = ""; 
    while (head != null) 
    { 
     toPrint = head + "\n" + toPrint; 
     head = head.next;  
    } 
    System.out.println(toPrint); 
}