please tell my mistake in implemented linked list
Doubt in linked list
I have asked three questions in comments (I used the term “container” which might not be technically correct, its just for explaining what I mean !)
-
head is a pointer to a node type of container. Basically, you create a node dynamically, ie at run time, so you need to know WHERE that node has been created, and hence we store the address of the newly created nodes inside “pointers”. Head is a pointer of type
node*
.
Since initially, no node is there and there is no address to be stored, we initialize head with NULL, else it could contain some garbage value. -
Temp is also a pointer of type node*, we are storing the address that head stores, inside temp also. So we can traverse the linked list freely without losing the initial address.
-
temp is pointer, to an object of class node. So it can access all the elements of the given instance of the class with the
->
operator. Typically if the object is created statically and not dyanmically then we use.
operator.
If the third point seems very confusing then you should study about OOPs in a little depth. I hope the thing about pointers is clear to you
@Ishitagambhir
by this you meant like we create an object and then access member functions like… obj.get_name();
and if we created a pointer which can point to object of class then we can do ptr->get_name();
Am I correct ?
@prerak_semwal yes thats correct, in this case we are making nodes based on the input so we’ll create them dynamically. But sometimes we make static objects also and in that case we can use the dot operator like obj.property
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.