RadixSort空指针

时间:2018-08-05 06:43:03

标签: java sorting radix

有人可以看看我的Radix排序代码,并帮助我弄清楚如何正确入列新条目吗?因为到目前为止,我的方式似乎永远无法设置第二个条目...由于某种原因,它始终将其指向null。我试图将tempNode设置为= new Node(newEntry),然后将下一个constalty设置为null(如果未定义),但仍然无效。

class LinkedQueue<T> implements QueueInterface<T> {

    private Node start;
    private Node end;

    public LinkedQueue() {
        start = null;
        end = null;
    }

    @Override
    public void enqueue(T newEntry) {
        Node tempNode = new Node(newEntry, null);
        if (isEmpty()) {
            start = tempNode;
        } else {
            end.setNext(tempNode);
            end = tempNode;
        }
    }

    @Override
    public T dequeue() {
        T front = null;
        if (!isEmpty()) {
            front = start.getData();
            start = start.getNext();
            if (start == null) // verification purposes
                end = null;
        }
        return front;
    }

    @Override
    public T getFront() {
        T front = null;
        if (!isEmpty())
            front = start.getData();
        return front;
    }

    @Override
    public boolean isEmpty() {
        return (start == null && end == null);
    }

    // reset the list
    @Override
    public void clear() {
        start = null;
        end = null;

    }

    private class Node {
        private T data;
        private Node next;

        public Node(T data) {
            this.data = data;
            next = null;
        }

        public Node(T data, Node next) {
            this.data = data;
            this.next = next;
        }

        public T getData() {
            return data;
        }

        public void setData(T data) {
            this.data = data;
        }

        public Node getNext() {
            return next;
        }

        public void setNext(Node next) {
            this.next = next;
        }
    }
}

public class RadixSorting {
    public static final int MAX = 10;

    public static void radixSort(int[] myArray, int start, int last, int maxDigit) {
        @SuppressWarnings("unchecked")
        QueueInterface<Integer>[] buckets = new LinkedQueue[MAX];
        System.out.println("\n");
        System.out.println("Buckets size:" + buckets.length);
        int bucket;
        int index;
        int i;

        for (bucket = 0; bucket < MAX; bucket++)
            buckets[bucket] = new LinkedQueue<Integer>();

        int multiplier = 1;
        for (i = 1; i <= maxDigit; i++, multiplier *= MAX) {
            for (bucket = 0; bucket < MAX; bucket++)
                buckets[bucket].clear();
            for (index = start; index <= last; index++) {
                int tempNum = (myArray[index] % (multiplier * MAX)) / multiplier;
                buckets[tempNum].enqueue(myArray[index]);
            }
            bucket = 0;
            for (index = start; index <= last; index++) {
                while (buckets[bucket].isEmpty())
                    bucket++;
                myArray[index] = buckets[bucket].dequeue();

            }
        }
    }

    public static void main(String args[]) {
        int array[] = { 5, 50, 15, 45, 40, 10, 25, 30, 20, 35 };
        RadixSorting.radixSort(array, 0, array.length -1, 4);
    }
}

出现我的错误:

Exception in thread "main" java.lang.NullPointerException
    at week5.LinkedQueue.enqueue(RadixSorting.java:51)
    at week5.RadixSorting.radixSort(RadixSorting.java:143)
    at week5.RadixSorting.main(RadixSorting.java:157)

1 个答案:

答案 0 :(得分:0)

我解决了它,我设法重新调试它,看到了答案。