I'm having some trouble on a problem. What is supposed to happen is that a list of integers is read into an unsorted Doubly Linked List, and then sorted using an insertion sort. The code I have for my Doubly Linked List class is
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; } public DoublyLinkedList sort() { DoublyLinkedList sortedList = new DoublyLinkedList(); sortedList.add(first.getData()); Node sortNode = first.getNext(); Node sortNode2 = first.getNext(); Node tempNode = sortedList.last; Node tempNode2 = null; for (int i = 0; i < size-1; i++) { if (sortNode.getData() >= sortedList.last.getData()) { sortedList.add(sortNode.getData()); } else { if (tempNode.getPrevious() != null) { for (int j = 0; j < sortedList.size-1; j++) { tempNode2 = tempNode.getPrevious(); Node tempNode3 = sortNode; if (tempNode3.getData() >= tempNode2.getData()) { tempNode2.setNext(tempNode3); tempNode3.setPrevious(tempNode2); tempNode3.setNext(tempNode); tempNode.setPrevious(tempNode3); } else { tempNode = tempNode2; } } } sortedList.size++; } sortNode = sortNode2.getNext(); sortNode2 = sortNode2.getNext(); } return sortedList; } public String toString () { String temp = ""; Node printNode = first; for (int i = 0; i < size(); i++) { temp += printNode.getData() + "\n"; printNode = printNode.getNext(); } return temp; } public String backwards () { String temp = ""; Node printNode = last; for (int i = 0; i < size(); i++) { temp += printNode.getData() + "\n"; printNode = printNode.getPrevious(); } return temp; } }
My Node class is
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 getPrevious() { return previous; } public void setNext(Node next) { this.next = next; } public void setPrevious (Node previous) { this.previous = previous; } }
And my main code which makes a new List then attempts to sort it.
I think my problem is that my pointers are getting messed up, but I can't tell where. I've tried following my code by hand, but I can't see where the problem is. Any help would be greatly appreciated.import java.io.*; public class A2Q1 { public static void main (String [] args) { String input = ""; int data; DoublyLinkedList testList = new DoublyLinkedList(); try { BufferedReader br = new BufferedReader(new FileReader("unsorted.txt")); while ((input = br.readLine()) != null) { data = Integer.parseInt(input); testList.add(data); } } catch (IOException E) { } System.out.println("StartPreSort\n" + testList + "ENDED"); System.out.println("Backwards\n" + testList.backwards() + "Ended"); testList = testList.sort(); System.out.println("StratPostSort\n" + testList + "ENDED"); } }