#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简单内核链表排序的更多相关文章

  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. 基于ROS_Arduino室内移动机器人SLAM实验测试

    纯手工搭建的机器人,因此外观并不美. 基于ROS(indigo)以及Arduino等搭建软硬件平台,包括语音.视觉.激光.码盘等传感器设备. 整体如下图所示: 底盘特写: 语音输入: Arduino模 ...

  2. STL - set和multiset

    set/multiset的简介 set是一个集合容器,其中所包含的元素是唯一的,集合中的元素按一定的顺序排列.元素插入过程是按排序规则插入,所以不能指定插入位置. set采用红黑树变体的数据结构实现, ...

  3. java面试之常见编程题

    1.编程实现:二分搜索算法 解答: public class SearchTest { /** 被搜索数据的大小 */ private static final int size = 5000000; ...

  4. "《算法导论》之‘图’":不带权二分图最大匹配(匈牙利算法)

    博文“二分图的最大匹配.完美匹配和匈牙利算法”对二分图相关的几个概念讲的特别形象,特别容易理解.本文介绍部分主要摘自此博文. 还有其他可参考博文: 趣写算法系列之--匈牙利算法 用于二分图匹配的匈牙利 ...

  5. C++语言之动态内存分配

    在C语言中,我们熟悉的内存分配与释放的最常用的接口分别是malloc , free .在C++中: 存在着更加方便的动态存储分配: 1.new 和delete 机制,new 它能更可靠控制存储区的分配 ...

  6. Google官方网络框架Volley实战——QQ吉凶测试,南无阿弥陀佛!

    Google官方网络框架Volley实战--QQ吉凶测试,南无阿弥陀佛! 这次我们用第三方的接口来做一个QQ吉凶的测试项目,代码依然是比较的简单 无图无真相 直接撸代码了,详细解释都已经写在注释里了 ...

  7. MongoDB 3.0新增特性一览

    转自:http://blog.sina.com.cn/s/blog_48c95a190102vedr.html 引言 在历经版本号修改(2.8版本直接跳到3.0版本)和11个rc版本之后,MongoD ...

  8. MQ队列管理器搭建(一)

    多应用单MQ使用场景 如上图所示,MQ独立安装,或者与其中一个应用同处一机.Application1与Application2要进行通信,但因为跨系统,所以引入中间件来实现需求.   Applicat ...

  9. Java + Selenium + TestNG + Maven

    环境准备: 1. Java: Install Java jdk: Version: java 1.8 or aboveConfigure Java Environment Variables:Add ...

  10. vue学习:props,scope,slot,ref,is,slot,sync等知识点

    1.ref :为子组件指定一个索引 ID,给元素或者组件注册引用信息.refs是一个对象,包含所有的ref组件. <div id="parent"> <user- ...