简体   繁体   中英

Sort Single Linked List in descending order

How would I display this linked list in desending order by Score? I need it when it when I display it in my GUI to sort by the highest score at the top, and then desending to the lowest score in the bottom? Also, I was wondering if there is a way to limit the entries to only 10. Any help would be appreciated! Thanks.

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;
    }








} 


} 

Why are you taking Score as a String?

I am assuming score as Integer

below is the sort method that you can include in ScoreList class, which will sort your LinkList in descending order of player score.

  • Time Complexity : O(nlogn)
  • Space COmplexity: O(n)

Hope this helps

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];
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM