链表:冒泡排序指针(C)

发布于 2024-12-27 10:39:37 字数 1007 浏览 0 评论 0原文

我正在做一项大学作业。我正在尝试用 C 编写链表排序。我不允许交换值 - 只能交换指针。

这是我的排序功能:

struct node *sort_list(struct node *head) {

bool swapped ;
struct node *cur = head, *first ;

if ( head == NULL || head->next == NULL ) return head ;
else {
    do {
        swapped = false ;
        while ( cur != NULL && cur->next != NULL ){
            if (cur->value > cur->next->value){
                cur = swap_with_next( cur ) ;
                swapped = true ;
            }
            cur = cur->next ;
        }   
    } while (swapped == true) ;

}
return head ;

}

和交换功能:

 struct node *swap_with_next(struct node *n) {
      struct node *tmp ;
      tmp = n ;
      n = n->next ;
      tmp->next = n->next ;
      n->next = tmp ;
      return n ;
 }

问题: 输出不正确:

input:  5->2->NULL
output: 5->NULL

input:  9->1->5->2->8->3
output: 9->5->8

任何帮助将不胜感激!

I'm working on a university assignment. I am trying to write a linked-list sort in C. I am not allowed to swap values - only pointers.

Here is my sorting function:

struct node *sort_list(struct node *head) {

bool swapped ;
struct node *cur = head, *first ;

if ( head == NULL || head->next == NULL ) return head ;
else {
    do {
        swapped = false ;
        while ( cur != NULL && cur->next != NULL ){
            if (cur->value > cur->next->value){
                cur = swap_with_next( cur ) ;
                swapped = true ;
            }
            cur = cur->next ;
        }   
    } while (swapped == true) ;

}
return head ;

}

And the swap function:

 struct node *swap_with_next(struct node *n) {
      struct node *tmp ;
      tmp = n ;
      n = n->next ;
      tmp->next = n->next ;
      n->next = tmp ;
      return n ;
 }

Problem: Incorrect output:

input:  5->2->NULL
output: 5->NULL

input:  9->1->5->2->8->3
output: 9->5->8

Any help would be greatly appreciated!

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

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

发布评论

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

评论(2

人疚 2025-01-03 10:39:37

好的,假设有 3 个节点 A、B 和 C(按顺序)。您正在尝试交换 B 和 C。因此,您将指向 B 的指针传递给 swap 函数。现在,所有指针操作都是正确的,但是,您错过了一件事。完成交换后,A 的下一个指针应指向 C。你从来没有设置过这个。当你完成交换函数后,A的next指针仍然指向B,这显然是不正确的。这就是问题所在。

Ok, so, let's say there are 3 nodes A, B and C (in that order). You are trying to swap B and C. So, you are passing the pointer to B to the swap function. Now, all the pointer manipulations are correct but, you miss one thing. A's next pointer should point to C when you finish the swap. You never set this. When you finish the swap function, A's next pointer still points to B which is obviously not correct. That's the problem.

过气美图社 2025-01-03 10:39:37

您正在传回一个很好的指针,但是在交换中您正在操纵指针的副本,而不是您试图更改的“实际”指针。您正在尝试操作参数以在交换内部进行交换,但这只是更改在调用该函数时创建的该节点的副本。如果您想在函数内更改它,则需要参数是指向指针的指针。

You are passing back a pointer which is good, but in swap you are manipulating a copy of the pointer not the "actual" one you are trying to change. You are trying to manipulate the parameter to swap inside of swap but that is only changing a copy of that node that was created on calling the function. You need the parameter to be a pointer to the pointer, if you want to change it inside the function.

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