首页 > 解决方案 > 难以理解 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 = *sourcenewNode是指向指针中的值的source指针吗?

*source = newNode -> next;为此,source指针正在获取 的地址newNode,并与newNode?

我无法理解这些作业。

PS:此moveNode代码用于合并两个排序的链表。

另外,请说明地址何时传递或存储在上述代码中。

标签: c++pointerslinked-list

解决方案


此代码将一个节点从源列表的前面移动到目标列表的前面。

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

推荐阅读