在调用free()之前;函数,如果我不将NULL值分配给要删除的节点的链接部分,会有什么问题?我查看了一些删除其他网站中节点的代码,但未发现链接部分没有分配NULL值。他们只是调用了free();功能。请回复以消除我的困惑。谢谢。
struct node
{
int data;
struct node * next;
}
struct node * head = NULL; //This is the head node.
/* Here some other functions to create the list */
/* And head node is not going to be NULL here, after creating the list */
void deleteFirstNode()
{
struct node * temp = head;
head = temp->next;
temp->next = NULL; //my question is in this line, is this line necessary?
free(temp);
}
不行
temp->next = NULL;
is not necessary. Any data in the node pointed to by
temp
will become invalid as soon asfree
is called, so changing any values inside that node immediately before they become invalid will have no effect.