gpt4 book ai didi

c - 链表温度自动变化

转载 作者:太空宇宙 更新时间:2023-11-04 04:58:49 24 4
gpt4 key购买 nike

我无法理解为什么 temp 在语句后将其值从 9 更改为 1:

last->data = (*head_ref)->data;

我目前的目标是反转包含1、3、5、7和9的链表的第一个和最后一个节点中的数据。

我得到的结果是 9、3、5、7、9。

如果 temp 等于 last 等于 head_ref,即使我没有设置 也会改变 last 影响 temp >temp = last 最后更改后?

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;
}

谢谢!

最佳答案

void reverse(){
struct node *first=NULL,*second=start,*third=start->next;
if(start==NULL){
printf("No Element to Reverse\n");
}else{
while(second!=NULL){
second->next=first;
first=second;
second=third;
if(third!=NULL){
third=third->next;
}
}

start=first;
}
display(); }

此代码将反转整个链表而不丢失数据。

关于c - 链表温度自动变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52463239/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com