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
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
Joined: Jan 24, 2006
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?