6

I have a linked list in which first node contains null object. means firstNode.data is equal to null, firstNode.nextPointer = null, firstNode.previousPointer = null.

And I want to check if firstNode is null or not. So I tried-

if(list.firstNode == null){

            //do stuff          
        }

but this doesn't works?

I also tried equals too. Any suggestions?

I tried printing. And I got as-

{null} -- firstNode

7
  • 4
    Please explain "doesn't work" in greater detail. Commented Feb 19, 2012 at 19:42
  • It is not going inside that loop. That's what I mean.
    – AKIWEB
    Commented Feb 19, 2012 at 19:43
  • So perhaps list.firstNode is not equal to null? Commented Feb 19, 2012 at 19:44
  • That comparison should work. Use System.out.println() to print out the two values you're comparing. You will likely find that what you're comparing to is not null. Commented Feb 19, 2012 at 19:44
  • 2
    One point worth noting - there's no such thing as a null object; there's a null reference.
    – Jon Skeet
    Commented Feb 19, 2012 at 19:45

6 Answers 6

13

I think your firstNode is not null, but its fields are. Try something like this:

if (list.firstNode.data == null) {
    //do stuff          
}
1

Did you try

if (list.firstNode.data == null) { /* Do stuff */ }
0

You checking for list.firstNode being null. Do you mean to check for

list.firstNode.data==null
0

The answer is in the question. You said:

 have a linked list in which first node contains null object. **means firstNode.data is equal to null**,

This means you should do the following instead:

if(list.firstNode.data == null){
     //do stuff          
}
0

It seems to me that your question is related to the processing of a doubly-linked list.
To check if empty use: (list.firstNode.next == list.firstNode.previous) this is true for an empty doubly linked list.

0

You can check if all the fields of the node are null:

Node firstNode = list.firstNode;
if(firstNode.data == null &&
   firstNode.nextPointer == null &&
   firstNode.previousPointer == null) {
    //Do stuff
}

Or to prevent code repetition, you can either create an instance method isNull() to do the test or create a NULL object and override the equals method in your Node class to check if a node is equal to the null node as you described.

class Node<E> {
    //The null node, assuming your constructor takes all three values.
    public static final Node NULL = new Node(null, null, null);

    //Fields here with constructors etc.

    @Override
    public void equals(Object obj) {
        if(!obj instanceof Node) return false;
        Node<?> node = (Node<?>)obj;
        if(node.data.equals(this.data) &&
           node.nextPointer == this.nextPointer &&
           node.previousPointer == this.previousPointer) {
            return true;
        } else {
            return false;
        }
    }

Then when you want to check if a node is null you can do:

if(list.firstNode.equals(Node.NULL)) {
    //Do stuff
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.