我正在做大学任务.我试图在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 …Run Code Online (Sandbox Code Playgroud)