首页 > 解决方案 > Linked List 中的 remove() 方法删除最低分

问题描述

如果得分高,我希望 GameEntry 得分在添加到 GameEntry 后删除最低得分。我设置的最大条目是 10。如果超过 10,它应该删除最低分数并替换索引。我尝试使用 remove() 方法在 main 中显示。这里我使用单链表。请帮助查看和反馈我错过的地方。谢谢。


public class ScoresSingleLL {

private GameEntry head; //head node to hold the list

    //It Contains a static inner class Game Entry
    private static class GameEntry{
        private int score;
        private GameEntry next;
        
        public GameEntry(int score) {
            this.score = score;
            this.next = null;
        }
        
        public int getScore() {
        return score;
        }

    }
    
    private static final int highestScoregames = 10;
    private int totalAdded = 0;
    private GameEntry[] games = new GameEntry[highestScoregames];
    
    
    public void add(GameEntry game) {
        game.next = head;
        head = game;
        int score = game.getScore();
        if (totalAdded == highestScoregames) {
        if (score <= games[totalAdded - 1].getScore()) {
        return;
        }
        } else {
        totalAdded++;
        }

        int i = totalAdded - 1;
        for (; (i >= 1) &&(score > games[i -1].getScore()); i--) {
        games[i] =games[i - 1];
        }
        games[i] = game;
        
        }
    
    public void remove(int i) throws IndexOutOfBoundsException {

        GameEntry game = head;
        games[i] = game;
        
        if ((i < 0) || (i >= totalAdded)) { //i is score where less than total added score
        throw new IndexOutOfBoundsException("Invalid index: " + i);
        }
                
        
        for (int j = i; j < totalAdded -1; j++) {
        games[j] = games[j + 1];
        }

        games[totalAdded - 1] = null;
        totalAdded--;
            
        }

    
    
        
    public void display() {

        GameEntry current = head;
        while(current != null){
            System.out.print("Game Entry [Score = "+ current.score +"]" + "  ");
            current = current.next;             
        }
        System.out.println("null");
    }


    public static void main(String[] args) {
        
        
        
        GameEntry game1 = new GameEntry(1);
        GameEntry game2 = new GameEntry(2);
        GameEntry game3 = new GameEntry(3);
        GameEntry game4 = new GameEntry(4);
        GameEntry game5 = new GameEntry(5);
        GameEntry game6 = new GameEntry(6);
        GameEntry game7 = new GameEntry(7);
        GameEntry game8 = new GameEntry(8);
        GameEntry game9 = new GameEntry(9);
        GameEntry game10 = new GameEntry(10);
        GameEntry game11 = new GameEntry(11);
        
        ScoresSingleLL scores = new ScoresSingleLL();
        
        scores.add(game1);
        scores.add(game2);
        scores.add(game3);
        scores.add(game4);
        scores.add(game5);
        scores.add(game6);
        scores.add(game7);
        scores.add(game8);
        scores.add(game9);
        scores.add(game10); 
        
        scores.display();   
        scores.add(game11);
        scores.remove(0);
        scores.display();
        

    }

}

标签: javalinked-list

解决方案


您的代码将对象维护为链表数组。这是矫枉过正。您的代码中有几个实例不能保持同步。您应该只选择其中一种数据结构。

由于您的问题是关于链表的,我建议删除数组。你也不需要totalAdded.

这是两个函数的更正代码。该add功能还将确保列表不会变得太长。发生这种情况时,它将在最后删除项目:

    private static final int highestScoregames = 10;
    
    public void add(GameEntry game) {
        if (highestScoregames == 0) { // Nothing allowed
             head = null;
             return;
        }
        if (head == null) {
            head = game;
            return;
        }
        int score = game.getScore();
        int count = 1;
        GameEntry current = head;
        if (score >= head.getScore()) {
            game.next = head;
            current = head = game;
        } else {
            while (current.next != null && current.next.getScore() > score) {
                current = current.next;
                count++;
            }
            // Insert game here
            game.next = current.next;
            current.next = game;
        }
        while (current.next != null && count < highestScoregames) {
            current = current.next;
            count++;
        }
        // Clip the rest of the list
        current.next = null;
    }
    
    public void remove(int i) throws IndexOutOfBoundsException {
        if (i < 0 || head == null) {
            throw new IndexOutOfBoundsException("Invalid index: " + i);
        } 
        if (i == 0) {
            head = head.next;
            return;
        }
        GameEntry game = head;
        for (int j = 1; j < i; j++) {
            game = game.next;
            if (game == null || game.next == null) {
                throw new IndexOutOfBoundsException("Invalid index: " + i);
            }
        }
        // Remove game.next
        game.next = game.next.next;
    }

推荐阅读