Re: List help
From: Daniel Sjöblom (dsjoblom_at_mbnet.fi_NOSPAM)
Date: 07/07/04
- Next message: Shanmuhanathan T: "Re: Deployment Problem"
- Previous message: Roedy Green: "Re: Can't read file when called from Applet?"
- In reply to: john: "Re: List help"
- Next in thread: J: "Re: List help"
- Reply: J: "Re: List help"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 07 Jul 2004 10:44:28 +0300
john wrote:
> On Tue, 06 Jul 2004 08:29:38 +0300, Daniel Sjöblom
> <dsjoblom@mbnet.fi_NOSPAM> wrote:
>
>
>>> Any idea what I'm doing wrong here? I think I am setting prev to
>>> current then current to current.next. then putting the node between
>>> the two. Where is the null pointer coming from?
>>> That's the error I get. I shoule mention that in the main there are
>>> three titles which are the other nodes.
>
>
>>> public void insert (Book newBook){
>>> BookNode node = new BookNode (newBook);
>>> BookNode current;
>>> BookNode prev;
>>> if (head == null)
>>> head = node;
>>> else
>>> {
>>> current = head;
>>> while (current.next != null){
>>> prev = current;
>>> current = current.next;
>>> node.prev = current.prev;
>>> node.next = current;
>>> node.prev.next = node;
>>> current.prev = node;
>>> }
>>> current.next = node;
>>> }
>>> }
>>
>>
>> Consider what happens if there is only one node in the list (the head)
>> and you add another node. What does node.prev point to in that case?
>
>
> Yes, I see. I had to modify the code that was given to me. Linked
> lists are new to me. Christ, I didn't know what one was until the other
> day. I think I could argue pretty well that my understand is not much
> better than when I didn't know they existed. :P
I would suggest you start out with a singly linked list. It is slightly
easier to manage. Also, as a general programming tip, if you do not
understand something, you will not be able to program it. It is better
to work something out on paper first if the problem is too hard.
> I really didn't know what question to ask since I was having a hard
> time with it.
>
> Is a node simply a pointer to a reference variable?
A Node is a datatype, or simply a class. It contains two references
(next and prev) to other nodes. When you are using an object variable
(an instance of a class) you are always using a reference, in effect,
you are always manipulating what a variable refers or points to.
A reference is somewhat similar to a pointer in some other languages,
hence the NullPointerException. Unfortunately, java does not have
pointers! This is a confusing inconsistency in the naming of the
exception. It should be probably be named NullReferenceException instead.
Can I get
> compare data with a node?
Yes. You can compare the data (the book) in this case.
>Or is a node ONLY a pointer to next the node?
Well, a node is simply a node. A variable of type Node is a reference to
a Node object. But a Node object also *contains* references to two other
Node objects, prev and next.
> So if I compared nodes, I'd comparing what the nodes pointed to, right?
Yes. That is, if you write thisNode != thatNode, or thisNode ==
thatNode, you would be comparing the adresses that thisNode and thatNode
are stored at, not the contents of the nodes. To compare the books
contained in the nodes you need to write a Comparator.
>> It is also not clear what exactly you are trying to achieve with the
>> loop. It reminds me of a quote : "This is not right. It is not even
>> wrong" :-)
>
>
> Hey, wait. I've used that one. Jeez, I'm way off. Remember, I am
> new *sniff*
No offense intended. I know how frustrating it can be to debug something
for hours.
> Ignore the loop for a minute. When creating the 'current' reference
> in the above code, what exactly happens there?
>
When you say:
BookNode current;
you are simply creating a variable of type BookNode. It does not refer
or point to anything yet. In fact, if you try to use it directly after
declaring it, the compiler will complain about an unitialized variable.
Later, when you say:
current = head;
You are setting current to refer or point to the same object as the head
variable. If we ignore the loop and look at the statement after it:
current.next = node;
Here, we are setting the current.next field to point to node. If the
loop wasn't there, this would also mean that it sets head.next to point
to node, since head and current point to the same object. But we forgot
to set node.prev to point to current! It is still pointing at null. In a
doubly-linked list, we must always make sure that if someNode.next
points to anotherNode, then anotherNode.prev must point to someNode.
Whew! That was complicated. Or rather not complicated, but longwinded.
The gist of it is, that you cannot use objects directly in java. Instead
you are always accessing an object through a reference. Because spelling
all of this out takes a lot of effort, many java programmers just say
that they are using objects, or passing objects or whatever. This
further confuses newbies. Another confusing thing is that the rules work
differently for primitive types (int, long, float etc.), as opposed to
reference types.
If you are still confused, I suggest you try reading chapter 4 of the
java lanaguage specification (you can get it for free from
java.sun.com). It may be some heavy reading, but it is the
specification. If someone tries to argue that java doesn't work the way
described in there, they are wrong and you should ignore them.
-- Daniel Sjöblom Remove _NOSPAM to reply by mail
- Next message: Shanmuhanathan T: "Re: Deployment Problem"
- Previous message: Roedy Green: "Re: Can't read file when called from Applet?"
- In reply to: john: "Re: List help"
- Next in thread: J: "Re: List help"
- Reply: J: "Re: List help"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|