So this is the code
void add(String data) {
Link newLink = new Link(data);
newLink.next = firstLink;
firstLink = newLink;
}
If we have only one element firstLink.next will point itself i.e firsLink (because of newLink.next = firstLink;) so it will be not null. and if we have print method like this:
void print() {
Link currentLink = firstLink;
while (currentLink != null) {
System.out.println(currentLink.data);
currentLink = currentLink.next;
}
}
this should be a infinite loop, but in fact its not true when I start it in eclipse. My question is why ?