0

I have a very simple question that I am not able to understand how should I move further.

I have a doubly linked list in which First node is null object and Last node is null object.

And I have a various string element that I want to insert in between first and last node. For example- I have string element "C", "D", "A", "P".

And after inserting into doubly linked list. I need some insertion order like this-

FirstNullObject P D C A LastNullObject

So I am not sure how should I compare null object with the various string that I want to compare and keep on adding. Suppose firstly I am adding C then somehow I have to insert C in between those two null object. And then if I am adding D then I have to compare D with C and then list should like this-

FirstNullObject D C LastNullObject

here newLink is the newNode like C, D and firstNode is null object. but this doesn't works. I always get an exception.

if (newLink.data.compareToIgnoreCase(list.firstNode.data) > 0) {

        //logic

        }

I hope questions is clear

arsenal
  • 23,366
  • 85
  • 225
  • 331
  • did you get NullPointerException? I can only assume that you using wrong NullObjectPattern, and compare not string data but objects itself: `newLink.compareToIgnoreCase(list.firstNode)` to do this implement Comparable. – mishadoff Feb 19 '12 at 12:10
  • yeah I get the NPE. But that's not possible as firstNode is not string. Its a NewLink datatype. – arsenal Feb 19 '12 at 12:13
  • You might want to look at Ruby - nulls (nils) are objects there, of the NilClass – Oleg Mikheev Feb 19 '12 at 12:27

4 Answers4

3

You never asked a question, so no, the question is not clear.

It may be "How do I determine if an object is null?" An answer:

o == null

It may be, "How might I determine that I'm at the end of a linked list?" An answer: you have the final node when the next node would be null.

It may be, "How might I represent an end-node node in a linked list?" An answer: it's the same as any node, but the field that would refer to the next node in the list is null.

It may be, "How can I have a field that can be both a list-node and, at times, null? Do I need some magical NodeONull, or...?" An answer: the field just has the type of your node, and you just assign it it null. Read section 4.1

It may be, "How can I have a field that can be both a String member of a list and, at times, some special value that serves as a sentinel for the list?" An answer: uh, what? The 'value' field of a node has nothing at all to do with the structure of the list. That you ask this question suggests that you're way out in the wilderness with your code, and should refer back to your course material on what exactly you're to deal with.

I'm aware that you spoke of doubly-linked lists; none of these answers change for them. Yes, I think that any one of above could be the answer you're looking for.

EDIT:

and firstNode is null object

Yeah, you're out in the wilderness.

  1. Your sentinel is not a node, except technically.

  2. Your sentinel does not have a .data field that you're ever interested in.

  3. What you're actually doing here - or half-doing, as you get errors - is attemping to have special 'first' and 'last' and 'middle' nodes of a list. Three separate kinds of nodes, and maybe you've also thought of an 'empty list' object. Since this is dumb, probably you're doing it only because you don't understand what you're to do.

What you're to do is rather more like

class Cons {
  public String data;
  public Cons prev;
  public Cons next;
  Cons(String data, Cons prev, Cons next) { ... }
}

An empty list: null

A one-member list: new Cons("a", null, null);

A three-member list:

Cons list = new Cons("a", null, null);
list.next = new Cons("b", list, null);
list.next.next = new Cons("c", list.next, null);

Just as nothing much changes when you move from a linked to a doubly-linked list, nothing much changes when you move from Java's provided exceptional subtype of Cons (that is, null) to your own subtype of it.

Julian Fondren
  • 5,459
  • 17
  • 29
1

If you have implemented Null object pattern correctly, then instead of doing like

if (newLink.data.compareToIgnoreCase(list.firstNode.data) > 0) {

    //logic

    }

You should do something like

if (newLink.equals(list.firstNode)) {

    //logic

    }

provided to overriden equals and hashcode appropriately.

Sajan Chandran
  • 11,287
  • 3
  • 29
  • 38
1

Null object should be an object, not null (for Java) - that's the main intent of the pattern. You need to have some subclass of your data class that essentially "does nothing" (see this article, the C++ or C# example work well to give you a clue about what should be done in Java) and does not represent any valid object of the type in your domain.

Alexander Pavlov
  • 31,598
  • 5
  • 67
  • 93
0

You cannot compare null with an Object, as null is not an object. See Is null an Object?. Why do you want these "NullObjects" anyway?

Community
  • 1
  • 1
RedPixel
  • 1,904
  • 1
  • 11
  • 11