Reputation:
I have the following function to append a double value at the end of a linked list.
void deappendlinked(dNODE *head,double value){
while(head!=NULL){
if(head->next==NULL){
break;
}
head=head->next;
}
dNODE *newElement=memalloc(sizeof(dNODE));
newElement->data=value;
newElement->next=NULL;
head->next=newElement;
}
It receives the head, i.e., the address of the first element of the linked list, which I iterate through to the end.
As I'm not passing a pointer of a pointer, I do not need a temporary variable to hold the value of the head, correct? (I.e. I can just do what I did – at least it works.)
Upvotes: 1
Views: 60
Reputation: 51864
As I'm not passing a pointer of a pointer, I do not need a temporary variable to hold the value of the head, correct?
That is correct. The function will receive a copy of the pointer passed by the calling module. In that module, the value of that pointer will not be changed by your function (i.e. it will remain pointing at the head of the list).
As an aside, even if you know that your passed head
value will not (read: should not) be NULL
, it is still better to add a check for that, in case a future editor of your code does something silly.
Also, your while
loop can be much simplified:
void deappendlinked(dNODE *head,double value) {
if (head == NULL) {
// Optional error message?
return;
}
while (head->next != NULL) head = head->next;
dNODE *newElement=memalloc(sizeof(dNODE));
newElement->data=value;
newElement->next=NULL;
head->next=newElement;
}
Upvotes: 1