微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

链表插入函数通过引用发送参数误解

如何解决链表插入函数通过引用发送参数误解

我正在阅读一本关于 C/C++ 的书,并从中学习了链表。

我将 LL 实现为:

struct Node {
    int information; // the information of one node. assumed is that this is an integer only
    struct Node * next; // pointer which stores the address of next node in the list. points to next node in the list.
}

typedef struct Node * List;

我创建 Insert 函数为:

void Insert(List & First,List p,int x) { // First is given by reference because after insertion the First node can be changed.
    // Dynamically allocate new memory for the node we want to insert
    struct Node * q  = new Node;
    
    // Populate the new node with its information (the integer it contains)
    q -> information = x;

    if (!p) { // case 1: insert at beginning
        q -> next = First;
        First = q;
    }
    else { // case 2: insert in the middle of the list or at the end of the list
        q -> next = p -> next;
        p -> next = q; 
    }
}

我的问题是:

为什么我们发送 First 参数作为引用而不发送 p 参数作为引用? 我看到 First 改变了“整体”,只有 p 的“片断”改变,所以“不是整体”。会不会是这个原因?

First = q;

p -> next = q;

我对指针不是很熟悉,但是如果我们将变量的地址从调用函数发送到被调用函数,则被调用函数可以“操纵变量”,因为它不会在其堆栈上复制实际参数的值转换为一个新对象,但作用于调用函数堆栈中的实际变量。

为什么我们需要写那个参考符号,为什么我们不需要为 p 写那个符号?

我使用这个函数来创建一个队列:

First = Last = NULL;
std::cin >> n; // this many events (n) will populate the Queue

for (int i=1; i<=n; i++) {
    std::cin >> x; // the information of this i-th node.

    if (First) { // the Queue is NOT EMPTY
        Insert(First,Last,x);
        Last = Last -> next;
    }
    else { // the Queue is EMPTY
        Insert(First,NULL,x);
        Last = First;
    }
}

谢谢!

解决方法

为什么我们将 First 参数作为引用发送,而 p 参数不作为引用发送?

因为 First 可能会改变(以需要与调用者沟通的方式)而 p 可能不会 - 正如评论所说。

我看到 First 改变了“整体”,只有 p 的“片断”发生了变化

不,p 根本没有变化。更改 p 看起来像

p = new_value_for_p;

++p;

什么的,但是没有这样的代码

...只有“片断”的 p 处发生变化

不不不。变量 p 是一个指针。它永远不会改变。更改指针意味着您使其指向其他内容(或不指向任何内容,使用 nullptr)。

线

p->next = q;

不会改变 p 的“一块”。它改变了p引用的对象。该对象没有名称,但我们可以称其为 *p。更改此对象的内容根本不会影响p。相反,将指针更改为指向其他位置不会影响它指向的对象

我对指针不是很熟悉

这是显而易见的。你需要学习理解指针间接性,否则在阅读使用指针的代码时总会遇到麻烦。当我说“麻烦”时,我的意思是不知道代码在做什么

尝试退后一步,绘制一些方框和指针图 (pdf)。

,

这个 typedef 确实引起了一些混乱(至少对我来说它起初是一个混乱的根源):

df_concat = pd.merge(df1,df2,on='common_column_name',how='outer')

所以这里的论点:

typedef struct Node * List;

实际上是

void Insert(List & First,List p,int x) {

参数在 C++ 中是按值传递的,除非你是按引用传递。指针也不例外。当你打电话

void Insert(Node*& First,Node* p,int x) {

然后Insert(head,node,42); 会修改Insert,不会修改head,正如你在代码中看到的:

node

void Insert(Node*& First,int x) { struct Node * q = new Node; q->information = x; if (!p) { q->next = First; First = q; // (1) } else { q->next = p->next; p->next = q; // (2) } } 在传递给函数时会修改 (1)。行 head 只修改指针指向的内容,而不是指针本身。如果有类似的东西

(2)

在函数中,那么这只会修改函数局部 p = nullptr; ,不会影响传递给函数的p。因为函数不需要修改它的第二个参数,所以不作为引用传递。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。