Java中十大玩家链接列表

时间:2013-09-28 19:35:04

标签: java linked-list

我正在寻找一些我为作业设计的课程的帮助。它将游戏分数添加到链接列表中,并从最高到最低列出它们。最高分数为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;
        }
    }

}

这是我的班级来测试GamerList

公共类TestGamerList {

/**
 * @param args
 */
public static void main(String[] args) {


    GamerList list1 = new GamerList();

    list1.insert("Fry", 89);
    list1.insert("Bender", 25);
    list1.insert("Leela", 90);
    list1.insert("Zoidburg", 23);
    list1.insert("Amy", 34);
    list1.insert("Hermes", 96);
    list1.insert("Zapp",123);
    list1.insert("Nibbler", 56);
    list1.insert("Calculon", 12);
    list1.insert("Hypnotoad", 189);
    list1.insert("Lrrr", 5);
    list1.insert("Scruffy", 28);

    System.out.println("Top 10 Scores: ");
    list1.printList();
}

}

2 个答案:

答案 0 :(得分:2)

您似乎没有设置head的{​​{1}}。这是一个问题。第二个是,即使你这样做,你也会陷入无限循环,因为你错误地完成了插入逻辑。我已经改变了你next以使它工作,但仍然缺乏优雅,远远没有有效实施。例如,在您有10个元素之后的每次插入时,您运行insert()这会使您的代码复杂性增加大约一倍。 size()。如果您真的想这样做,请将N = size()变为一个变量,并在每个size的末尾增加它。无论如何,编辑的代码:

insert()

答案 1 :(得分:-1)

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

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

因为frontPtr为空。

上添加一项检查
if (frontPtr!=null)
    while(frontPtr.score > node.score && frontPtr.next != null)