下面是我在链表中插入一个数字的代码,如果after 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;
}
}
最佳答案
因为 temp 已经是一个指针,所以你不必写 &temp。 使用 head->next=temp;
关于c++ - 为节点分配指针地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33586734/