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 内核中常用的最普通的内建数据结构,链表是一种存放和操作可变数据元 素(常称为节点) ...
随机推荐
- FPGA学习笔记(一)Verilog语法基础
一.变量类型 ①数值 数值表示采用 <二进制位数>'<数值表示的进制><数值>的结构. 其中进制可以为b.o.d.h分别代表二.八.十.十六进制. 例如22'd0代 ...
- 图像边缘检测--OpenCV之cvCanny函数
图像边缘检测--OpenCV之cvCanny函数 分类: C/C++ void cvCanny( const CvArr* image, CvArr* edges, double threshold1 ...
- "《算法导论》之‘队列’":队列的三种实现(静态数组、动态数组及指针)
本文有关栈的介绍部分参考自网站数据结构. 1. 队列 1.1 队列的定义 队列(Queue)是只允许在一端进行插入,而在另一端进行删除的运算受限的线性表. (1)允许删除的一端称为队头(Front) ...
- 详解Linux2.6内核中基于platform机制的驱动模型 (经典)
[摘要]本文以Linux 2.6.25 内核为例,分析了基于platform总线的驱动模型.首先介绍了Platform总线的基本概念,接着介绍了platform device和platform dri ...
- 【图片版】学习CSS网格布局
简言 CSS网格布局(Grid)是一套二维的页面布局系统,它的出现将完全颠覆页面布局的传统方式.传统的CSS页面布局 一直不够理想.包括table布局.浮动.定位及内联块等方式,从本质上都是Hack的 ...
- 赋给个人账户sudo的全部root执行权限
sudo visudo 输入root账户密码后,会自动打开sudoers文件编辑 在root ALL=(ALL) ALL此行下增加一行 user ALL=(ALL) NOPASSW ...
- 使用 Helm - 每天5分钟玩转 Docker 容器技术(163)
Helm 安装成功后,可执行 helm search 查看当前可安装的 chart. 这个列表很长,这里只截取了一部分.大家不禁会问,这些 chart 都是从哪里来的? 前面说过,Helm 可以像 a ...
- Spring Boot + Jersey发生FileNotFoundException (No such file or directory)
我在使用Spring Boot + Jersey 项目,解决了上一篇随笔中的FileNotFoundException,然后又报了一个FileNotFoundException,不过报错信息不一样了 ...
- 【转载】tomcat+nginx+redis实现均衡负载、session共享(一)
http://www.cnblogs.com/zhrxidian/p/5432886.html 在项目运营时,我们都会遇到一个问题,项目需要更新时,我们可能需先暂时关闭下服务器来更新.但这可能会出现一 ...
- AbstractQueuedSynchronizer 原理分析 - Condition 实现原理
1. 简介 Condition是一个接口,AbstractQueuedSynchronizer 中的ConditionObject内部类实现了这个接口.Condition声明了一组等待/通知的方法,这 ...