View Single Post
CompuChip
#4
Sep27-11, 01:29 AM
Sci Advisor
HW Helper
P: 4,300
Java: Creating my own Linked List

Quote Quote by rcgldr View Post
I don't know Java, so I'm not sure if the next pointer should be placed first in a node class that has a variable data component.
That's not necessary, Java is quite good at handling memory itself.

Quote Quote by rcgldr View Post
If you want the ability to determine if a node is in a list
then you should ask the list whether it contains a node. IMO you shouldn't even need to be able to create a node yourself (its constructor should be private).

Quote Quote by rcgldr View Post
You could make this more generic by defining the node class to have a generic pointer to a data object (the equivalent of a void pointer in C), rather than including the actual data in the node class.
In Java, you would do this using generics, and declare a Node<T> object. Then you'd probably also have a List<T> with an add(T newElement) function that creates a Node<T>(newElement).