#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. 使用API获取(默认付款条件和到期日)

    1. 目的:使用API取到应收事务处理的付款条件(实现标准功能的付款条件和到期日)2. 实现方法:调用 ARP_TRX_DEFAULTS_3.get_term_default获得付款条件 3.实现代码 ...

  2. Developing User Interfaces

      Developing a User Interface with ADF Faces Purpose This tutorial covers developing the user interf ...

  3. Android特效专辑(十)——点击水波纹效果实现,逻辑清晰实现简单

    Android特效专辑(十)--点击水波纹效果实现,逻辑清晰实现简单 这次做的东西呢,和上篇有点类似,就是用比较简单的逻辑思路去实现一些比较好玩的特效,最近也是比较忙,所以博客更新的速度还得看时间去推 ...

  4. 安卓TV开发(八) 移动智能终端多媒体爬虫技术 获取加载网页视频源

    转载请标明出处:http://blog.csdn.net/sk719887916/article/details/40049137,作者:skay 从上一篇学习中,学习了多媒体技术中的怎么去用josu ...

  5. vim配置文件(本人喜欢的风格)

    在/etc/vimrc这个文件 if v:lang =~ "utf8$" || v:lang =~ "UTF-8$" set fileencodings=utf ...

  6. javascript语言扩展:可迭代对象(2)

    在文章迭代器(1)中我们简单介绍了如何创建一个可迭代对象:出于某种考虑你可能想从可迭代对象中显式获取一个迭代器对象,这时你可以调用Iterator()函数(该函数是定义在JavaScript 1.7中 ...

  7. ORM框架--GreenDao 3.0基本使用指南

    0. ORM框架--GreenDao 3.0基本使用指南 1. Gradle 的配置 这里可以参照官方的文档进行最新的配置(本示例的版本等你看到可能就不是最新的了),但是值得注意的一点是,GreenD ...

  8. FCL源码中数组类型的学习及排序函数Sort函数的分析

    Array 是所有数组的基类ArrayList 解决了所有Array 类的缺点    能动态扩容, 但是类型不安全的,而是会有装箱与拆箱的性能开销List<T> 则是解决了ArrayLis ...

  9. JVM学习--(七)性能监控工具

    前言 工欲善其事必先利其器,性能优化和故障排查在我们大都数人眼里是件比较棘手的事情,一是需要具备一定的原理知识作为基础,二是需要掌握排查问题和解决问题的流程.方法.本文就将介绍利用性能监控工具,帮助开 ...

  10. SpringBoot的第一个例子

    1. 安装springboot的开发IDE,IntelliJ IDEA 2016.3.1这个工具,在IDE的官网上可以下载最新版本.https://www.jetbrains.com/idea/#ch ...