C++ 为什么第一个推压头之后仍然为空?

C++ 为什么第一个推压头之后仍然为空?,c++,linked-list,singly-linked-list,C++,Linked List,Singly Linked List,我正在尝试创建单链接列表。在第一次推送之后,head仍然为空。为什么头在第一次推后没有更新 using namespace std; typedef struct node { int data; // will store information node *next; // the reference to the next node }; void push(node*,int); void print(node*); int main() {

我正在尝试创建单链接列表。在第一次推送之后,
head
仍然为空。为什么头在第一次推后没有更新

using namespace std;

typedef struct node {
    int data;        // will store information
    node *next;     // the reference to the next node
};

void push(node*,int);
void print(node*);

int main()
{
    node* head = NULL;  //empty linked list
    push(head, 2);
    if (head == NULL) {
        cout << "vrvrvr";
    }
    push(head, 3);
    push(head, 5);
    push(head, 2);
    //print(head);
    getchar();
    return 0;
}

void push(node* x, int y){
    node *temp = new node();
    if (x == NULL) { // check linked list is empty
        temp->next = x;
        temp->data = y;
        x = temp;

    }
    else {
        node *temp1 = new node();
        temp1 = x;
        while (temp1->next != NULL) { // go to the last node
            temp1 = temp1->next;
        }
        temp1->next = temp;
        temp->data = y;
        temp->next = NULL;
        delete temp1; // 'temp' node will be the last node
    }
}

void print(node* x){
    node *temp1 = new node();
    temp1 = x;
    while (temp1->next != NULL) {
        cout << temp1->data << endl;
        temp1 = temp1->next;
    }
}
使用名称空间std;
类型定义结构节点{
int data;//将存储信息
node*next;//对下一个节点的引用
};
无效推送(节点*,int);
作废打印(节点*);
int main()
{
node*head=NULL;//空链表
推(头,2);
if(head==NULL){
cout-next=x;
温度->数据=y;
x=温度;
}
否则{
node*temp1=新节点();
temp1=x;
while(temp1->next!=NULL){//转到最后一个节点
temp1=temp1->next;
}
temp1->next=temp;
温度->数据=y;
temp->next=NULL;
删除temp1;//“temp”节点将是最后一个节点
}
}
无效打印(节点*x){
node*temp1=新节点();
temp1=x;
while(temp1->next!=NULL){
下一步是收集数据;
}
}

推送
的主要问题是对函数中的
x
所做的更改是函数的本地更改。它不会更改
main
head
的值

您可以通过将参数类型更改为
node*&
来解决此问题

void push(node*& x, int y) {
   ...
}
我看到的其他问题也在这个模块中:

else {
    node *temp1 = new node();
    temp1 = x;
    // Problem 1:
    // After this, the memory returned by the previous line is lost.
    // It is a memory leak.

    while (temp1->next != NULL) { // go to the last node
        temp1 = temp1->next;
    }
    temp1->next = temp;
    temp->data = y;
    temp->next = NULL;
    delete temp1; // 'temp' node will be the last node
    // Problem 2:
    // You are deleting a node from the linked list.
    // The linked list now has a dangling pointer.
}
您可以使用以下方法纠正这些问题:

    node *temp1 = x;
    while (temp1->next != NULL) { // go to the last node
        temp1 = temp1->next;
    }
    temp1->next = temp;
    temp->data = y;
    temp->next = NULL;
}
建议的改进

  • 节点的定义中删除
    typedef
    。在您发布的代码中,它是一个悬空的
    typedef
    。此外,在C++中,可以使用<代码>节点< />代码,而不使用<代码> TyPulf< /Cord>。
    struct node {
       int data;
       node *next;
    };
    
  • 将构造函数添加到
    节点

    struct node {
       node(int d) : data(d), next(nullptr) {}
       int data;
       node *next;
    };
    
    这将简化
    push
    中的代码

    void push(node*& x, int y){
       node *temp = new node(y);
    
       if (x == NULL) { // check linked list is empty
          x = temp;
       }
       else {
          node *temp1 = x;
          while (temp1->next != NULL) { // go to the last node
             temp1 = temp1->next;
          }
          temp1->next = temp;
       }
    }
    

  • 您正在分配给
    节点*
    参数。这与分配给
    int
    参数一样有效。指针没有什么特别之处。如果要修改变量,则需要指向要修改的变量的引用或指针。请阅读有关通过引用传递参数的信息
    void push(node*x,int y)
    应该是
    void push(node*&x,int y)
    我现在尝试该解决方案:)引发异常:读取访问冲突。temp1是0x1。如果存在此异常的处理程序,则程序可以安全地继续。仍然不起作用