【问题标题】:Assigning a pointer address to a node将指针地址分配给节点
【发布时间】:2015-11-08 03:46:28
【问题描述】:

如果afterWhat是head中包含的数字,以下是我在链表中​​插入数字的代码。 错误出现在代码的最后一行

head -> next = &temp;

错误是:

无法在赋值中将 Node** 转换为 Node*。

我想要做的是将 temp 的地址提供给 head.next,以便 head 指向 temp。

void LinkedList::insertAfter(int toInsert, int afterWhat)
{
       if(head->data == afterWhat)
       {

        Node* temp = new Node;
        temp->next = head->next;
        temp->data = toInsert;
        head->next = &temp;
       }
}

【问题讨论】:

  • head->next = temp;?
  • temp 已经是指向节点的指针。你不想要head->next = temp;吗?

标签: c++ linked-list


【解决方案1】:

因为 temp 已经是一个指针,所以你不必写 &temp。 使用头->下一个=临时;

【讨论】:

    【解决方案2】:

    你需要

    head->next = temp;
    

    temp 的类型是Node*,所以&temp 的类型是Node**,所以编译器报错是正确的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-23
      • 2021-01-18
      相关资源
      最近更新 更多