• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

nodes

 
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Reader,

[ May 09, 2006: Message edited by: Shyam Prasad Murarka ]
 
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic