at the moment, I am programming a circular doubly linked list and my task is completed, but I have a slight problem in understanding the code 100%.
This is the code:
if (counter == 0) {
startpoint = newNode;
startpoint.next = startpoint;
startpoint.prev = startpoint;
head = startpoint;
currentNode = newNode;
} else {
newNode.prev = startpoint.prev;
newNode.next = startpoint;
startpoint.prev.next = newNode; <- this is, where I have problems
startpoint.prev = newNode;
head = startpoint;
currentNode = newNode;
}
counter++;
}
The first part is completeley clear and just describes, that the first node (if there is no other node) is going to point on itself, when the next or first-method is called. After the else-statement, the first line describes that the first node can point to the second and the second to the previous node, right? The second line after else just describes, that the last node points onto the first one. The fourth line than describes, that the first node points onto the last node, if the prev-method is called and so the circle is closed. But what exactly does the third line describes? The code definitely works.
Thank you for your time!
Greets Dominik
startpoint.prevpoints to the "last node before startpoint" in the circular list. Thus,startpoint.prev.next = newNodesays that the successor of that node isnewNodestartpoint.prev) by doingstartpoint.prev.next = newNode;beforestartpoint.prev = newNode;.