c++ - 难以理解 C++ 中的链表中的一些作业
问题描述
我是编程新手,并且已经开始学习链表,但是我在理解代码中的某些作业实际上在做什么方面遇到了严重的麻烦。
例如:
void moveNode(node** desti, node** source) {
struct node* newNode = *source;
assert (newNode != NULL);
*source = newNode -> next;
newNode -> next = *desti;
*desti = newNode;
像 in 一样node* newNode = *source
,newNode
是指向指针中的值的source
指针吗?
*source = newNode -> next;
为此,source
指针正在获取 的地址newNode
,并与newNode
?
我无法理解这些作业。
PS:此moveNode
代码用于合并两个排序的链表。
另外,请说明地址何时传递或存储在上述代码中。
解决方案
此代码将一个节点从源列表的前面移动到目标列表的前面。
void moveNode(node** desti, node** source) {
struct node* newNode = *source;
// Now newNode points to the first node of the source list
assert (newNode != NULL);
// assert if source is empty
*source = newNode -> next;
// Pop first node from source by making it point to the next node
newNode -> next = *desti;
// Add newNode to the front of the desti list
*desti = newNode;
// Lengthen desti by pointing to the new first node
推荐阅读
- php - 扩展名 .php 在链接本身之前添加到文件链接
- google-cloud-run - 未拾取写入 /var/log 的 Google Cloud Run 日志
- react-native - react native 中的 expo-constants 我应该如何以及在哪里添加它
- django - Wagtail:如何将模型链接到可链接到另一个模型的 Orderable
- python - 如何使用 Selenium Python 将 PDF 上传到 Google Drive
- reporting-services - 根据组中的值对行求和
- java - 如何在 Angular 10 的 Java 服务器 api 中显示作为 BufferedImage 接收的图像
- javascript - 关于将节点标记为脏的浏览器策略
- python - 如何使用 django rest 框架添加每个学生的多个图像
- android - 整箱实现 Android Studio