Reputation: 8433
I have a question, if I wish to delete a node from linked list and I do this : Assume: Head points to first node.
deleteFirstNode(struct node * head)
{
struct node* temp=head;//this line
temp->next=head->next->next;
temp->data=head->next->data;
free(head);
head=temp;
}
1) will this delete first node?
2) If so, when I free head , will it not free temp pointer too? because both temp and head point to same location in this line (see comment in "this line" in code). If both above are true, how will I retain retain pointer to beginning of the list. ? Thank you very much.
Upvotes: 0
Views: 3493
Reputation: 8433
From your suggestions above here is my final code to delete first node and return pointer to head node (which will be second node in list).
struct node* freeFirstNode(struct node* head)
{
struct node* temp;
temp=head//edited after expert's comment
temp->next=head->next;//please confirm if this is valid and not Bad pointer.
temp->data=head->data;
free(head);//how will I fix this? it will free temp too.
return(temp);
}
Upvotes: 0
Reputation: 340316
You want to delete the node that head
points to; you have several problems:
1) you pass in a copy of the head
pointer - the function is unable to change the original head
pointer the function was called with, so the last line of the function, head=temp
does nothing really. When the function returns whatever you had pointing to the first node on the list will now be pointing to freed memory. In effect, you have lost the list.
2) when you're grabbing head->next->data
you're not getting the data item you want because head->next
has been overwritten.
3) free(head)
will also free temp
since it points to the same thing as head
. temp
is kind of pointless.
Some notes in the code:
deleteFirstNode(struct node * head)
{
struct node* temp=head;
temp->next=head->next->next; // note: this overwrites head->next
temp->data=head->next->data; // so this gets what used to be
// head->next->next->data
free(head); // this frees `temp` (which is an alias for `head`)
// - so whats the point of `temp`?
head=temp; // this is pointless in more ways than one
}
So here's a proposed (untested) alternate version of deleteFirstNode()
:
deleteFirstNode(struct node ** head)
{
struct node* temp = *head; // temp points to the node we want to free
struct node* next = temp->next; // next points to what will be the new
// first node
free(temp);
*head=next;
}
You will have to call the function by passing in a pointer to the head pointer:
struct node* head_pointer;
// ...
deleteFirstNode(&head_pointer);
Another thing to consider:
NULL
, deleteFirstNode()
will not work. You should make it handle that case.Upvotes: 1
Reputation: 206566
Yes you have two pointers pointing to the same address.
So deallocating memory by calling free()
on either of them will free the pointed memory.
EDIT:
For understanding Link lists always draw them on piece of paper.
------------- ------------ -----------
| | | | | |
| head |----->| first |------>| second |
| | | | | |
------------- ------------ -----------
Steps you need to do are:
Isolate first
.
Make head
point to Second
.
free first
.
temp = head->next; //temp now points to first
head = temp->next; //head now points to second
free(temp);
Upvotes: 1
Reputation: 17441
i would pass a double pointer and do something on these lines
deleteFirstNode(struct node ** head) {
struct node* temp= *head;
*head = temp->next;
free(temp);
}
Upvotes: 4
Reputation: 11232
will this delete first node?
Yes.
If so, when I free head , will it not free temp pointer too? because both temp and head point to same location in this line (see comment in "this line" in code).
Yes, it will free the memory pointed by temp
too as both head
and temp
are pointing to same memory. Pointer head
will be pointing to an invalid memory location after this method is executed.
Upvotes: 1