写一个函数,实现链表的排序(使链表中元素的从无序到有序,要求从小到大);单链表

 int main(int argc, const char *argv[])                            
 {                                                                        
     linklist H;                                                                   
     int a[] = {1,5,9,4,11,96,10,6,75};      
     int i;                                                
                                                                       
     H = list_create();  //建立新链表                    
     if (H == NULL)                                         
         return -1;                                                 
                                                                 
     for (i = 0;i < sizeof(a)/sizeof(int); i+                         
         list_tail_insert(H,a[i]);    //建立新节点、尾部插入                         
     }                                           
     list_show(H);  //遍历链表                                        
                                                                
     printf("sort:\n");                                           
     list_sort(H);//排序                                         
     list_show(H);                                               
                                                     
     list_free(H);                         
                                             
     return 0;                                                     
3 }
 int list_sort(linklist H){
     linklist p,q,r;
 
     if (H == NULL){
         printf("H is NULL\n");
         return -1;
     }
 
     if (H->next == NULL)
         return 0;
 //赋值
     p = H->next;
     q = H->next->next;
     r = H;
     H->next = NULL;
 //插入第一个节点
     p->next = H->next;
     H->next = p;
 //与已插入节点比较、并按大小插入
     while(q){
         p = q;
         q = q->next;
         r = H;                                                                                                                                    
         while (r->next){
             if (r->next->data < p->data){
                 r = r->next;
             }else{
                 break;
             }
         }
             p->next = r->next;
             r->next = p;
     }
     return 0;
 }

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面是一个基于插入排序链表排序的示例代码,使用C语言实现: ```c #include <stdio.h> #include <stdlib.h> typedef struct ListNode { int val; struct ListNode* next; } ListNode; ListNode* insertionSortList(ListNode* head) { // 如果链表为空或只有一个节点,直接返回 if (head == NULL || head->next == NULL) { return head; } ListNode* dummy = (ListNode*)malloc(sizeof(ListNode)); // 创建一个虚拟头节点 dummy->next = head; ListNode* lastSorted = head; // 表示已经有序链表尾节点 ListNode* cur = lastSorted->next; // 表示待排序的节点 while (cur) { if (lastSorted->val <= cur->val) { // 如果已排序节点的值小于等于待排序节点的值,则直接更新lastSorted指针 lastSorted = lastSorted->next; } else { // 否则需要在已排序链表找到待排序节点的插入位置 ListNode* prev = dummy; while (prev->next->val <= cur->val) { // 寻找待排序节点的插入位置 prev = prev->next; } lastSorted->next = cur->next; // 先将待排序节点从原位置删除 cur->next = prev->next; // 将待排序节点插入到prev和prev->next之间 prev->next = cur; } cur = lastSorted->next; } return dummy->next; } // 创建一个链表 ListNode* createList(int* arr, int n) { if (n == 0) { // 如果数组为空,返回NULL return NULL; } ListNode* head = (ListNode*)malloc(sizeof(ListNode)); head->val = arr[0]; head->next = NULL; ListNode* cur = head; for (int i = 1; i < n; i++) { ListNode* newNode = (ListNode*)malloc(sizeof(ListNode)); newNode->val = arr[i]; newNode->next = NULL; cur->next = newNode; cur = cur->next; } return head; } // 打印链表 void printList(ListNode* head) { ListNode* cur = head; while (cur) { printf("%d ", cur->val); cur = cur->next; } printf("\n"); } int main() { int arr[] = {4, 2, 1, 3}; int n = sizeof(arr) / sizeof(int); ListNode* head = createList(arr, n); printf("排序前:"); printList(head); ListNode* sortedHead = insertionSortList(head); printf("排序后:"); printList(sortedHead); return 0; } ``` 在这个示例代码,我们使用了插入排序的思想,每次将一个排序节点插入到已排序链表的合适位置。时间复杂度为 $O(n^2)$。 希望这个示例代码能够帮助你实现链表排序

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值