2017-06-04 44 views
1

我正在做我的鏈接列表插入排序,我遇到了一個問題。InsertionSort鏈接列表,與來自文本文件的數據。 Java

我的數據文件:

Myjob 66 
Junk 17 
Fun 25 
Vital  99 
Important 96 
MoreFun 28 
Work  69 
Assignment 44 

這裏是我的插入代碼排序

public Node insertSort(Node node){ 
    Node sortedNode = null; 
    while(node != null){ 
    Node current = node; 
    node = node.next; 
    Node x; 
    Node previous = null; 
    for(x = sortedNode; x != null; x = x.next){ 
     if(current.getData().getNum() > x.getData().getNum()){ 
       break; 
     } 
     previous = x; 
    } 
    if(previous == null){    
      current.next = sortedNode; 
      sortedNode = current; 
    } 
    else{    
     current.next = previous.next; 
     previous.next = current; 
    } 
    } 
    sortedNode=head; 
    return sortedNode; } 

我目前輸出:

Name = Myjob   Priority=66 
Name = Assignment   Priority=44 
Name = MoreFun   Priority=28 
Name = Fun   Priority=25 
Name = Junk   Priority=17 
null 

他們跳過任何超過66更大千萬人有任何想法如何解決這個問題,所以它可以顯示從99下降到17?我嘗試重新排列文本文件中的順序,最先排在第一位。那麼程序可以完美地從99回到17。但是按照原始順序,我的程序只能執行從66到最低的排序。我不知道爲什麼,以及如何解決它?請幫忙。我在Java中很新。非常感謝。

我的數據類

public class Data { 

private String name; 
private int num; 

public Data(){ 
} 

public Data(String name,int num){ 
    this.name=name; 
    this.num=num; 
} 

public String getName(){ 
    return name; 
} 

public void setName(String name){ 
    this.name=name; 
} 

public int getNum(){ 
    return num; 
} 

public void setNume(int num){ 
    this.num=num; 
} 

public String toString() 
{ 
    return "Name = " + name + "   Priority=" + num ; 
} 

} 

這裏是我的LinkedList類:

public class LinkedList { 
Node head; 
Node prev; 
Node cur; 


public LinkedList(){ 

} 

public LinkedList(Node head){ 
    head = null; 
} 

//getter to get head 
public Node gethead(){ 
    return head; 
} 

public void printLinkedList(){ 
    System.out.println(head); 
} 

public void initializeLL(){ 
    Node currentNode = head; 
    try 
    { 
     File file = new File("Asg2Data.txt"); 
     Scanner sc = new Scanner(file); 
     while (sc.hasNext()) 
     { 
      Data d = new Data(sc.next(), sc.nextInt()); 
      Node n = new Node(d); 

      if(currentNode == null){ 
       currentNode = n; 
       head = n; 
      }else{ 
       currentNode.setNext(n); 
       currentNode = n; 
      } 
     } 
     sc.close(); 
    } 
    catch (Exception ex) 
    { 
     ex.printStackTrace(); 
    } 
} 

public static void main(String[] args) throws FileNotFoundException{ 

     LinkedList l1 = new LinkedList(); 
     l1.initializeLL(); 
     l1.insertSort(l1.head); 
     l1.printLinkedList(); 
     System.out.println("************"); 
} 
} 

這裏是我的節點類:

public class Node{ 
Data dt; 
Node next; 

public Node(){ 

} 
public Node(Data dt){ 
    this.dt=dt; 
} 


public Node getNext(){ 
    return next; 
} 

public void setNext(Node next){ 
    this.next=next; 
} 


public Data getData(){ 
    return dt; 
} 

public void setData(Data dt){ 
    this.dt=dt; 
} 


public String toString() 
{ 
    StringBuilder sb = new StringBuilder(); 
    sb.append(dt).append(System.getProperty("line.separator")); 
    sb.append(next).append(System.getProperty("line.separator")); 

    return sb.toString(); 

} 
} 

的插入排序方法是LinkedList類裏面。

+0

好像你的問題發生在你需要添加一個項目到列表的開頭。 – nhouser9

+0

我也找到了。但我不知道如何修復我的代碼。你可以幫我嗎 ? –

+0

@ nhouser9我剛添加了我的其他代碼。請看看和幫助。非常感謝。 –

回答

0

您確定此處的代碼是正確的嗎? 我跟蹤了你的代碼,我發現for循環並沒有運行,因爲每次運行insertSort()函數時x總是空的。

如何調用insertSort()函數對數據進行排序?

編輯

確定。我審查你的代碼。我發現你的排序是正確的,但是鏈接列表的排序功能頭沒有更新。所以你的代碼打印舊頭的排序列表。 你應該在排序函數後更新鏈表的頭部。

+0

**你確定你的代碼在這裏是正確的嗎?**實際上他不是,加上你的回答作爲一個答案是質疑,而不是基於解決方案的本質,試着提出一個解決方案是我的意思 – ShayHaned

+0

@ hadi.mansouri我只是在上面添加了我的其他代碼。請看看和幫助。非常感謝。 –

+0

@ShayHaned我剛剛添加了我的代碼。請看看和幫助。非常感謝。 –

