c - 关于删除链表中节点的问题
问题描述
在调用 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;
没有必要。所指向的节点中的任何数据都temp
将在free
被调用时立即变为无效,因此在该节点内的任何值变为无效之前立即更改该节点内的任何值都将无效。
推荐阅读
- mysql - 在 SQL 中汇总数据
- linux - 使用多个选项卡和不同命令打开新的 gnome-terminal (v3.28+)
- angular - 响应 rxjs 取消订阅
- angular - 如何将 params.get('brand') 分配给以下函数中的变量?
- ruby-on-rails - 多态关联如何在 New 操作中获取其 imageable_id 值?
- c# - 根据 SVG (XML) 文件中的子元素文本获取子元素的属性值
- jqgrid - free-jqGrid 4.15.6 ExpandNode 产生运行时错误
- javascript - 单击 NavItem 以在 React/Materilize 中显示弹出窗口/模态框
- momentjs - 使用 momentjs 计算错误的日期差异
- laravel - Laravel 关系一对多得到结果加入