创建链表时,就是插入排序
    链表排序
    先插入后排序

    1. #include <stdio.h>
    2. #include<stdlib.h>
    3. #include<math.h>
    4. #include<string.h>
    5. #include<stdbool.h>
    6. typedef struct Node {
    7. int data;
    8. struct Node* pnext;
    9. }NODE,*pNode; // 创建指针
    10. pNode CreateList(int n) { // 头插法
    11. int i;
    12. int data=0;
    13. pNode phead = NULL, pnew = NULL;
    14. for (i = 0; i < n; i++) {
    15. pnew = (pNode)malloc(sizeof(NODE));
    16. scanf("%d", &data);
    17. if (pnew != NULL) {
    18. pnew->data = data;
    19. pnew->pnext = phead;
    20. phead = pnew;
    21. }
    22. }
    23. return phead;
    24. }
    25. void PrintList(pNode p,int pos) {
    26. int i = 0;
    27. for(i=0;i<pos-1;i++)p = p->pnext;
    28. printf("%d\n", p->data);
    29. }
    30. void buble_sort(pNode p ,int n) {
    31. int i = 0;
    32. int j = 0;
    33. int temp = p->data;
    34. pNode tmp = p; // 创建的是指向结点的指针
    35. for (i = 0; i < n; i++) {
    36. for (j =0; j < n-i-1; j++) {
    37. if (p->data>= p->pnext->data) {
    38. temp = p->data;
    39. p->data = p->pnext->data;
    40. p->pnext->data = temp;
    41. }
    42. p = p->pnext;
    43. }
    44. p = tmp;
    45. }
    46. }
    47. int main() {
    48. int n;
    49. scanf("%d", &n);
    50. pNode head = CreateList(n);
    51. buble_sort(head, n);
    52. scanf("%d", &n);
    53. PrintList(head,n);
    54. return 0;
    55. }