【问题标题】:Insertion Sort on Linked List in CC中链表的插入排序
【发布时间】:2020-07-16 13:46:55
【问题描述】:

我正在尝试使用以下函数在 C 中的链表上执行插入排序,它陷入了无限循环。我调试了代码,发现它适用于第一遍,而在第二遍中卡在无限循环中。

void insertion_sort()//Insertion Sort function
{
    struct Node *p = root->next;

    struct Node *a = NULL, *b = NULL;

    while(p != NULL)
    {
        a = root;

        while(a != p)
        {
            b = a;
            a = a->next;
        }

        if(b != NULL)
            b->next = a->next;
        a->next = NULL;

        struct Node *q = root;
        struct Node* r = NULL;

        while(p->data > q->data)
        {
            r = q;
            q = q->next;
        }
        p->next = q;
        if(r != NULL)
            r->next = p;

        p = p->next;
    }

}

【问题讨论】:

  • 我马上看到的一个问题:while(a != p) 循环更改 a 但不检查它是否会变为 NULL。我有点惊讶你没有遇到段错误。与while(p->data > q->data) 相同:它更改q,但从不检查q 是否为NULL。解决这些问题。如果您仍然有一个无限循环,在您的调试过程中,检查while 循环条件以确保它们朝着最终会产生错误测试的方向前进。
  • 为什么函数返回void?为什么不接受任何争论?为什么 root 是全局变量?

标签: c sorting linked-list infinite-loop insertion-sort


【解决方案1】:

对于初学者来说,当函数依赖于全局变量时是个坏主意。例如,对于您的程序,这意味着您不能在一个程序中拥有两个列表。

我看不到函数insertion_sort 中指针root 的更改位置。因此,即使所有其他代码都有效,但该函数是不正确的,因为当指向的节点的值无序时,它不会更改指针 root

我可以建议以下演示程序中显示的以下解决方案。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

struct Node
{
    int data;
    struct Node *next;
};

int push_front( struct Node **head, int data )
{
    struct Node *node = malloc( sizeof( struct Node ) );
    int success = node != NULL;
    
    if ( success )
    {
        node->data = data;
        node->next = *head;
        
        *head = node;
    }
    
    return success;
}

void insertion_sort( struct Node **head )
{
    for ( struct Node **current = head; *current != NULL; )
    {
        struct Node **sorted = head;
        
        while ( *sorted != *current && !( ( *current )->data < ( *sorted )->data ) )
        {
            sorted = &( *sorted )->next;
        }
        
        if ( *sorted != *current )
        {
            struct Node *tmp = *current;
            *current = ( *current )->next;
            tmp->next = *sorted;
            *sorted = tmp;
        }
        else
        {
            current = &( *current )->next;
        }
    }
}

FILE * output( struct Node *head, FILE *fp )
{
    for ( ; head != NULL; head = head->next )
    {
        fprintf( fp, "%d -> ", head->data );
    }
    
    fputs( "null", fp );
    
    return fp;
}

int main(void) 
{
    enum { N = 13 };
    
    struct Node *head = NULL;
    
    srand( ( unsigned int )time( NULL ) );
    
    for ( int i = 0; i < N; i++ )
    {
        push_front( &head, rand() % N );    
    }
    
    fputc( '\n', output( head, stdout ) );
    
    insertion_sort( &head );
    
    fputc( '\n', output( head, stdout ) );
    
    return 0;
}

程序输出可能看起来像

1 -> 12 -> 0 -> 4 -> 0 -> 12 -> 3 -> 7 -> 12 -> 2 -> 5 -> 9 -> 7 -> null
0 -> 0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 7 -> 7 -> 9 -> 12 -> 12 -> 12 -> null

【讨论】:

    猜你喜欢
    • 2013-04-04
    • 2012-12-29
    • 2016-08-26
    • 1970-01-01
    • 2020-12-11
    • 1970-01-01
    • 1970-01-01
    • 2020-06-13
    • 1970-01-01
    相关资源
    最近更新 更多