wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes nodes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "nodes" Watch "nodes" New topic
Author

nodes

Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802


ok how does the node know that when ".next" is called, to go to the next
node in the list?

thx,
Justin


You down with OOP? Yeah you know me!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11250
    
  16

IMHO, a node shouldn't know how to go to the next one. all a node knows is it's value, and where the next one is.

you should have some kind of class that DOES know how to traverse your list - like a linkedList class. it would contain a node, a getter for the value, and a setter which would point it to some (probably the first) node.

it would also have a next() method, that would do something like

node = node.next;

the NODES don't move along, but the linkedList object does.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209
Dear Reader,
First of all, you need to make your data members private. Its not compulsory but a good design technique.
Next, make setter and getter methods for each of the data members.
eg.


With that in place, you need to now understand that the NumberNode class does not do anything important except hold information.
You need to have another driver sort of class, or should I say a List (LinkedList or anything similar) to make use of this NumberNode class.
The driver class keeps a reference to a root NumberNode object. And this root reference holds a refernce to another NumberNode object and this ... so you get what I am trying to say.
A kind of linked list is maintained where each node (NumberNode in this case) holds information to the next node.
The driver class is responsible for inserting and deleting nodes from this list and performing other manipulations.
The driver class while performing the above operations, calls the getNext() and setNext() (and other setter and getter methods) of the NumberNode class.


ok how does the node know that when ".next" is called, to go to the next
node in the list?


So, the NumberNode object does not need to know when to go to the next. Infact, its the driver class that keeps track of this information, with the help of the getter and setter methods.

Do you understand what I am trying to say?


With Best Regards,
Shyam Prasad Murarka
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802


ok, but this piece of code has no getter or setter methods, and it still
traverses the linked list?

im just confused, because it seems to me that all current.next would reveal is whether or not currents "next" value is null or not.

how does the current = current.next work?

i figured it would just make the the "current" nodes value equal itself?

thanks

Justin
Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209
Dear Reader,

[ May 09, 2006: Message edited by: Shyam Prasad Murarka ]
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

I don't know whether I am helping you out or I am creating confusion. But I developer a code for a Linked List here it is as follows:




I am sorry Justin if , you really don't understand this one... This is a complete code for a linked list.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: nodes