c - 链表温度自动更改
问题描述
我无法理解为什么temp
在声明之后将其值从 9 更改为 1:
last->data = (*head_ref)->data;
我目前的目标是反转包含 1、3、5、7 和 9 的链表的第一个和最后一个节点中的数据。
我得到的结果是 9、3、5、7、9。
如果temp
等于 last 等于,即使我在更改 last 后没有设置,更改 lasthead_ref
会影响吗?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(); }
此代码将反转整个链表而不会丢失数据。
推荐阅读
- machine-learning - 机器学习,其中训练数据的标签可能不是 100% 准确
- cookies - 如果您可以控制登录会话,是否可以使用 Headless Chrome 访问登录会话?
- python - 在 Pandas 中分组和求和而不丢失列
- sql - 在 LINQ 中包含带有 where 子句的对象
- asp.net-core - 在 ASP.NET Core 2 中返回状态码为 201 的文件
- javascript - 试图将 Django 隐藏到页面源中显示的 JS 声明变量
- javascript - Safari、javascript:等待打印确认窗口
- python - 为什么在这个产生小倍数的代码中需要 numpy.ravel() ?
- python - 使用 python 在谷歌云存储桶中创建文件夹
- wordpress - wordpress 自定义字段更新后禁止 403