C++ 通过引用传递时未更新起始指针

C++ 通过引用传递时未更新起始指针,c++,pointers,linked-list,stack,C++,Pointers,Linked List,Stack,这是推送功能 void push(struct node **head) { struct node *temp; temp = new node; cout<<"enter the value"; cin>>temp->data; temp->link=NULL; if(*head==NULL) { *head=new node; *head=temp; }

这是推送功能

void push(struct node **head)
{
    struct node *temp;
    temp = new node;
    cout<<"enter the value";
    cin>>temp->data;
    temp->link=NULL;
    if(*head==NULL)
    {   
        *head=new node;
        *head=temp;
    }
    else{
        temp->link=*head;
        *head=temp;
}
}    
这是一个节点

struct node{
    int data;
    struct node *link;
};
现在的问题是:我认为名单没有更新。起始值始终保持为空。不知道为什么

编辑:

void显示(结构节点**head)
{
结构节点*temp;
温度=*水头;
如果(*head==NULL){

在注释中提到了PXDIABLO的答案:C++有旁路引用。
#include <iostream>

struct node
{
    int data;
    struct node *link;
};

void push(node*& head)
{
    struct node *temp = new node;
    std::cout << "enter the value";
    std::cin >> temp->data;
    temp->link = head;
    head = temp;
}

int main()
{
    node *start = NULL;
    push(start);
    return 0;
}
#包括
结构节点
{
int数据;
结构节点*链接;
};
无效推送(节点*&头部)
{
结构节点*temp=新节点;
std::cout>temp->data;
温度->链接=头部;
压头=温度;
}
int main()
{
node*start=NULL;
推(启动);
返回0;
}

在注释中,PaxDabLo提到了答案:C++具有旁路引用。
#include <iostream>

struct node
{
    int data;
    struct node *link;
};

void push(node*& head)
{
    struct node *temp = new node;
    std::cout << "enter the value";
    std::cin >> temp->data;
    temp->link = head;
    head = temp;
}

int main()
{
    node *start = NULL;
    push(start);
    return 0;
}
#包括
结构节点
{
int数据;
结构节点*链接;
};
无效推送(节点*&头部)
{
结构节点*temp=新节点;
std::cout>temp->data;
温度->链接=头部;
压头=温度;
}
int main()
{
node*start=NULL;
推(启动);
返回0;
}

替代实施方案:

void push(struct node** head_reference, int new_data)
{
    struct node* a_node = new node;
    a_node->data  = new_data;
    a_node->link = (*head_reference);    
    (*head_reference)    = a_node;
}

int main() {
    struct node* head = NULL;
    push(&head, 10);
    // rest of your code here
    return 0;
}

替代实施:

void push(struct node** head_reference, int new_data)
{
    struct node* a_node = new node;
    a_node->data  = new_data;
    a_node->link = (*head_reference);    
    (*head_reference)    = a_node;
}

int main() {
    struct node* head = NULL;
    push(&head, 10);
    // rest of your code here
    return 0;
}

如果只有一种方法可以避免使用指针来模拟RC的憎恶。如果只有C++有真正的引用。这将是一个值得考虑的特性:-是的——不是你的问题,而是<代码> *Head =新节点;< /C>是多余的。A会增加答案的可能性和质量。@ SamarYadav,因为你赋值<代码> > Typ>代码>下一行。分配的内存泄漏。不能复制:如果只有一个方法来避免通过使用指针来模拟RF的C憎恶。如果只有C++有真正的引用。这是一个值得考虑的特性,是的:-不是你的问题,而是<代码> *Head =新节点;< /C> >超级。fluous.A会增加答案的可能性和质量。@Samayadav,因为您在下一行中分配了
temp
。分配的内存泄漏。无法复制: