class Nodetype
{
int info;
Nodetype next;
Nodetype(int i)
{
info=i;
next=null;
}
}
My textbook has this code to create Linked List dynamically.
The question is, when the programs is executed line-by line, it defines variable ‘info’ as type ‘int’ & then variable ‘next’ as Nodetype.
What is actually happening here?
does it mean that variable ‘next’ will contain –
- Constructor ‘Nodetype’
- int info
- Nodetype “next” where “next” will again have all 1,2,3 & then 3 will again have 1,2,3…and so on….till infinity?
I’m really irritated because I’m unable to understand how it works, can someone easily explain this?
At first variable
nextdoesn’t point to any object(it points tonull). At some time you will make it point to another node withnext = new NodeType(number). The idea is that you use composition – you have one instance of class which has a reference to another instance. It is likenodeApoints tonodeB,nodeBpoints tonodeC. Here you have three instances and the first instance has a reference to the second instance and the second instance has a reference to the third instance. The third instance is the last one and its next instance points tonull.