0

好吧所以下面的代碼片段這裏是我的代碼插入排序正在作出一些嚴重的解決方法,我猜即使你將無法正確想象:)。

嘗試將問題分解爲更簡單的步驟,您將如何去實現一個LinkedList,您可以按升序或降序形式添加一個Integer(實際上,只是OOP的複雜性會變化)。我只是測試下面的代碼會幫助你ATLEAST看看你應該已經實施了LinkedList的是通過插入排序生長算法中,注意,這是處理的升序排列的情況下

public class LinkedList 
{ 

    public Node first , last; 

    public class Node 
    { 
     public int data; 

     public Node next; 

     public Node(int data) 
     { 
      this.data = data; 
     } 
    } 

    public void addInsertSort(int num) 
    { 
     if(isEmpty()) 
     { 
      first = new Node(num); 
      last = first; 
     } 
     else 
     { 
      Node newNode = new Node(num); 
      Node curNode = first , preNode = null; 
      while(curNode != null && newNode.data > curNode.data) 
      { 
       preNode = curNode; 
       curNode = curNode.next; 
      } 
      if(curNode == first) 
      { 
       newNode.next = first; 
       first = newNode; 
      } 
      else 
      { 
       if(curNode != null && preNode != null) //middle of list 
       { 
        newNode.next = curNode; 
        preNode.next = newNode; 
       } 
       else if(curNode == null) // end of list 
       { 
        preNode.next = newNode; 
        last = newNode; 
       } 
      } 
     } 
    } 

    public boolean isEmpty() 
    { 
     return first == null; 
    } 

    public String toString() 
    { 
     String str = "["; 
     for(Node curNode = first; curNode != null; curNode = curNode.next) 
     { 
      str += curNode.data + " "; 
     } 
     return str + "]"; 
    } 

    public static void main(String[] args) 
    { 
     LinkedList list = new LinkedList(); 

     list.addInsertSort(4); 
     list.addInsertSort(5); 
     list.addInsertSort(1); 
     list.addInsertSort(6); 
     list.addInsertSort(10); 
     list.addInsertSort(0); 

     System.out.println(list); 
    } 

} 

對不起,這裏的代碼編輯器是真的殺死我,請不要介意頻繁的重新編輯

現在繼續如何將這個想法集成到您的方法中,您可以修復這樣的事情,並確保它看起來確切這種方式

// You really dont need more variables in your LinkedList class 
// Both of these are enough, forget currentNode reference 
public Node head , last; 

public void initializeLL(){ 
    try 
    { 
     File file = new File("Asg2Data.txt"); 
     Scanner sc = new Scanner(file); 
     while (sc.hasNext()) 
     { 
      Data d = new Data(sc.next(), sc.nextInt()); 
      Node n = new Node(d); 

      addInsertSort(n); 
     } 
     sc.close(); 
    } 
    catch (Exception ex) 
    { 
     ex.printStackTrace(); 
    } 
} 


public Node addInsertSort(Node newNode) 
{ 
    if(isEmpty()) 
    { 
     head = newNode; 
     last = head; 
    } 
    else 
    { 
     Node curNode = head , preNode = null; 
     // This is How InsertionSort would have gone to search the list 
     while(curNode != null && newNode.getData().getNum() > curNode.getData().getNum()) 
     { 
      preNode = curNode; 
      curNode = curNode.next; 
     } 
     //Thats the happy ending of the insertionSort algo 
     //Now we need to link newNode with OUR DYNAMICALLY GROWING LIST 
     //When the insertion Algo stopped, It told us where the newNode 
     //shall be placed in comparison to the current state of list 

     // Is this supposed to be the head node now?? 
     if(curNode == head) 
     { 
      newNode.next = head; 
      head = newNode; 
     } 
     else // maybe we landed BETWEEN the list or at the END OF LIST?? 
     { 
      if(curNode != null && preNode != null) //BETWEEN THE list 
      { 
       newNode.next = curNode; 
       preNode.next = newNode; 
      } 
      else if(curNode == null) // end of list 
      { 
       preNode.next = newNode; 
       last = newNode; 
      } 
     } 
    } 
    return newNode; 
} 

public boolean isEmpty() 
{ 
    return head == null; 
} 

讓我知道,如果事情還是會出錯,我會更深入地

+0

Awesomeeeeee。我的程序工作完美。非常感謝。 –

+0

非常感謝,兄弟。我的程序現在工作很完美。 –

+0

@DustinLe Alrighty !!玩得開心!! – ShayHaned

1

有一對夫婦的事情這裏發生瞭解釋。首先,您的代碼中有一些錯誤,如返回head,這些錯誤尚未更新。這可以很容易地解釋你所看到的行爲,通過列表中途開始迭代。

更重要的是,這似乎並不是我插入排序的實現。插入排序是通過在正確的點處插入已排序的列表完成的。所以看起來像:

sortedList Sort(unsortedlist): 
    sortedList = [] 
    foreach item in unsortedlist: 
    sortedList.InsertAtTheRighPlace(item) 
    return sortedList 
+0

謝謝so兄弟,你的方向很重要。 –