【问题标题】:how last->next = temp works?last->next = temp 是如何工作的?
【发布时间】:2021-03-22 13:39:01
【问题描述】:

这是显示来自 udemy 教程的链表的代码,我们将获取一个数组并将这些值存储在链表中。我不明白代码是如何工作的。在下面的代码中,我们存储下一个节点的地址 last->next=temp;在我没有创建最后一个节点的地方工作,比如 last=new node;我只创建了最后一个指针。没听懂,谁能给我解释一下它是如何工作的

#include <iostream>
using namespace std;
 
class Node{
public:
    int data;
    Node* next;
};
 
int main() {
 
    int A[] = {3, 5, 7, 10, 15};
 
    Node* head = new Node;
 
    Node* temp;
    Node* last;
 
    head->data = A[0];
    head->next = nullptr;
    last = head;
 
    // Create a Linked List
    for (int i=1; i<sizeof(A)/sizeof(A[0]); i++){
 
        // Create a temporary Node
        temp = new Node;
 
        // Populate temporary Node
        temp->data = A[i];
        temp->next = nullptr;
 
        // last's next is pointing to temp
        last->next = temp;
        last = temp;
    }
 
    // Display Linked List
    Node* p = head;
 
    while (p != nullptr){
        cout << p->data << " -> " << flush;
        p = p->next;
    }
 
    return 0;
}

【问题讨论】:

  • 如果你不明白代码是如何工作的,你可以做的第一件事:用调试器单步调试。

标签: c++ data-structures linked-list


【解决方案1】:

找出指针的最好方法是用笔和纸画出方框和箭头。

这是对所发生事情的(悲伤的)ASCII 再现:

head->data = A[0];
head->next = nullptr;
last = head;

head指向一个新创建的节点,last指向与head相同的地方:

   head
    |
    v
+------+------+    
| data | next |
|      |(null)|
|      |      |
+------+------+
   ^
   |
  last

接下来,

// Create a temporary Node
temp = new Node;

// Populate temporary Node
temp->data = A[i];
temp->next = nullptr;

看起来像这样:

   head                 temp
    |                    |
    v                    v
+------+------+       +------+------+  
| data | next |       | data | next |
|      |(null)|       |      |(null)|
|      |      |       |      |      |
+------+------+       +------+------+
   ^
   |
  last

然后

last->next = temp;

更改节点last 指向的next 成员(在第一次迭代中,这与head 指向的节点相同):

   head                 temp
    |                    |
    v                    v
+------+------+       +------+------+  
| data | next |       | data | next |
|      |   ---------->|      |(null)|
|      |      |       |      |      |
+------+------+       +------+------+
   ^
   |
  last

最后,让last 指向最近创建的节点:

last = temp;

给了

   head                 temp
    |                    |
    v                    v
+------+------+       +------+------+  
| data | next |       | data | next |
|      |   ---------->|      |(null)|
|      |      |       |      |      |
+------+------+       +------+------+
                         ^
                         |
                        last

然后你从那里重复循环。

【讨论】:

  • 非常感谢兄弟这么详细的解释
【解决方案2】:

您已经在 for 循环之前使用 last = head 将变量 last 初始化为 head。这样做是为了确保在将其他节点添加到列表时不会修改原始头。在每次迭代中,最后一个节点用于跟踪链表中的最后一个节点,以便每个新节点都被添加到链表的末尾。

【讨论】:

    【解决方案3】:

    在下面的代码中,我们存储下一个节点的地址 last->next=temp;在我没有创建最后一个节点的地方工作,比如 last=new node;我只创建了最后一个指针。

    last 始终只是一个指针。它开始等于head,它是指向第一个节点的指针。在您的循环中,您创建一个temp 指向的新节点,然后将last 当前指向的节点的next 字段设置为temp,然后更改last 使其现在指向同一个节点,即新的最后一个节点。

    【讨论】:

      猜你喜欢
      • 2019-06-07
      • 1970-01-01
      • 2020-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多