3

I am trying to insert a node at the end of doubly linked list but when I run the add method, the code never finishes running. Here is the code below:

public class DoublyLinkedList<T> {
    static class Node<T> {
        T data;
        Node<T> next;
        Node<T> previous;
        
        Node() {
            data = null;
            next = null;
            previous = null;
        }
        
        Node(T value) {
            data = value;
            next = null;
            previous = null;
        }
    }
    
    private Node<T> head;
    private int size;
        
    public DoublyLinkedList() {
        head = null;
    }
    
    public DoublyLinkedList(T value) {
        head = new Node<T>(value);
        size ++;
    }

    public void add(T value) {
        Node<T> append = new Node<T>(value);
        append.next = null;
        if(head == null) {
            append.previous = null;
            head = append;
            size ++;
            return;
        }
        Node current = head;
        while(current.next != null) {
            current = current.next;
        }
        current.next = append;
        append.previous = current;
        size ++;
    }

I pretty sure the line that says current.next = append is the problem, but I am not sure how to fix it. What am I doing wrong?

  • 1
    These lines: `append.next = null;` and `append.previous = null;` seem unnecessary, as they repeat (parts of) functionality of the `Node`'s constructor. – CiaPan Nov 23 '20 at 17:41
  • Is the `Node current` variable of _the same type_ `Node` as the rest of your data? – CiaPan Nov 23 '20 at 17:43
  • Have you tried actually debugging your code? Like, setup breakpoints inside your `add` function and go step by step? The function itself doesn't look wrong to me, but if the code won't finish you might be stuck in your `while` loop, but I don't see why it should be the case. – NRUB Nov 23 '20 at 17:44
  • I tried to run your code. I didn't face any problems. Everything works fine – Tugay Nov 23 '20 at 17:46
  • @CiaPan I added those two lines just to see if it would work, but I didn't originally have them and changing current to Node doesn't make a difference. – SwedishFish14 Nov 23 '20 at 17:49
  • @MichałKaczorowski When comment "current.next = append" the code runs fine, but when it isn't it doesn't. I even commented the while loop and kept "current.next = append" and it still did not work – SwedishFish14 Nov 23 '20 at 17:51
  • @SwedishFish14 "when comment ( ...) the code runs fine (...)" I'm sorry but that's not what debugging is. Please refer to your specific IDE and how to setup breakpoints in it. Typically, you navigate to specific line and hit some key like F9 or RMB and set a breakpoint, and then you run your program in debug mode. Once it reaches first breakpoint you can then execute it step by step. I still don't see why commenting out simple assignment could solve your issue. You should also provide minimal reproducible example https://stackoverflow.com/help/minimal-reproducible-example – NRUB Nov 23 '20 at 17:58
  • @MichałKaczorowski I can't use breakpoints as this is an online IDE for a class. I am using it to get more familiar with it and practice doubly linked lists. Also thanks for the link, I'll take a look at that. – SwedishFish14 Nov 23 '20 at 18:06
  • At This point there is probably an error or limitation with my class IDE so I'll just email the teacher and see what they say. Thanks For the help! – SwedishFish14 Nov 23 '20 at 18:10
  • @SwedishFish14 Well, you won't get anywhere with IDE that doesn't offer debugging, especially if you want to learn. Get yourself, Netbeans or Eclipse or IntelliJ, and start there. You can always then paste your finished code into that online something if it's a requirement. – NRUB Nov 23 '20 at 18:16
  • Can you share your code with main function and a sample setup (i.e., an exact call where it is getting the error)? I do not feel the root cause of this problem is not lying in the `add()` method. – biqarboy Feb 11 '21 at 03:12

0 Answers0