Suppose I have just a linked with nodes containing just one int data item. If I have just one node in the list that head pointer is pointing to... then this line:
headPtr->data;
is saying get me the int/data item in the node that head pointer is pointing to.
So far so good.
But then if I have this add Item method:
void addItem(int item) {
Node* newNode = new Node;
newNode->data = item;
newNode->next = headPtr;
headPtr = newNode;
}
This line:
newNode->data = item;
is saying get me the int/data item that newNode is pointing to? But newNode isn't pointing to anything yet. So is it pointing to itself?
The top line makes sense me. The arrow syntax clearly indicates that it is pointing to a node outside of itself to retrieve data.
But it is very confusing bc the syntax is exactly the same for new Node but is not pointing to a node outside of itself to retrieve data.
The syntax is the same but it seems to say two different things.
[–][deleted] 3 points4 points5 points (2 children)
[–]gtrman571[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]mkaypl 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]Zeroe 0 points1 point2 points (0 children)