为什么这个交换不必更改以前的链接但仍然可以运行

发布于 2025-01-12 06:32:02 字数 960 浏览 2 评论 0原文

下面的代码是从 geekforgeeks 复制的。我对交换感到好奇 它没有改变前一个节点的下一个节点。 为什么排序后还能打印出正确答案呢? 如果它改变了前一个节点的下一个链接,那么在哪里?

感谢您的回答。

struct Node* swap(struct Node* ptr1, struct Node* ptr2)
{
    struct Node* tmp = ptr2->next;
    ptr2->next = ptr1;
    ptr1->next = tmp;
    return ptr2;
}

int bubbleSort(struct Node** head, int count)
{
    struct Node** h;
    int i, j, swapped;
  
    for (i = 0; i <= count; i++) {
  
        h = head;
        swapped = 0;
  
        for (j = 0; j < count - i - 1; j++) {
  
            struct Node* p1 = *h;
            struct Node* p2 = p1->next;
  
            if (p1->data > p2->data) {
  
                /* update the link after swapping */
                *h = swap(p1, p2);
                swapped = 1;
            }
  
            h = &(*h)->next;
        }
  
        /* break if the loop ended without any swap */
        if (swapped == 0)
            break;
    }
}

Below code is copy from geekforgeeks. I'm curious about the swap
It didn't change the previous node's next.
Why can it still print the right answer after sort?
If it has change previous node's next link, then where?

Thanks for answering.

struct Node* swap(struct Node* ptr1, struct Node* ptr2)
{
    struct Node* tmp = ptr2->next;
    ptr2->next = ptr1;
    ptr1->next = tmp;
    return ptr2;
}

int bubbleSort(struct Node** head, int count)
{
    struct Node** h;
    int i, j, swapped;
  
    for (i = 0; i <= count; i++) {
  
        h = head;
        swapped = 0;
  
        for (j = 0; j < count - i - 1; j++) {
  
            struct Node* p1 = *h;
            struct Node* p2 = p1->next;
  
            if (p1->data > p2->data) {
  
                /* update the link after swapping */
                *h = swap(p1, p2);
                swapped = 1;
            }
  
            h = &(*h)->next;
        }
  
        /* break if the loop ended without any swap */
        if (swapped == 0)
            break;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稀香 2025-01-19 06:32:02

它没有改变前一个节点的下一个节点。

它因赋值而改变

*h = swap(p1, p2);

双指针h指向前一个节点的数据成员next或通过指向它的指针间接通过引用传递给函数的指针头执行此语句

h = &(*h)->next;

后,

*h = swap(p1, p2);

前一个节点的数据成员 next 获取指针 p2 的值。指针p2指向的节点的数据成员next获取指针p1的值和数据成员next<指针p1指向的节点的/code>获取指针p2指向的节点的数据成员next的初始存储值代码>.

It didn't change the previous node's next.

It changes due to the assignment

*h = swap(p1, p2);

The double pointer h points to the data member next of the previous node or to the pointer head passed to the function by reference indirectly through pointer to it die to this statement

h = &(*h)->next;

After this assignment

*h = swap(p1, p2);

the data member next of the previous node gets the value of the pointer p2. The data member next of the node pointed to by the pointer p2 gets the value of the pointer p1 and the data member next of the node pointed to by the pointer p1 gets the initially stored value of the data member next of the node pointed to by the pointer p2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文