1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define container_of(ptr, type, mem)(type *)((unsigned long)ptr -(unsigned long)&((type *)NULL)->mem)
  5.  
  6. struct person {
  7. struct person *next;
  8. struct person *pre;
  9. };
  10.  
  11. struct boy {
  12. struct person p;
  13. int age;
  14. };
  15.  
  16. struct person *creat(struct person *head, int age);
  17. struct person *choose_sort(struct person *head);
  18. struct person *insert_sort(struct person *head);
  19. void show(struct person *head);
  20.  
  21. int main()
  22. {
  23. struct person *head = NULL;
  24. head = creat(head, );
  25. head = creat(head, );
  26. head = creat(head, );
  27. head = creat(head, );
  28. head = creat(head, );
  29. head = creat(head, );
  30. head = insert_sort(head);
  31. show(head);
  32.  
  33. }
  34. struct person *creat(struct person *head, int age)
  35. {
  36. struct boy *tmp = NULL;
  37. struct person *find = NULL;
  38.  
  39. tmp = (struct boy *)malloc(sizeof(struct boy));
  40. tmp->age = age;
  41. tmp->p.next = NULL;
  42. tmp->p.pre = NULL;
  43.  
  44. if(NULL == head) {
  45. head = &tmp->p;
  46. head->next = head;
  47. head->pre = head;
  48. return head;
  49. }
  50.  
  51. find = head;
  52. while(find->next != head) {
  53. find = find->next;
  54. }
  55.  
  56. find->next = &tmp->p;
  57. tmp->p.pre = find;
  58. tmp->p.next = head;
  59. head->pre = &tmp->p;
  60.  
  61. return head;
  62. }
  63. #if 0
  64. struct person *choose_sort(struct person *head)
  65. {
  66. struct person *tmp = NULL;
  67. struct boy *min = NULL;
  68. struct person *newhead = NULL;
  69. struct person *tail = NULL;
  70. struct boy *find = NULL;
  71.  
  72. while(head) {
  73. tmp = head;
  74. min = container_of(head, struct boy, p);
  75.  
  76. do {
  77. find = container_of(tmp, struct boy, p);
  78. if(find->age < min->age) {
  79. min = find;
  80. }
  81. tmp = tmp->next;
  82.  
  83. } while(tmp != head);
  84.  
  85. struct person *min_p = &min->p;
  86. if(min_p == head && head->next != head) {
  87. head = head->next;
  88. head->pre = min_p->pre;
  89. min_p->pre->next = head;
  90. min_p->pre = NULL;
  91. min_p->pre = NULL;
  92. }
  93. else {
  94. if(min_p == head && head->next == head) {
  95. head->next = NULL;
  96. head->pre = NULL;
  97. head = head->next;
  98. }
  99.  
  100. else {
  101. min_p->pre->next = min_p->next;
  102. min_p->next->pre = min_p->pre;
  103. min_p->next = NULL;
  104. min_p->pre = NULL;
  105. }
  106. }
  107. if(NULL == newhead) {
  108. newhead = min_p;
  109. newhead->next = newhead;
  110. newhead->pre = newhead;
  111. tail = newhead;
  112. continue;
  113. }
  114. tail->next = min_p;
  115. min_p->pre = tail;
  116. min_p->next = newhead;
  117. newhead->pre = min_p;
  118. tail = min_p;
  119. }
  120. return newhead;
  121. }
  122.  
  123. #else
  124. struct person *choose_sort(struct person *head)
  125. {
  126. struct person *tmp = NULL;
  127. struct boy *min = NULL;
  128. struct person *newhead = NULL;
  129. struct person *tail = NULL;
  130. struct boy *find = NULL;
  131.  
  132. while(head) {
  133. tmp = head;
  134. min = container_of(head, struct boy, p);
  135.  
  136. do {
  137. find = container_of(tmp, struct boy, p);
  138. if(find->age < min->age) {
  139. min = find;
  140. }
  141. tmp = tmp->next;
  142.  
  143. } while(tmp != head);
  144.  
  145. if(&min->p == head && head->next != head) {
  146. head = head->next;
  147. head->pre = min->p.pre;
  148. min->p.pre->next = head;
  149. min->p.pre = NULL;
  150. min->p.next = NULL;
  151. }
  152. else {
  153. if(&min->p == head && head->next == head) {
  154. head->next = NULL;
  155. head->pre = NULL;
  156. head = head->next;
  157. }
  158.  
  159. else {
  160. min->p.pre->next = min->p.next;
  161. min->p.next->pre = min->p.pre;
  162. min->p.next = NULL;
  163. min->p.pre = NULL;
  164. }
  165. }
  166. if(NULL == newhead) {
  167. newhead = &min->p;
  168. newhead->next = newhead;
  169. newhead->pre = newhead;
  170. tail = newhead;
  171. continue;
  172. }
  173. tail->next = &min->p;
  174. min->p.pre = tail;
  175. min->p.next = newhead;
  176. newhead->pre = &min->p;
  177. tail = &min->p;
  178. }
  179. return newhead;
  180. }
  181. #endif
  182.  
  183. struct person *insert_sort(struct person *head)
  184. {
  185. struct boy *tmp = NULL;
  186. struct boy *new = NULL;
  187. struct person *find = NULL;
  188. struct person *list = NULL;
  189. struct person *newhead = NULL;
  190. struct person *tail = NULL;
  191. struct boy *key = NULL;
  192.  
  193. while(head) {
  194. find = head;
  195. tmp = container_of(find, struct boy, p);
  196. if(head->next == head) {
  197. head->next = NULL;
  198. head = head->next;
  199. find->pre = NULL;
  200. }
  201. else {
  202. head = head->next;
  203. head->pre = find->pre;
  204. find->pre->next = head;
  205. find->pre = NULL;
  206. find->next = NULL;
  207. }
  208.  
  209. if(NULL == newhead) {
  210. newhead = find;
  211. newhead->pre = newhead;
  212. newhead->next = newhead;
  213. continue;
  214. }
  215. new = container_of(newhead, struct boy, p);
  216. if(tmp->age < new->age) {
  217. find->next = newhead;
  218. find->pre = newhead->pre;
  219. newhead->pre->next = find;
  220. newhead->pre = find;
  221. newhead = find;
  222. continue;
  223. }
  224.  
  225. list = newhead;
  226. do {
  227. key = container_of(list, struct boy, p);
  228. if(key->age > tmp->age)
  229. break;
  230. list = list->next;
  231. } while(list != newhead);
  232.  
  233. if(key->age < tmp->age) {
  234. list->next = find;
  235. find->pre = list;
  236. find->next = newhead;
  237. newhead->pre = find;
  238. }
  239. else {
  240. find->next = list;
  241. find->pre = list->pre;
  242. list->pre->next = find;
  243. list->pre = find;
  244. }
  245. }
  246. return newhead;
  247. }
  248.  
  249. void show(struct person *head)
  250. {
  251. struct person *tmp = NULL;
  252. struct boy *find = NULL;
  253. tmp = head;
  254. do {
  255. find = container_of(tmp, struct boy, p);
  256. printf("age is %d\n", find->age);
  257. tmp = tmp->next;
  258.  
  259. } while(tmp != head);
  260.  
  261. printf("-------------------------------------\n");
  262. do {
  263. find = container_of(tmp, struct boy, p);
  264. printf("age is %d\n", find->age);
  265. tmp = tmp->pre;
  266.  
  267. } while(tmp != head);
  268. }

