2014-02-07 47 views
0

試圖在下面的程序中實現single-linked-list,我真的無法解除和如何在鏈接列表中添加節點(對於開始,m試着在空鏈接列表)。在鏈表中添加節點

說得樸素簡單,我試圖setDatasetNextgetSizeofList()回報0每次....它真的看起來像一個火箭科學對我來說現在!

問:一些人能告訴我如何實現它....或者說,節點添加到現有的鏈表....

我至今嘗試過,爲什麼他們dint解決了:我引用了多個程序,但它們太複雜了,我不明白(火箭科學),所以在下面的程序中寫下了我從算法中理解的內容....但即使在算法中,他們也只是展示如何實施,這是我失敗的地方,因爲,我不明白,什麼data-typevalue是通過添加節點...


請不在於m不是Java的傢伙,所以請去容易,這個問題就會出現,試圖瞭解

package Data_S; 

public class Linked_List { 

    private int data; 
    private Linked_List next_ptr; 
    private Linked_List headNode = null; 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     // TODO Auto-generated method stub 

     Linked_List ll = new Linked_List(); 
     //ll.setnext(25); 
     ll.insert_node(24); 
     ll.traverse(); 
     ll.getSizeofList(); 
    } 


    //size of list 
    public void getSizeofList() 
    { 
     int l = 0; 
     Linked_List curr = headNode; 
     while(curr != null) 
     { 
      l++; 
      curr = curr.getnext(); 
     } 
     System.out.print("Size of list is = "+l); 
    } 

    //insert node 
    public void insert_node(/*Linked_List node, */int data) 
    { 
     if(headNode == null) 
     { 
      System.out.println("in insert"); // checking 
      this.setnext(headNode); 
      this.setData(data); 
      System.out.print("value = "+this.getData()); 
     } 
    } 

    //set data for this node 
    public void setData(int data) 
    { 
     this.data = data; 
    } 

    //return the data 
    public int getData() 
    { 
     return this.data; 
    } 

    //set next pointer 
    public void setnext(Linked_List next_ptr) 
    { 
     this.next_ptr = next_ptr; 
    } 

    //get next pointer 
    public Linked_List getnext() 
    { 
     return this.next_ptr; 
    } 


} 
+0

這是很好的,你想學習,但也有數以百計的描述上鍊表的書籍和網站。你對插入的工作原理不瞭解麼? – Joni

+0

@Joni:我無法弄清楚它是如何分配在內存中的......就像'數組',它們很簡單,因爲它們沒有'next'和'fwd'指針只有一個連續的塊... ..回答你的問題,我不明白如何一塊數據可以有'指針'類的東西在Java中,以便它可以連接.....我只是想了解,這是所謂的'先前/ next'的指針在內存中,它們是如何工作的! – NoobEditor

+1

@NoobEditor:數據分配在與其他數據無關的獨立位置。對於每個節點,爲3個指針分配足夠的空間,這些空間指向內存中包含前一個節點,下一個節點和當前節點數據的3個抽象點。 –

回答

1

你必須做一個鏈表的單鏈(節點)之間的區別,整個容器(鏈表)。

public class LinkedList { 
    Node head; 
    int size; // Maybe 

    public void insertAtEnd(int data) { 
     Node previous = null; 
     for (Node current = head; current != null; current = current.next) { 
      previous = current; 
     } 
     Node baby = new Node(data); 
     if (previous == null) { 
      head = baby; 
     } else { 
      previous.next = baby; 
     } 
     ++size; 
    } 

    public void insertInSortedList(int data) { 
     Node previous = null; 
     Node current = null; 
     for (current = head; current != null && data < current.data; 
       current = current.next) { 
      previous = current; 
     } 
     Node baby = new Node(data); 
     baby.next = current; 
     if (previous == null) { 
      head = baby; 
     } else { 
      previous.next = baby; 
     } 
     ++size; 
    } 
} 

class Node { 
    int data; 
    Node next; 
    Node(int data) { 
     this.data = data; 
    } 
} 

人們可能有時會看到封裝爲:

public class LinkedList { 
    private static class Node { 
    } 
    ... 
} 
+0

+1:哇....'你必須區分單鏈'清除幾乎每一個疑問....非常感謝,答案接受! :) – NoobEditor

0

你從未設置headnode。在insertnode中,您只需使用setnext,它不會設置headnode。您將頂級類和節點實現混合在一起。

下面是如何在Java中實現進一步的參考鏈接列表的示例: How do I create a Linked List Data Structure in Java?

+0

我看到了那個引用鏈接和被接受的答案......但是在這個答案中的後續評論讓我懷疑! :\ – NoobEditor