I am practicing a linked-list code. Below is the insert function for that:
Node* insert_at_pos(Node *head, int pos){
struct Node *ptr=NULL;
printf("enter data\n");
ptr=(Node*) malloc(sizeof(Node));
scanf("%d",&ptr->data);
ptr->next=NULL;
if (pos==0){
if (head==NULL){
head=ptr;
return head; //return that I want to remove
}
}
printf("done\n");
}
Instead of returning Node*
, if I return void
, I think this code should still work because I am passing value by reference. So the value of head
should update automatically instead of returning it, but it doesn't work if I remove the Node*
and put void
in the return type of insert_at_pos
.
And, I am calling insert_at_pos
function like this::
Node *head=insert_at_pos(head,0);
What could be the possible explanation or what is going wrong here?