linux简单内核链表排序的更多相关文章

  1. linux内核链表---挑战常规思维

    一.普通链表 1.一般教材上的链表定义如下: struct node{ int content: node *next: }: 它将指针域放在链表节点中,上一个节点指针域中的值指向下一个节点的首地址, ...

  2. Linux 内核 链表 的简单模拟(2)

    接上一篇Linux 内核 链表 的简单模拟(1) 第五章:Linux内核链表的遍历 /** * list_for_each - iterate over a list * @pos: the & ...

  3. Linux 内核 链表 的简单模拟(1)

    第零章:扯扯淡 出一个有意思的题目:用一个宏定义FIND求一个结构体struct里某个变量相对struc的编移量,如 struct student { int a; //FIND(struct stu ...

  4. linux内核链表分析

    一.常用的链表和内核链表的区别 1.1  常规链表结构        通常链表数据结构至少应包含两个域:数据域和指针域,数据域用于存储数据,指针域用于建立与下一个节点的联系.按照指针域的组织以及各个节 ...

  5. C语言 Linux内核链表(企业级链表)

    //Linux内核链表(企业级链表) #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> ...

  6. 深入分析 Linux 内核链表--转

    引用地址:http://www.ibm.com/developerworks/cn/linux/kernel/l-chain/index.html 一. 链表数据结构简介 链表是一种常用的组织有序数据 ...

  7. 深入分析 Linux 内核链表

    转载:http://www.ibm.com/developerworks/cn/linux/kernel/l-chain/   一. 链表数据结构简介 链表是一种常用的组织有序数据的数据结构,它通过指 ...

  8. Linux中的内核链表

    链表中一般都要进行初始化.插入.删除.显示.释放链表,寻找节点这几个操作,下面我对这几个操作进行简单的介绍,因为我的能力不足,可能有些东西理解的不够深入,造成一定的错误,请各位博友指出. A.Linu ...

  9. Linux 内核链表实现和使用(一阴一阳,太极生两仪~)

    0. 概述 学习使用一下 linux 内核链表,在实际开发中我们可以高效的使用该链表帮我们做点事, 链表是Linux 内核中常用的最普通的内建数据结构,链表是一种存放和操作可变数据元 素(常称为节点) ...

随机推荐

  1. STM32学习笔记(一)时钟和定时器

    由于近期在准备海洋航行器比赛,正好趁此机会学习一下ARM,看到周围很多同学都在使用32,所以我也买了一块STM32F103ZET6,准备好好地学习一下. STM32的时钟系统相当的复杂,包含了5个时钟 ...

  2. 04_Nginx命令行参数,控制信号,Nginx启动、停止、重启命令

     Nginx简单型,先关闭进程,修改你的配置后,重启进程. kill -QUIT `cat/usr/local/nginx/nginx.pid` ./nginx 2 重新加载配置文件,不重启进程, ...

  3. DiskLruCache硬盘缓存技术详解

    上次讲了使用内存缓存LruCache去加载很多图片而不造成OOM,而这种缓存的特点是在应用程序运行时管理内存中的资源(图片)的存储和释放,如果LruCache中有一张图片被释放了,再次加载该图片时需要 ...

  4. Windows核心编程读书笔记1

    今天特别困啊,这是为什么?!!刚刚把第一章看了一下,困到不行,所以写blog清醒一下. 第一章标题是“错误处理”,看了之后吓了一跳,难道第一章就讲这么高大上的东西?!不是不是,我现在的理解是,这章主要 ...

  5. Linux进程快照相关知识

    查寻内核版本 uname  -a    //  uname  -r 进程快照 ps       report a snapshot of the current processes USER     ...

  6. 存储引擎-Buffered tree

    Buffered-tree 也称为COLA,即cache-oblivious,可以不需要知道具体内存大小和一个块的大小,使用一套逻辑进行处理,因此内存大小可知,内存可能被临时占用去做其它事情. Buf ...

  7. Jbpm工作流(一)

    了解一下什么是Jbpm及特点. jBPM,全称是Java Business Process Management,是一种基于J2EE的轻量级工作流管理系统.jBPM是公开源代码项目,它使用要遵循 Ap ...

  8. C++std函数之transform

    /*//////////////////////////////// template < class InputIterator, class OutputIterator, class Un ...

  9. Asp.Net WebApi Swagger终极搭建

    [PS:原文手打,转载说明出处,博客园] 关于为什么用Swagger 目前稍微有点规模的公司,已经从原先的瀑布流开发到了敏捷开发,实现前后端分离,为此后端工程师只关注写好Api即可,那程序员最讨厌的就 ...

  10. 【数据可视化之Flask】快速设计和部署Flask网站

    Flask是Python应用于WEB开发的第三方开源框架,以设计简单高效著称.我也尝试过Django,相对于Flask显得更加全面同样也更加笨重,并且我也不需要它的后台管理功能,因此选择了Flask作 ...