【发布时间】:2019-02-27 00:22:07
【问题描述】:
我无法理解为什么 temp 在声明后将其值从 9 更改为 1:
last->data = (*head_ref)->data;
我目前的目标是将包含1、3、5、7和9的链表的第一个和最后一个节点中的数据反转。
我得到的结果是 9、3、5、7、9。
如果temp 等于last 等于head_ref,即使我在更改last 后没有设置temp = last,更改last 是否会影响temp?
void reverseNode(struct Node** head_ref)
{
struct Node *last = *head_ref;
while(last->next != NULL)
{
last = last->next;
}
struct Node *temp = last;
printf("%d ", temp->data); // temp->data = 9
last->data = (*head_ref)->data;
printf("%d ", temp->data); // temp->data = 1
(*head_ref)->data = temp->data;
}
谢谢!
【问题讨论】:
-
在此语句中
struct Node *temp = last;的指针与last相同,并且您正在更改last的数据,因此temp反映了该更改。这就是你所看到的。 -
@Azeem 感谢您的澄清!
标签: c pointers linked-list