0

For a ClassProject we have to write our own Linkedlist with all its methods. The method i am posting now is simply putting an Object with its Data and 2 referrences (prev / next) at the end of the list.

Node addObject(Object o) {
    Node item = new Node (o);
    Node node = head;
    while(node!=null) {
        if(node.getNext()==null) {
            node.setNext(item);
            item.setPrev(node);
            
        }
        node = node.getNext();
    }

The question for me is, and not just for this method, what is item.next pointing on now, is it initially NULL ? I did not initializies it with NULL in the Node Objekt. Does it need a pointer in it like item.setnext(NULL) ?

  • 1
    It will point to null. It doesnt need any initialization to be null. if you initialize your item node, it will point to next, which is null until you assign something to it – YHStan Jun 22 '21 at 16:00
  • 1
    You're asking what the default value of `Node.next` is, yet haven't provided the `Node` class. However if it's as you'd expect (`Node next;`) then yes, it would be `null`. – Henry Twist Jun 22 '21 at 16:01
  • 1
    simple logic: if the condition to know which one is the last one is `if(node.getNext()==null)` (your code), and you append another one, what would be the value of `next` in the last element? – jhamon Jun 22 '21 at 16:04
  • Henry Twist, yes, thanks – David Rumpf Jun 22 '21 at 16:08

0 Answers0