During an interview I was asked to implement some linked list methods in Java. I've only had experience implementing linked list in C++ and there were somethings that concerned me. After having read this question and seeing the answer, I still have concerns.
class Link {
public int data1;
public double data2;
public Link nextLink;
//Link constructor
public Link(int d1, double d2) {
data1 = d1;
data2 = d2;
}
//Print Link data
public void printLink() {
System.out.print("{" + data1 + ", " + data2 + "} ");
}
}
My first problem is Java doesn't have pointers. I'm used to seeing something like Node* nextNode;
instead of public Link nextLink;
. How can a value be assigned to Link without pointers? Is it because in Java new
returns an actual Link
object? Or does it return a reference to it? If it returns a reference to it isn't that a problem since a reference is different than an actual Link
object? Or in Java is any object's identifier actually a reference?
I know this is a matter of opinion, but why is it that linked lists seem to occur more in C++? I guess the language doesn't have built in support so a lot of tutorials written about linked lists are geared towards C++ (even if the focus was on the data structure).