[关闭]
@okokme 2018-07-21T15:08:13.000000Z 字数 567 阅读 535

链表排序

c语言


单链表进行冒泡排序

  1. int count(Node *pHead)
  2. {
  3. Node *temp;
  4. int count = 0;
  5. temp = pHead->next;
  6. if(pHead == NULL || pHead->next == NULL)
  7. return 0; //如果头结点为空,返回0节点
  8. while(temp != NULL)
  9. {
  10. count++;
  11. temp = temp->next;
  12. }
  13. return count; //返回目标节点数
  14. }
  15. Node *ascending_sort(Node *pHead,int count)
  16. {
  17. Node *temp;
  18. while(count--) //外层循环
  19. {
  20. for(temp = pHead;temp->next->next != NULL;temp = temp->next) //内层循环
  21. {
  22. if(temp->next->number > temp->next->next->number) //如果符合交换条件
  23. {
  24. Node *cur = temp->next;
  25. Node *loop = temp->next->next;
  26. cur->next = loop->next;
  27. temp->next = loop;
  28. loop->next = cur;
  29. }
  30. }
  31. }
  32. temp->next->next = NULL;
  33. printf("\t\t\t\tThe data has been sorted!\n");
  34. return pHead;
  35. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注