代码之家  ›  专栏  ›  技术社区  ›  Sarah Sepanski

在c中使用temp变量将节点添加到链表的末尾++

  •  1
  • Sarah Sepanski  · 技术社区  · 8 年前

    我正在学习链表,我有以下代码,但我不理解它。我试图掌握的是如何使用临时变量将节点添加到链表末尾的概念。有人能告诉我发生了什么事吗?

    struct Node{
    int data;
    Node *next;
    };
    
    Node *head = NULL;
    head = new Node;
    head->next = NULL;
    head->data = 97; // I understand up to here
    Node *temp = head; //I do not understand this part on
    temp->next = new Node;
    temp = temp->next;
    temp->next=NULL;
    temp->data=50;
    
    1 回复  |  直到 8 年前
        1
  •  2
  •   Mohamed Kandeel    8 年前
    Node *temp = head 
    

    温度现在等于水头

    temp->next = new Node;
    

    初始化下一个节点

    temp = temp->next;
    

    temp->next=NULL;
    

    这只是意味着链表的最后一个元素为空

    temp->data=50;