下面这段代码有问题,但是我不知道怎么改?求大佬解答一下,谢谢!

struct node
{
int data;
struct node *next;
}
//swap between second and third
void swap(node *first, node *second, node *third)
{
third->next = second;
first->next = third;
second->next = third->next;
}

 

void swap(node* first, node* second, node* third)
{
    int temp = second->data;
    second->data = third->data;
    third->data = temp;

    first->next = third;
    second->next = third->next;
    third->next = second;
}

 

first->next = third;

second->next = third->next;

third->next = second;

 

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632

希望对你有帮助:https://blog.csdn.net/it_xiangqiang/category_10581430.html
 

由于不确定这些节点在整个链表中的关系,也不知道first是不是就是链表头,所以不能冒然该指针。只能交换数据。

struct node
{
    int data;
    struct node *next;
}
//swap between second and third
void swap(node *first, node *second, node *third)
{
    int temp = second->data;
    second->data = third->data;
    third->data = temp;
}

附注:求赞助积分和C币。加入CSDN将近20年了。最近几年忙小孩没登录。刚才搜索到一本电子书想下载,需要20积分/C币。已经收到8元了,还差12元。赞助多少都可以。多谢。

如果肯定者3个节点是顺序关系,就可以交换指针

void swap(node* first, node* second, node* third)
{
    if(first != NULL && second != NULL && third != null &&
        first->next = second && second->next == third)
    {
        first->next = third;
        second->next = third->next;
        third->next = second;
    }
}

非常感谢您使用有问必答服务,为了后续更快速的帮您解决问题,现诚邀您参与有问必答体验反馈。您的建议将会运用到我们的产品优化中,希望能得到您的支持与协助!

速戳参与调研>>>https://t.csdnimg.cn/Kf0y