2016-10-16 4 views
0

이 링크 된 목록을 점수순으로 표시하려면 어떻게해야합니까? GUI에서 최상위 점수를 기준으로 정렬 한 다음 하위 점수가 가장 낮은 점수를 얻으려고 할 때 필요합니다. 또한 항목을 10 개로 제한 할 수있는 방법이 있는지 궁금합니다. 도움을 주시면 감사하겠습니다! 감사.단일 링크 목록을 내림차순으로 정렬

public class ScoreList { 
private Player head; //reference to the head of the list 
private Player tail; //reference to the tail of the list 
int count; 


public ScoreList() { 
    count = 0; 
      head = null; 
      tail = null; 
} 

public int getCount() { 
    return count; 
} 

public int size() { 

    int count = 0; 
    Player p = head; 

    while(p != null) { 

     count++; 
     p = p.next; 
    } 

    return count; 
} 

//method to add an item to the list - append to the tail of the list 
public void add(String name, String score) { 
    // Create Player object 
    Player newPlayer = new Player(name, score); 
    if (head == null) { 
     head = newPlayer; 
     tail = head; 
     count++; 
    } 

    else { 
     //append to the end of the list 
     tail.setNext(newPlayer); 
     tail = newPlayer; 
     count++; 
    } 

    if(size() > 10) { 

     Player currentPlayer = head; 

     for (int i = 0; i < 9; i++) { 
      currentPlayer = currentPlayer.next; 
     } 
     currentPlayer.next = null; 
    } 
} 


// end add method 


//method to let the user get the data from a node in the list 

public String getItem(int index) { 
    String result = ""; 
    String name = ""; 
    String score = ""; 
    Player curName; 

    if (count > 0 && index == 0) { 
     //return the Player info at the head of the list 
     name = head.getName(); 
     score = head.getScore(); 
    } 

    else if (index > 0 && index < count) { 
     curName = head; 
     for (int i = 1; i <= index; i++) { 
      curName = curName.getNext(); 
     } 
     name = curName.getName(); 
     score = curName.getScore(); 
    } 

    result = "Player: " + name + " Score: " + score; 
    return result; 
} 


//nested inner class 
public class Player { 
     private String player; 
     private String score; 
     private Player next; 

    public Player() { 
     player = ""; 
     score = ""; 
     next = null; 
    } 

    public Player(String artist, String title) { 
     this.player = artist; 
     this.score = title; 
     next = null; 
    } 

    public String getName() { 
     return player; 
    } 

    public String getScore() { 
     return score; 
    } 

    public Player getNext() { 
     return next; 
    } 

    public void setArtist(String player) { 
     this.player = player; 
    } 

    public void setTitle(String score) { 
     this.score = score; 
    } 

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








} 


} 
+0

을하는 데 도움이됩니다. 방법에 대한 예는이 답변을 참조하십시오. http://stackoverflow.com/questions/11003155/change-priorityqueue-to-max-priorityqueue – kerryjj

답변

0

왜 점수를 문자열로 사용하고 있습니까?

나는 아래

정수

로 점수를 가정하고하면 플레이어의 점수 순에 LinkList를 정렬합니다 ScoreList 클래스에 포함 할 수 정렬 방법입니다.

  • 은 시간 복잡도 : O (nlogn)
  • 공간 복잡도 : O (n)이

희망이 당신이 최대 우선 순위 대기열이 필요 것처럼 소리

public void sort() { 
    Player runner = head; 
    Player[] arr = new Player[size()]; 
    int i = 0; 
    while (runner != null) { 
     arr[i++] = runner; 
     runner = runner.next; 
    } 

    Arrays.sort(arr, new Comparator<Player>() { 

     public int compare(Player o1, Player o2) { 
      if (Integer.parseInt(o1.getScore()) > Integer.parseInt(o2.getScore())) { 
       return -1; 
      } else if (Integer.parseInt(o1.getScore()) < Integer.parseInt(o2.getScore())) { 
       return 1; 
      } else { 
       return 0; 
      } 
     } 

    }); 

    for (int j = 0; j < arr.length - 1; j++) { 
     arr[j].setNext(arr[j + 1]); 

    } 
    if (arr.length > 0) { 
     arr[arr.length - 1].setNext(null); 
     head = arr[0]; 
     tail = arr[arr.length - 1]; 
    } 

} 
관련 문제