*next pointer will contain the reference for next node and data will have the value. After this will create three Node type global pointers.
At first we have to check if head is equal to NULL or not. If there is no element in linked list, head will be NULL. So, we'll initialize it with newNode and we'll also initialize tmp with newNode. Now all three pointers is pointing to the same node and as we're not initializing the *next pointer it points to NULL.
If the linked list is not empty that means the head is not NULL, then we'll just insert the value in the variable data and initialize the previous nodes *next pointer to the current node and initialze *tmp to now point at current node.
If we want to print the values inserted in the linked list we can create a function print_elements().
we'll make a node type pointer that points to the head that means the first node of the linked list and we'll iterate loop until currentNode is NULL and print the data of each node. currentNode will be NULL when we reach the last node.
Full Source code
simple and easy explanation. thanks.
ReplyDelete