I'm attempting to sort a DoublyLinkedList, but my code is producing an error in the cmd box. My problem is, I don't know what's wrong because it just prints line after line of "at(DoublyLinkedList.sortRecur(Test.java:170)" . The code for the sorting algorithm is
public void sort () { Node test = first; Node current = first.getNext(); current.setPrev(null); while (current.getNext() != null) { sortRecur(test, current); current = current.getNext(); } } public void sortRecur(Node test, Node current) { if (test.getData() < current.getData()) { if (current.getPrev() == null) { test.setPrev(null); test.setNext(current); current.setPrev(test); first = test; } else if (test.getData() > current.getPrev().getData()) { (current.getPrev()).setNext(test); test.setPrev(current.getPrev()); test.setNext(current); current.setPrev(test); } else { sortRecur(test, current.getPrev()); } } else { if (current.getNext() == null) { test.setNext(null); test.setPrev(current); current.setNext(test); last = test; } else if (test.getData() < current.getNext().getData()) { (current.getNext()).setPrev(test); test.setNext(current.getNext()); test.setPrev(current); current.setNext(test); } else { { sortRecur(test, current.getNext()); } } } }
The line it refers to is the "sortRecur(test, current.getNext());" line. My DoublyLinkedList class is as follows
class DoublyLinkedList { private int size; private Node first; private Node last; public DoublyLinkedList () { first = null; last = null; size = 0; } public void add (int data) { if (first == null) { first = new Node(data, null, null); last = new Node(data, null, null); } else if (size < 2) { Node newNode = new Node(data, first.getNext(), first); first.setNext(newNode); last = newNode; } else { Node newNode = new Node(data, null, last); last.setNext(newNode); last = newNode; } size++; } public int size() { return size; } }
And my Node class is as follows:
class Node { private int data; private Node next; private Node previous; public Node(int data, Node next, Node previous) { this.data = data; this.next = next; this.previous = previous; } public int getData() { return data; } public Node getNext() { return next; } public Node getPrev() { return previous; } public void setNext(Node next) { this.next = next; } public void setPrev (Node previous) { this.previous = previous; } }
If anyone could help me out with what my code's error is, or how I can figure out what the error is, that would be greatly appreciated.