Pointer to Node class

When we declare a pointer to a Node class, like Node *temp = head, What it does is, It declares a Pointer to Node class, temp which is pointing towards the head. So, if temp is a pointer then, How are we able to do temp->next or temp->data? Does temp->data means head->data internally as temp is pointing towards head in a Linked List? Thus, if we do any modification in temp like temp->next=NULL it is being reflected in head. Am I right?

hey @raghav6, node* temp is pointer that will store the address of object of Class.

So, if temp is a pointer then, How are we able to do temp->next or temp->data?

next and data are data members of class. They can be access using object of class or using pointer to object of class. If object is used to access them then . operator is used, if pointer to object is used to access them then -> is used thatswhy we are able to access next and data using pointer using ->.

Does temp->data means head->data internally as temp is pointing towards head in a Linked List?
Yes.

Thus, if we do any modification in temp like temp->next=NULL it is being reflected in head.
Yes.

Hence, node* temp is a pointer to node, which can be used to access the address of an object of class Node, thus having access to its data members (data, next) as well.
Thanks @Gaurav13998 , Best :grinning:

hey @raghav6 , if your query is resolved. Please mark this doubt as resolved and rate me on the basis of your experience.
rating option will appear when to mark this doubt as resolved

Done and rated! @Gaurav13998

1 Like