C语言排序算法,请各位大神帮我看看这个程序哪里有问题?排序之后第一个结点的值没有动

2025-05-18 03:37:05
推荐回答(1个)
回答(1):

//=============冒泡排序====================
void bubbleSort(NODE *head)
{
    NODE *cur, *next, *end;
    cur = head -> next;
    end = NULL;
    while(cur != end)
    {
        for(next = cur->next; next != end; cur = cur -> next, next = next->next)
        {
            if(cur -> data > next -> data)
            {
                int tmp = cur->data;
                cur->data = next->data;
                next->data = tmp;
            }
        }
        end = cur;
        cur = head->next;
    }
}
int main()
{
    NODE *head,*p;
    head=create();
    p=head->next;
    bubbleSort(head);
    while(p)
    {
        printf("%d ",p->data);
        p=p->next;
    }
    printf("\n");
    return 0;
}