0

Can someone take a look at my Radix sort code and help me figure out how i can properly enque the new entry? Because as of right now the way i did it seems to never be able to set a second entry... It always points it as a null for some reason. I tried to set the tempNode to = new Node(newEntry) then just set the next constalty to null if its not defined but that still did not work.

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);
    }
}

Heres my error:

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)
Z World
  • 11
  • 2
  • Please clearly describe what exactly your program, as in: what is expected vs actual output? – GhostCat Aug 05 '18 at 06:49
  • The program is suppose to take this array as an the input 5, 50, 15, 45, 40, 10, 25, 30, 20, 35 And then run the radix sort function which would sort that array to this 5 10 15 20 25 30 35 45 50 I am supposed to follow this format in terms of submission but i've been stressing about it for a minute and i had to turn for help.. – Z World Aug 05 '18 at 06:56
  • Please: never put such infos into comments. Update your question instead, to contain it as nicely formatted, well human readable information! But as I almost expected: NPEs are very common problem. You simply have to study what these things are, and what the different conditions are that put them into existence. – GhostCat Aug 05 '18 at 07:11
  • In your case: you might come in handy to learn how to use a debugger. – GhostCat Aug 05 '18 at 07:13

1 Answers1

0

I solved it, i managed to debug it with a fresh mind and i saw the answers.

Z World
  • 11
  • 2