2017-06-03 38 views
0

我試圖用關鍵字(優先級編號,int編號)從高到低排序鏈接列表,用文本文件中的數據排序。我正在插入物品並對其進行分類。 這是我的文本文件。鏈接列表,從文本文件中讀取,並按鍵排序

Myjob 66

垃圾17

玩轉25

重要96

命門99

MoreFun 28

工作69

分配44

這裏是我的鏈表類

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.PrintStream; 


@SuppressWarnings("unused") 
public class LinkedList { 
    Node head; 
    Node prev; 
    Node cur; 
    Node node; 

public LinkedList(){ 

} 

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

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

//insert method 
public void insert(Data dt){ 
    try { 
     if(head==null){ 
      head = new Node(dt.getData(), null); 
      System.out.println("Opps"); 
      } 
     else if (dt.getData().num > head.dt.num){ 
      head = new Node(dt.getData(),head); 
      System.out.println("Was Here");} 
     else if (head.getNext()==null){ 
      head.setNext(new Node(dt.getData(),null)); 
      } 
     else{ 
      cur = head.getNext(); 
      for(prev = head;cur!= null;cur=cur.getNext()){ 
       if(cur.dt.num>prev.dt.num){ 
        Node temp = new Node(dt.getData(),cur); 
        prev.setNext(temp); 
        break; 
       } 
       else{ 
        prev=cur; 

       } 
      } 
     } 
    } 
     catch (NullPointerException e){ 
      System.out.println("Here is Error"); 
     } 


} 



//Running Linked list 
public static void main(String[] args) throws FileNotFoundException{ 

     LinkedList l1 = new LinkedList(); 
     Data dt = new Data(); 

     l1.insert(dt); 


} 

}

下面是我的節點類:

public class Node { 
Data dt; 
Node next; 


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


public Node getNext(){ 
    return next; 
} 

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


public Data getData(){ 
    return dt.getData(); 
} 




} 

這裏是我的數據類:

import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.Scanner; 

public class Data { 

protected String name; 
protected int num; 

public Data(){ 
} 

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

//getter to get Data 
@SuppressWarnings("unused") 
public Data getData(){ 
     try{ 
      int count =0; 
      File x = new File("Asg2Data.txt"); 
      Scanner sc = new Scanner(x); 
      while (sc.hasNext()){ 
       String name = sc.next(); 
       int num = sc.nextInt(); 
       Data data= new Data(name,num); 
       System.out.println("Name = "+ name+"   "+"Priority = "+num); 
       System.out.println(" "); 
       count++; 
      } 
      System.out.println("Total Objects: "+count); 
      sc.close(); 
     }catch(FileNotFoundException e){ 
      System.out.println("Error"); 
     } 
     return this; 
} 

//Compare Function 
// public int compare(Data dt){ 
//  if (this.getData().num > dt.getData().num) 
//   return 1; 
//  else if (this.getData().num < dt.getData().num) 
//   return -1; 
//  else 
//   return 0; 
// } 


// Testing: Worked !!!!!  
    public static void main(String[] args){ 
     Data d1 = new Data(); 
     d1.getData(); 
    } 

} 

我目前輸出,當我從鏈表類運行:

Name = Myjob   Priority = 66 

Name = Junk   Priority = 17 

Name = Fun   Priority = 25 

Name = Important   Priority = 96 

Name = Vital   Priority = 99 

Name = MoreFun   Priority = 28 

Name = Work   Priority = 69 

Name = Assignment   Priority = 44 

Total Objects: 8 
Opps 

我的插入方法總是停在第一個if語句,永遠保持下去。我不知道如何解決它。同時,它不能執行插入數據並按優先級從高到低排序它們。誰能幫我 ?我是Java的超級新手。非常感謝。

-Dustin

回答

0

首先第一件事情你LinkedList的是沒有得到初始化,是因爲在你Data->getData()方法所創建的數據對象,但從來沒有將其分配到頭部和下LinkedList的頭。

由於您剛剛接觸Java,只想給您一些關於Java編碼的提示。

  • 除非最重要,否則不要使用壓制警告。大多數時候你可以處理它們。
  • 儘可能地分離代碼。這將幫助您和其他人瞭解代碼。防爆。爲什麼你的Data類有初始化代碼,它應該在LinkedList中。數據只是一個表示類,它將充當您數據的模型(這可能是設計考慮因素,您可能有理由這麼做......只是說)
  • 您也可以通過標識符一次以及如何使用他們。

我不會給你完整的代碼(因爲那樣你就沒有什麼可以做的),但下面的代碼片段會讓你開始。我試圖儘可能使用你的代碼,以便你更容易理解。下面的代碼將使用所需的數據初始化LinkedList,也許你可以從那裏注意。您可以繼續問任何問題,如果你面對任何問題,前進

`

import java.io.File; 
import java.util.Scanner; 

public class LinkedList 
    { 
     Node head; 

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

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

     private 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.setNextNode(n); 
         currentNode = n; 
        } 
       } 
       sc.close(); 
      } 
      catch (Exception ex) 
      { 
       ex.printStackTrace(); 
      } 
     } 

    } 

    class Node 
    { 

     private Data data; 

     private Node nextNode; 

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

     public Data getData() 
     { 
      return data; 
     } 

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

     public Node getNextNode() 
     { 
      return nextNode; 
     } 

     public void setNextNode(Node nextNode) 
     { 
      this.nextNode = nextNode; 
     } 

     @Override 
     public String toString() 
     { 
      return "Node [data=" + data + ", nextNode=" + nextNode + "]"; 
     } 



    } 

    class Data 
    { 

     private String name; 

     private int data; 

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

     public String getName() 
     { 
      return name; 
     } 

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

     public int getData() 
     { 
      return data; 
     } 

     public void setData(int data) 
     { 
      this.data = data; 
     } 

     @Override 
     public String toString() 
     { 
      return "Data [name=" + name + ", data=" + data + "]"; 
     } 
    } 

`

+0

你的建議太棒了。我只是修復我的代碼。現在它運作良好。現在我仍然在分揀部分。我在做插入排序。我的代碼就在 –

+0

以下你的建議太棒了。我只是修復我的代碼。現在它運作良好。現在我仍然在分揀部分。我在做插入排序。我的代碼就在@ utkarsh31下面 –

0

你的建議是如此真棒。我只是修復我的代碼。現在它運作良好。現在我仍然在分揀部分。我在做插入排序。這是我的代碼。

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; 
     } 
     } 
     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 dipslay下降到17?我重新安排了文本文件中的順序,最高的是第一個。那麼它可以完成從99到17的排序。但按照原始順序,我的程序只執行從66到最低的排序。我不知道爲什麼,以及如何解決它?非常感謝。

相關問題