Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

nodes

 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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

thx,
Justin
 
fred rosenberger
lowercase baba
Bartender
Posts: 12122
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Shyam Prasad Murarka
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Reader,

[ May 09, 2006: Message edited by: Shyam Prasad Murarka ]
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic