Chinaunix首页 | 论坛 | 博客
  • 博客访问: 831854
  • 博文数量: 157
  • 博客积分: 542
  • 博客等级: 中士
  • 技术积分: 1696
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-21 20:21
文章分类
文章存档

2017年(1)

2016年(2)

2015年(6)

2014年(42)

2013年(77)

2012年(19)

2011年(10)

分类: LINUX

2014-07-01 17:35:10

这里实现的复杂度是O(n^2)的,后续有时间不上更高效的。

实现的流程主要就是利用类似冒泡排序的思想,每一次遍历找出最小值的那个节点now,此时记录该节点的前一个节点指针prev(这里只要前一个就可以了,因为找到的这个可以用now = prev->next来获得,这样节省空间),然后使now掉链,把它挂载到已经排好序的节点的后面。然后重新循环找到无序链中的最小的节点。


点击(此处)折叠或打开

  1. typedef struct linknd_s {
  2.         int data;
  3.         struct linknd_s *next;
  4. }linknd_t;

  5. void create_link(linknd_t **head_t)
  6. {
  7.         int count = 0;
  8.         linknd_t *head = *head_t;
  9.         if (head == NULL) {
  10.                 head = malloc(sizeof(linknd_t));
  11.                 if (head == NULL) {
  12.                         printf("error create link\n");
  13.                         return;
  14.                 }

  15.                 scanf("%d", &head->data);
  16.                 head->next = NULL;
  17.         }
  18.         linknd_t *t = head;
  19.         while (count != 9) {
  20.                 linknd_t *node = malloc(sizeof(linknd_t));
  21.                 if (node == NULL) {
  22.                         printf("count = %d\n", count);
  23.                         return;
  24.                 }
  25.                 printf("enter the linkdata:\n");
  26.                 scanf("%d", &node->data);
  27.                 t->next = node;
  28.                 node->next = NULL;
  29.                 t = node;
  30.                 count++;
  31.         }
  32.         *head_t = head;
  33.         return;
  34. }

  35. void link_sort(linknd_t **head)
  36. {
  37.         linknd_t *tmp, *prev = NULL, *head_t = NULL;
  38.         linknd_t start;

  39.         if (*head == NULL)
  40.                 return;

  41.         start.next = *head;
  42.         head_t = &start;
  43.         while(head_t && head_t->next) {
  44.                 tmp = head_t->next;
  45.                 prev = head_t;
  46.                 while(tmp->next) {
  47.                         if (tmp->next->data < prev->next->data) {
  48.                                 prev = tmp;
  49.                         }
  50.                         tmp = tmp->next;
  51.                 }

  52.                 if (prev != head_t){
  53.                         linknd_t *s = prev->next;
  54.                         prev->next = s->next;
  55.                         s->next = head_t->next;
  56.                         head_t->next = s;

  57.                 }
  58.                 head_t = head_t->next;
  59.         }

  60.         *head = start.next;
  61.         return;
  62. }


  63. int main(void)
  64. {
  65.         printf("create the link list\n");
  66.         linknd_t *head = NULL, *tmp;
  67.         create_link(&head);
  68.         tmp = head;

  69.         while (head) {
  70.                 printf("%d\n", head->data);
  71.                 head = head->next;
  72.         }

  73.         link_sort(&tmp);

  74.         while (tmp) {
  75.                 printf("%d\n", tmp->data);
  76.                 tmp = tmp->next;
  77.         }
  78.         return 0;
  79. }

阅读(1391) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~