I am currently learning to implement linked list in Java when I came across sample code which counts nodes in a linked list
class ListNodes { int item; ListNodes next; }
int count(Node ptr_start)
{
int c=0;
Node ptr = new Node();
ptr = ptr_start;
while(ptr != null)
{
c++;
ptr = ptr.next;
}
return c;
}
I have few questions here what exactly is
ListNodes next;
and 'ListNodes' has same name as that of it's class ?
what is
ptr = ptr_start; i think it's for pointer to start with a value , but can it have other values other than 'ptr_start' ? and
ptr = ptr.next;
I think it moves to next pointer but 'next' moves it to other pointer , it 'next' Java defined or user defined thing? It sort of seems like I don't understand anything is this code , please help?
Node ptr = new Node();when you're just usingptrto iterate through the list? Seriously, throw it away and start from scratch.