1

我的循环链表出现问题。它是一个列表,当前填充有节点,每个节点都包含我的 Person 类的一个对象。

我想做的是:

删除节点,比启动节点提前 5 步。它工作正常,直到我的列表只包含 1 个节点。然后在调用 randomStart() 并尝试打印列表时,它给了我这个错误。

cirkulærliste.CircularLinkedList.randomStart(CircularLinkedList.java:60) 的线程“主”java.lang.NullPointerException 异常在 cirkulærliste.Test.main(Test.java:98)

如果有人可以看看我的 CircularLinkedList 类和我的 Test 类,那就太好了。:)

public class CircularLinkedList<E extends Comparable<? super E>> {

    private Node<E> cursor;
    private Node<E> start;
    private int size = 0;

    public CircularLinkedList() {
        setCursor(null);
    }

    /**
     * tilføjer personer
     * @param p
     */
    public void addElement(E e) {
        Node<E> newNode = new Node<E>(e, null);
        Node<E> tempNext = cursor;

        if(cursor == null){
            cursor = newNode;
            cursor.setNext(cursor);
            cursor.setPrev(cursor);
        } else if(size > 1){
            newNode.setPrev(cursor);
            newNode.setNext(cursor.getNext());
            cursor.getNext().setPrev(newNode);
            cursor.setNext(newNode);

        } else {
            newNode.setPrev(cursor);
            newNode.setNext(cursor);
            cursor.setPrev(newNode);
            cursor.setNext(newNode);
        }
        size++;
    }

    /**
     * udskriver personerne i den rækkefølge de står i listen
     */
    public void print() {
        Node<E> tempNode = start;
        while (!tempNode.getNext().equals(start)) {
            System.out.println(tempNode);
            tempNode = tempNode.getNext();
        }
        System.out.println(tempNode);
    }

    /**
     * en tilfældig person i den cirkulæreliste vælges som start i listen
     */
    public void randomStart() {
        int size = 1;
        Node<E> tempNode = cursor.getNext();
        // NPE occurs here
        while (!tempNode.getNext().equals(cursor.getNext())) {
            tempNode = tempNode.getNext();
            size++;
        }
        Random randomizer = new Random();
        int chosen = randomizer.nextInt(size);
        for (int i = 0; i <= chosen; i++) {
            tempNode = tempNode.getNext();
        }
        start = tempNode;
    }

    /**
     * fjerner den person fra listen der ligger 5 pladser fra start i listen. Personen der fjernes returneres.
     */
    public Node<E> remove() {
        Node<E> tmpNode = start;

        for(int i = 1; i < 5; i++){
            tmpNode = tmpNode.getNext();
        }

        Node<E> tmpNode2 = tmpNode.getNext();


        if(tmpNode.getNext().equals(start)){
            Node<E> tmp3 = start.getNext();
            tmpNode.setNext(start.getNext());
            start.getNext().setPrev(tmpNode);
            start = tmp3;

        } else {

        tmpNode.setNext(tmpNode.getNext().getNext());
        tmpNode.getNext().setPrev(tmpNode);
        tmpNode2.setNext(null);
        tmpNode2.setPrev(null);
        }

        return tmpNode2;

    }


    public void setCursor(Node<E> cursor) {
        this.cursor = cursor;
    }


    public Node<E> getCursor() {
        return cursor;
    }


    public void setStart(Node<E> start) {
        this.start = start;
    }


    public Node<E> getStart() {
        return start;
    }
}



package cirkulærliste;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        CircularLinkedList<Person> list = new CircularLinkedList<Person>();


        list.addElement(new Person("Thomas", 1));
        list.addElement(new Person("Bjarne", 2));
        list.addElement(new Person("Camilla", 3));
        list.addElement(new Person("Bo", 4));
        list.addElement(new Person("Bobo", 5));
        list.addElement(new Person("Martin", 6));
        list.addElement(new Person("Søren", 7));
        list.addElement(new Person("Luller", 8));

        list.randomStart();

        System.out.println("START: " + list.getStart());
        System.out.println("-----------------------------------------");
        list.print();
        System.out.println("-----------------------------------------");

        System.out.println("Deleting: " + list.remove());
        System.out.println("-----------------------------------------");

        list.randomStart();
        System.out.println("START: " + list.getStart());
        System.out.println("-----------------------------------------");
        list.print();

        System.out.println("-----------------------------------------");

        System.out.println("Deleting: " + list.remove());
        System.out.println("-----------------------------------------");

        list.randomStart();
        System.out.println("-----------------------------------------");
        System.out.println("START: " + list.getStart());
        System.out.println("-----------------------------------------");
        list.print();

        System.out.println("-----------------------------------------");

        System.out.println("Deleting: " + list.remove());
        System.out.println("-----------------------------------------");

        list.randomStart();
        System.out.println("-----------------------------------------");
        System.out.println("START: " + list.getStart());
        System.out.println("-----------------------------------------");
        list.print();

        System.out.println("-----------------------------------------");

        System.out.println("Deleting: " + list.remove());
        System.out.println("-----------------------------------------");

        list.randomStart();
        System.out.println("-----------------------------------------");
        System.out.println("START: " + list.getStart());
        System.out.println("-----------------------------------------");
        list.print();

        System.out.println("-----------------------------------------");

        System.out.println("Deleting: " + list.remove());
        System.out.println("-----------------------------------------");

        list.randomStart();
        System.out.println("-----------------------------------------");
        System.out.println("START: " + list.getStart());
        System.out.println("-----------------------------------------");
        list.print();


        System.out.println("-----------------------------------------");

        System.out.println("Deleting: " + list.remove());
        System.out.println("-----------------------------------------");

        list.randomStart();
        System.out.println("-----------------------------------------");
        System.out.println("START: " + list.getStart());
        System.out.println("-----------------------------------------");
        list.print();


        System.out.println("-----------------------------------------");

        System.out.println("Deleting: " + list.remove());
        System.out.println("-----------------------------------------");

        list.randomStart();
        System.out.println("-----------------------------------------");
        System.out.println("START: " + list.getStart());
        System.out.println("-----------------------------------------");
        list.print();


    }

}
4

1 回答 1

0

除非我弄错了,否则没有理由这样做:

int size = 1;
Node<E> tempNode = cursor.getNext();
// NPE occurs here
while (!tempNode.getNext().equals(cursor.getNext())) {
    tempNode = tempNode.getNext();
    size++;
}

在随机开始()中。

每次addElement()调用该方法时,您已经增加了一个“大小”int。this.size删除上述代码后只需使用或仅调整大小。

编辑: 我认为你的remove()方法比它需要的更复杂。

编辑:这现在允许您通过使大小等于 1 的特殊情况来删除最终节点。它还返回刚刚被删除的节点。

public Node<E> remove() {
    Node<E> tmpNode = start;
    if (size > 1) {
        // Move five nodes from the start
        for(int i = 0; i < 5; i++)
            tmpNode = tmpNode.getNext();

        // Move the start and cursor nodes if
        // they are about to be removed.
        if (tmpNode.equals(start))
            start = start.getNext();
        if (tmpNode.equals(cursor))
            cursor = cursor.getNext();

        // Remove the fifth node
        Node<E> next = tmpNode.getNext();
        Node<E> prev = tmpNode.getPrev();
        prev.setNext(next);
        next.setPrev(prev);

        size--;
    } else if (size == 1) {
        start = null;
        cursor = null;

        size--;
    }
    return tmpNode;
}

让我知道这是否有帮助!

于 2012-01-17T00:28:46.857 回答