Java十大玩家链接列表

Java十大玩家链接列表,java,linked-list,Java,Linked List,我正在为一个作业设计的课程寻求帮助。它将游戏分数添加到一个链接列表中,并从最高到最低列出。分数最多为10分。我几乎可以用了,但我想不出什么。我加上第一个分数就行了,如果我加上第二个分数,只有当这个分数高于第一个分数时才行。如果不是,则抛出一个java.lang.NullPointerException。有人能看看我的insert(String name,int score)方法并告诉我问题出在哪里吗 public class GamerList { /** * The nod

我正在为一个作业设计的课程寻求帮助。它将游戏分数添加到一个链接列表中,并从最高到最低列出。分数最多为10分。我几乎可以用了,但我想不出什么。我加上第一个分数就行了,如果我加上第二个分数,只有当这个分数高于第一个分数时才行。如果不是,则抛出一个
java.lang.NullPointerException
。有人能看看我的
insert(String name,int score)
方法并告诉我问题出在哪里吗

public class GamerList {

    /**
     * The node class stores a list element and a reference to the next node.
     * @author johnmckillip
     *
     */

    private class Node {

        String name;
        int score;
        Node next;

        /**
         * Constructor.
         * @param val The element to store in the node.
         * @param n The reference to the successor node.
         */
        Node(String val1, int val2, Node n) {

            name = val1;
            score = val2;
            next = n;
        }

        /**
         * Constructor.
         * @param val The element to store in the node.
         */
        Node(String val1, int val2) {

            this(val1, val2, null);
        }
    }

    private Node head;
    private Node tail;

    /**
     * Constructor.
     */

    public GamerList() {

        head = null;
        tail = null;
    }

    /**
     * The isEmpty method checks to see if the list is empty.
     * @return true if the list is empty, false otherwise.
     */
    public boolean isEmpty() {

        return head == null;
    }

    /**
     * The size method returns the length of the list.
     * @return The number of elements in the list.
     */
    public int size() {

        int count = 0;
        Node p = head;

        while(p != null) {

            count++;
            p = p.next;
        }

        return count;
    }

    public void insert(String name, int score) {

        Node node = new Node(name, score);

        if(isEmpty()) {

            head = node;
            tail = node;
        }

        else if(head.score <= node.score) {

            node.next = head;
            head = node;
        }

        else {

            Node frontPtr = head.next;
            Node backPtr = head;

            while(frontPtr.score > node.score && frontPtr.next != null) {

                backPtr = backPtr.next;
                frontPtr = frontPtr.next;
            }

            if(frontPtr != null && frontPtr.score <= node.score) {

                backPtr.next = node;
                node.next = frontPtr;
            }

            else {

                frontPtr.next = node;
                tail = node;
            }
        }

        if(size() > 10) {

            Node currentPtr = head;

            while(currentPtr.next != tail) {

                currentPtr = currentPtr.next;
            }

            tail = currentPtr;
            currentPtr.next = null;
        }
    }

    public void printList() {

        Node temp = head;

        while(temp != null) {

            System.out.print(temp.name + " " + temp.score + " ");
            System.out.println("");
            temp = temp.next;
        }
    }

}

}没有堆栈跟踪是很复杂的。 但错误可能就在这里

while(frontPtr.score > node.score && frontPtr.next != null)
因为frontPtr是空的

在列表中添加一个复选框

if (frontPtr!=null)
    while(frontPtr.score > node.score && frontPtr.next != null)

看起来您没有设置
下一步
。这是一个问题。第二,即使这样做,也会进入无限循环,因为插入逻辑做得不正确。我对您的
insert()
做了一些修改,使其能够正常工作,但这仍然缺乏优雅,离有效的实现还很远。例如,在运行10个元素之后,每次插入时都会运行
size()
,这会使代码复杂性增加大约一倍
N=size()
。如果确实要这样做,请将
size
设为变量,并在每个
insert()的末尾增加它。无论如何,编辑代码:

public class GamerList {

    private class Node {

        String name;
        int score;
        Node next;

        Node(String val1, int val2, Node n) {

            name = val1;
            score = val2;
            next = n;
        }

        Node(String val1, int val2) {

            this(val1, val2, null);
        }
    }

    private Node head;
    private Node tail;

    /**
     * Constructor.
     */

    public GamerList() {

        head = null;
        tail = null;
    }

    /**
     * The isEmpty method checks to see if the list is empty.
     * @return true if the list is empty, false otherwise.
     */
    public boolean isEmpty() {

        return head == null;
    }

    /**
     * The size method returns the length of the list.
     * @return The number of elements in the list.
     */
    public int size() {

        int count = 0;
        Node p = head;

        while(p != null) {

            count++;
            p = p.next;
        }

        return count;
    }

