linux简单内核链表排序
- #include <stdio.h>
- #include <stdlib.h>
- #define container_of(ptr, type, mem)(type *)((unsigned long)ptr -(unsigned long)&((type *)NULL)->mem)
- struct person {
- struct person *next;
- struct person *pre;
- };
- struct boy {
- struct person p;
- int age;
- };
- struct person *creat(struct person *head, int age);
- struct person *choose_sort(struct person *head);
- struct person *insert_sort(struct person *head);
- void show(struct person *head);
- int main()
- {
- struct person *head = NULL;
- head = creat(head, );
- head = creat(head, );
- head = creat(head, );
- head = creat(head, );
- head = creat(head, );
- head = creat(head, );
- head = insert_sort(head);
- show(head);
- }
- struct person *creat(struct person *head, int age)
- {
- struct boy *tmp = NULL;
- struct person *find = NULL;
- tmp = (struct boy *)malloc(sizeof(struct boy));
- tmp->age = age;
- tmp->p.next = NULL;
- tmp->p.pre = NULL;
- if(NULL == head) {
- head = &tmp->p;
- head->next = head;
- head->pre = head;
- return head;
- }
- find = head;
- while(find->next != head) {
- find = find->next;
- }
- find->next = &tmp->p;
- tmp->p.pre = find;
- tmp->p.next = head;
- head->pre = &tmp->p;
- return head;
- }
- #if 0
- struct person *choose_sort(struct person *head)
- {
- struct person *tmp = NULL;
- struct boy *min = NULL;
- struct person *newhead = NULL;
- struct person *tail = NULL;
- struct boy *find = NULL;
- while(head) {
- tmp = head;
- min = container_of(head, struct boy, p);
- do {
- find = container_of(tmp, struct boy, p);
- if(find->age < min->age) {
- min = find;
- }
- tmp = tmp->next;
- } while(tmp != head);
- struct person *min_p = &min->p;
- if(min_p == head && head->next != head) {
- head = head->next;
- head->pre = min_p->pre;
- min_p->pre->next = head;
- min_p->pre = NULL;
- min_p->pre = NULL;
- }
- else {
- if(min_p == head && head->next == head) {
- head->next = NULL;
- head->pre = NULL;
- head = head->next;
- }
- else {
- min_p->pre->next = min_p->next;
- min_p->next->pre = min_p->pre;
- min_p->next = NULL;
- min_p->pre = NULL;
- }
- }
- if(NULL == newhead) {
- newhead = min_p;
- newhead->next = newhead;
- newhead->pre = newhead;
- tail = newhead;
- continue;
- }
- tail->next = min_p;
- min_p->pre = tail;
- min_p->next = newhead;
- newhead->pre = min_p;
- tail = min_p;
- }
- return newhead;
- }
- #else
- struct person *choose_sort(struct person *head)
- {
- struct person *tmp = NULL;
- struct boy *min = NULL;
- struct person *newhead = NULL;
- struct person *tail = NULL;
- struct boy *find = NULL;
- while(head) {
- tmp = head;
- min = container_of(head, struct boy, p);
- do {
- find = container_of(tmp, struct boy, p);
- if(find->age < min->age) {
- min = find;
- }
- tmp = tmp->next;
- } while(tmp != head);
- if(&min->p == head && head->next != head) {
- head = head->next;
- head->pre = min->p.pre;
- min->p.pre->next = head;
- min->p.pre = NULL;
- min->p.next = NULL;
- }
- else {
- if(&min->p == head && head->next == head) {
- head->next = NULL;
- head->pre = NULL;
- head = head->next;
- }
- else {
- min->p.pre->next = min->p.next;
- min->p.next->pre = min->p.pre;
- min->p.next = NULL;
- min->p.pre = NULL;
- }
- }
- if(NULL == newhead) {
- newhead = &min->p;
- newhead->next = newhead;
- newhead->pre = newhead;
- tail = newhead;
- continue;
- }
- tail->next = &min->p;
- min->p.pre = tail;
- min->p.next = newhead;
- newhead->pre = &min->p;
- tail = &min->p;
- }
- return newhead;
- }
- #endif
- struct person *insert_sort(struct person *head)
- {
- struct boy *tmp = NULL;
- struct boy *new = NULL;
- struct person *find = NULL;
- struct person *list = NULL;
- struct person *newhead = NULL;
- struct person *tail = NULL;
- struct boy *key = NULL;
- while(head) {
- find = head;
- tmp = container_of(find, struct boy, p);
- if(head->next == head) {
- head->next = NULL;
- head = head->next;
- find->pre = NULL;
- }
- else {
- head = head->next;
- head->pre = find->pre;
- find->pre->next = head;
- find->pre = NULL;
- find->next = NULL;
- }
- if(NULL == newhead) {
- newhead = find;
- newhead->pre = newhead;
- newhead->next = newhead;
- continue;
- }
- new = container_of(newhead, struct boy, p);
- if(tmp->age < new->age) {
- find->next = newhead;
- find->pre = newhead->pre;
- newhead->pre->next = find;
- newhead->pre = find;
- newhead = find;
- continue;
- }
- list = newhead;
- do {
- key = container_of(list, struct boy, p);
- if(key->age > tmp->age)
- break;
- list = list->next;
- } while(list != newhead);
- if(key->age < tmp->age) {
- list->next = find;
- find->pre = list;
- find->next = newhead;
- newhead->pre = find;
- }
- else {
- find->next = list;
- find->pre = list->pre;
- list->pre->next = find;
- list->pre = find;
- }
- }
- return newhead;
- }
- void show(struct person *head)
- {
- struct person *tmp = NULL;
- struct boy *find = NULL;
- tmp = head;
- do {
- find = container_of(tmp, struct boy, p);
- printf("age is %d\n", find->age);
- tmp = tmp->next;
- } while(tmp != head);
- printf("-------------------------------------\n");
- do {
- find = container_of(tmp, struct boy, p);
- printf("age is %d\n", find->age);
- tmp = tmp->pre;
- } while(tmp != head);
- }
linux简单内核链表排序的更多相关文章
- linux内核链表---挑战常规思维
一.普通链表 1.一般教材上的链表定义如下: struct node{ int content: node *next: }: 它将指针域放在链表节点中,上一个节点指针域中的值指向下一个节点的首地址, ...
- Linux 内核 链表 的简单模拟(2)
接上一篇Linux 内核 链表 的简单模拟(1) 第五章:Linux内核链表的遍历 /** * list_for_each - iterate over a list * @pos: the & ...
- Linux 内核 链表 的简单模拟(1)
第零章:扯扯淡 出一个有意思的题目:用一个宏定义FIND求一个结构体struct里某个变量相对struc的编移量,如 struct student { int a; //FIND(struct stu ...
- linux内核链表分析
一.常用的链表和内核链表的区别 1.1 常规链表结构 通常链表数据结构至少应包含两个域:数据域和指针域,数据域用于存储数据,指针域用于建立与下一个节点的联系.按照指针域的组织以及各个节 ...
- C语言 Linux内核链表(企业级链表)
//Linux内核链表(企业级链表) #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> ...
- 深入分析 Linux 内核链表--转
引用地址:http://www.ibm.com/developerworks/cn/linux/kernel/l-chain/index.html 一. 链表数据结构简介 链表是一种常用的组织有序数据 ...
- 深入分析 Linux 内核链表
转载:http://www.ibm.com/developerworks/cn/linux/kernel/l-chain/ 一. 链表数据结构简介 链表是一种常用的组织有序数据的数据结构,它通过指 ...
- Linux中的内核链表
链表中一般都要进行初始化.插入.删除.显示.释放链表,寻找节点这几个操作,下面我对这几个操作进行简单的介绍,因为我的能力不足,可能有些东西理解的不够深入,造成一定的错误,请各位博友指出. A.Linu ...
- Linux 内核链表实现和使用(一阴一阳,太极生两仪~)
0. 概述 学习使用一下 linux 内核链表,在实际开发中我们可以高效的使用该链表帮我们做点事, 链表是Linux 内核中常用的最普通的内建数据结构,链表是一种存放和操作可变数据元 素(常称为节点) ...
随机推荐
- STM32学习笔记(一)时钟和定时器
由于近期在准备海洋航行器比赛,正好趁此机会学习一下ARM,看到周围很多同学都在使用32,所以我也买了一块STM32F103ZET6,准备好好地学习一下. STM32的时钟系统相当的复杂,包含了5个时钟 ...
- 04_Nginx命令行参数,控制信号,Nginx启动、停止、重启命令
Nginx简单型,先关闭进程,修改你的配置后,重启进程. kill -QUIT `cat/usr/local/nginx/nginx.pid` ./nginx 2 重新加载配置文件,不重启进程, ...
- DiskLruCache硬盘缓存技术详解
上次讲了使用内存缓存LruCache去加载很多图片而不造成OOM,而这种缓存的特点是在应用程序运行时管理内存中的资源(图片)的存储和释放,如果LruCache中有一张图片被释放了,再次加载该图片时需要 ...
- Windows核心编程读书笔记1
今天特别困啊,这是为什么?!!刚刚把第一章看了一下,困到不行,所以写blog清醒一下. 第一章标题是“错误处理”,看了之后吓了一跳,难道第一章就讲这么高大上的东西?!不是不是,我现在的理解是,这章主要 ...
- Linux进程快照相关知识
查寻内核版本 uname -a // uname -r 进程快照 ps report a snapshot of the current processes USER ...
- 存储引擎-Buffered tree
Buffered-tree 也称为COLA,即cache-oblivious,可以不需要知道具体内存大小和一个块的大小,使用一套逻辑进行处理,因此内存大小可知,内存可能被临时占用去做其它事情. Buf ...
- Jbpm工作流(一)
了解一下什么是Jbpm及特点. jBPM,全称是Java Business Process Management,是一种基于J2EE的轻量级工作流管理系统.jBPM是公开源代码项目,它使用要遵循 Ap ...
- C++std函数之transform
/*//////////////////////////////// template < class InputIterator, class OutputIterator, class Un ...
- Asp.Net WebApi Swagger终极搭建
[PS:原文手打,转载说明出处,博客园] 关于为什么用Swagger 目前稍微有点规模的公司,已经从原先的瀑布流开发到了敏捷开发,实现前后端分离,为此后端工程师只关注写好Api即可,那程序员最讨厌的就 ...
- 【数据可视化之Flask】快速设计和部署Flask网站
Flask是Python应用于WEB开发的第三方开源框架,以设计简单高效著称.我也尝试过Django,相对于Flask显得更加全面同样也更加笨重,并且我也不需要它的后台管理功能,因此选择了Flask作 ...