c - 链表添加新的根元素
问题描述
我正在尝试将一个新的根元素添加到我的链表中。我已经知道正确的答案,但我不明白为什么有一个指向当前根元素的双指针。这是正确的代码:
void elementAsRoot(Element **oldRoot, Element *newRoot){
newRoot -> next = *oldRoot;
*oldRoot = newRoot;
}
解决方案
我很确定你可以删除双指针,因为你最终会取消引用双指针。
void elementAsRoot(Element *oldRoot, Element *newRoot) {
newRoot->next = oldRoot;
oldRoot = newRoot;
}
推荐阅读
- php - 一次只允许购物车中的一个子类别
- sql - SQL 错误代码 ORA-00904: : 无效标识符 00904. 00000 - 创建带约束的表时出现“%s: 无效标识符”
- angular - 如何在没有 TestBed 的情况下为 Angular 组件编写单元笑话测试?
- reactjs - 如何将聊天中的加载图像替换为使用 ReactJS 上传到 Firebase 存储的图像?
- database - 如何在 Migration 的帮助下更改 Floor 数据库中的表列?
- android - Robolectric 启动一个有观察者的片段
- bash - 将参数附加到文件中的命令并运行附加的命令
- python-3.x - 使用按位运算进行异或
- typescript - Apollo 客户端 MockedProvider:跳过加载状态
- isabelle - 伊莎贝尔引理可以用来陈述关于定义的事实吗?