    public void insert(String name, int score) {

        Node node = new Node(name, score);

        if(isEmpty()) {

            head = node;
            head.next = tail;
        }
        else if(head.score <= node.score) {

            node.next = head;
            head = node;
        }

        else {
            Node beforeNode = head;
            while(beforeNode.score > node.score && beforeNode.next != null) {
                beforeNode = beforeNode.next;
            }
            node.next = beforeNode.next;
            beforeNode.next = node;
        }

        if(size() > 10) {

            Node currentPtr = head;

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

    public void printList() {

        Node temp = head;

        while(temp != null) {

            System.out.print(temp.name + " " + temp.score + " ");
            System.out.println("");
            temp = temp.next;
        }
    }

}
公共类玩家列表{
私有类节点{
字符串名;
智力得分;
节点下一步;
节点(字符串val1,int val2,节点n){
name=val1;
得分=2分;
next=n;
}
节点(字符串val1,int val2){
这(val1,val2,null);
}
}
专用节点头;
私有节点尾部;
/**
*构造器。
*/
公共玩家列表(){
head=null;
tail=null;
}
/**
*isEmpty方法检查列表是否为空。
*@如果列表为空,则返回true,否则返回false。
*/
公共布尔值为空(){
返回头==null;
}
/**
*size方法返回列表的长度。
*@返回列表中的元素数。
*/
公共整数大小(){
整数计数=0;
节点p=头部;
while(p!=null){
计数++;
p=p.next;
}
返回计数;
}
公共void插入(字符串名称、整数分数){
节点=新节点(名称、分数);
if(isEmpty()){
头部=节点;
head.next=tail;
}
else if(head.score node.score&&beforeNode.next!=null){
beforeNode=beforeNode.next;
}
node.next=beforeNode.next;
beforeNode.next=节点;
}
如果(大小()>10){
节点电流ptr=头;
对于(int i=0;i<9;i++){
currentPtr=currentPtr.next;
}
currentPtr.next=null;
}
}
公共作废打印列表(){
节点温度=头;
while(temp!=null){
系统输出打印(temp.name+“”+temp.score+“”);
System.out.println(“”);
温度=下一个温度;
}
}
}

Java6?还是Java7?什么是类
GamerList
?GamersList insert和printList方法在哪里?是否可以添加堆栈跟踪?是否尝试在代码中设置断点,将项目添加到链接列表中,并使用调试器运行程序?这将在几秒钟内向您显示问题。@AmirKost这些都在您必须滚动获取它们的第一个代码片段中。这里是堆栈跟踪:线程“main”java.lang.NullPointerException在GamerList.insert(GamerList.java:99)在TestGamerList.main(TestGamerList.java:13)中的异常这有什么不同吗?它抛出了与上述更改相同的异常。是的,我的错。我看错了。不管怎样,问题是
frontPtr
是空的,就像ai之前说的那样。所以只要检查一下,我做了一个编辑,尝试一下,抱歉,如果我不太清楚我在问什么。这正是我无法理解的。谢谢你的帮助!我还将size改为变量,而不是像您建议的那样每次调用size()方法。@user282701,我建议您1)阅读一些关于调试的基础知识(可能在IDE文档中),2)阅读一些关于数据结构和算法的知识(可能对于初学者-不要从Cormen或Sedgewick;)开始)。祝你好运!
public class GamerList {

    private class Node {

        String name;
        int score;
        Node next;

        Node(String val1, int val2, Node n) {

            name = val1;
            score = val2;
            next = n;
        }

        Node(String val1, int val2) {

            this(val1, val2, null);
        }
    }

    private Node head;
    private Node tail;

    /**
     * Constructor.
     */

    public GamerList() {

        head = null;
        tail = null;
    }

    /**
     * The isEmpty method checks to see if the list is empty.
     * @return true if the list is empty, false otherwise.
     */
    public boolean isEmpty() {

        return head == null;
    }

    /**
     * The size method returns the length of the list.
     * @return The number of elements in the list.
     */
    public int size() {

        int count = 0;
        Node p = head;

        while(p != null) {

            count++;
            p = p.next;
        }

        return count;
    }

    public void insert(String name, int score) {

        Node node = new Node(name, score);

        if(isEmpty()) {

            head = node;
            head.next = tail;
        }
        else if(head.score <= node.score) {

            node.next = head;
            head = node;
        }

        else {
            Node beforeNode = head;
            while(beforeNode.score > node.score && beforeNode.next != null) {
                beforeNode = beforeNode.next;
            }
            node.next = beforeNode.next;
            beforeNode.next = node;
        }

        if(size() > 10) {

            Node currentPtr = head;

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

    public void printList() {

        Node temp = head;

        while(temp != null) {

            System.out.print(temp.name + " " + temp.score + " ");
            System.out.println("");
            temp = temp.next;
        }
    }

}