0

I'm trying to define my own doubly linked list class in Java. My current methods add integers to the LinkedList (in order from lowest value to highest value), print them from lowest to highest, and print them from highest to lowest. My method that prints from highest to lowest has to do so by using the current node's pointer to the previous node to know which number to print next. However, for some reason my add method does not assign my previous node pointers properly. The LinkedList class looks like

public class DoubleLink {
    /**
     * Class to create a node object for double linked list
     * @author Nick Gilbert
     */
    private class Node {
        int num;
        Node next;
        Node previous;

        public Node(int num, Node next, Node previous) {
            this.num = num;
            this.next = next;
            this.previous = previous;
        }
    }
    private Node head;

    public DoubleLink() { //All lists start out empty
        head = null;
    }

    public void add(int num) {
        if(head == null) //If num is first entry to list
        {
            head = new Node(num, null, null);
            return;
        }

        Node current = null; //Pointer to iterate through list
        for(current = head; current.next != null; current = current.next){ //Starting at head and going to end
            if(current.next.num >= num) { //if next number is greater than number we want to add then we've found the spot to add it to
                current.next = new Node(num, current.next, current);

                if(num < head.num) //If this statement is true then the num we added is less than the current head's num which means the head needs to be reassigned
                {
                    int temp = current.next.num;
                    current.next.num = head.num;
                    head.num = temp;
                }
                return;
            }
        }
        current.next = new Node(num, null, current);
    }

    public void print() { //prints from lowest number to highest
        for(Node current = head; current != null; current = current.next) {
            System.out.println(current.num );
        }
    }

    public void printBackwards() { //prints from highest to lowest
        Node current = null;
        for(current = head; current != null; current = current.next) {
            if(current.next == null)
                break;
        }
        Node tail = new Node(current.num, null, current.previous);
        for(current = tail; current != null; current = current.previous) {
            System.out.println(current.num );
        }
    }
}

and this next code segment contains my test cases

public class DoubleLinkTester {

    public static void main(String[] args) {
        DoubleLink list = new DoubleLink();
        list.add(5);
        list.add(9);
        list.add(7);
        list.add(3);
        list.add(8);
        list.add(10);
        list.add(4);
        list.add(6);
        System.out.println("Call print, should get 3,4,5,6,7,8,9,10");
        list.print();
        System.out.println("Call printBackwards, should get 10,9,8,7,6,5,4,3");
        list.printBackwards();

Executing the printBackwards method with the above test cases prints 10, 9, 3 instead of the expected 10, 9, 8, 7, 6, 5, 4, 3. I believe it is because the add method does not properly reassign the pointers to previous numbers as new ones are added but I don't know how to fix it.

1 Answer 1

3

You missed to reset the previous of the next node to the new node added.

public void add(int num) {
    // Code above this remains same

            if(current.next.num >= num) { //if next number is greater than number we want to add then we've found the spot to add it to

                Node actualNext = current.next;    // Missed

                current.next = new Node(num, current.next, current);

                actualNext.previous = current.next;   //Missed

 // Code below this remains same
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.