Linux内核双向循环链表分析
来源:互联网 发布:浩辰cad建筑软件 编辑:程序博客网 时间:2024/05/21 14:51
#include <stdio.h>struct list_head{struct list_head *next;struct list_head *prev;};struct score{int num;int math;struct list_head list;};#define list_for_each(pos, head) \for (pos = (head)->next; pos != (head); pos = pos->next)#define offsetof(TYPE, MEMBER)((size_t) &((TYPE*)0)->MEMBER)#define container_of(ptr, type, member) ({\const typeof( ((type *)0)->member ) *__mptr = (ptr);\(type *)( (char *)__mptr - offsetof(type,member) );})#define list_entry(ptr, type, member)\container_of(ptr, type, member)static void INIT_LIST_HEAD(struct list_head *list){list->prev = list;list->next = list;}static void __list_add(struct list_head *new, struct list_head *prev, struct list_head *next){next->prev = new;new->next = next;new->prev = prev;prev->next = new;}static void list_add_tail(struct list_head *new, struct list_head *head){__list_add(new, head->prev, head);}static void list_add(struct list_head *new, struct list_head *head){__list_add(new, head, head->next);}static void __list_del(struct list_head *prev, struct list_head *next){next->prev = prev;prev->next = next;}static void list_del(struct list_head *entry){__list_del(entry->prev, entry->next);} int main(){struct score stu1;struct score stu2;struct score stu3;struct score stu4;struct score stu5;struct score stu6;struct score *temp;struct list_head *pos;struct list_head score_head;INIT_LIST_HEAD(&score_head);stu1.num = 1;stu2.num = 2;stu3.num = 3;stu4.num = 4;stu5.num = 5;stu6.num = 6;stu1.math = 60;stu2.math = 70;stu3.math = 80;stu4.math = 90;stu5.math = 20;stu6.math = 30;//在表尾部插入 list_add_tail(&(stu1.list), &score_head);list_add_tail(&(stu2.list), &score_head);list_add_tail(&(stu3.list), &score_head);list_add_tail(&(stu4.list), &score_head);list_for_each(pos, &score_head){temp = list_entry(pos, struct score, list);printf("num = %d, math = %d\n", temp->num, temp->math);}printf("\n");//在表头插入 list_add(&(stu5.list), &score_head);list_add(&(stu6.list), &score_head);list_for_each(pos, &score_head){temp = list_entry(pos, struct score, list);printf("num = %d, math = %d\n", temp->num, temp->math);}printf("\n");//删除list_del(&(stu5.list)); list_for_each(pos, &score_head){temp = list_entry(pos, struct score, list);printf("num = %d, math = %d\n", temp->num, temp->math);}printf("\n");return 0;}
运行效果:
内核双链表效果图:
大体的效果图就是如此,增加一个节点,删除一个节点都是基于这个模型展开的。
读者可以手动画画增加和删除的操作。
其实关于内核中链表的操作还有很多的函数,目前就分析这几个。其余留给自己尝试。
0 0
- Linux内核双向循环链表分析
- Linux内核-双向循环链表代码分析
- linux内核双向循环链表实例
- Linux内核2.6.14源码分析-双向循环链表代码分析(巨详细)
- Linux内核2.6.14源码分析-双向循环链表代码分析
- Linux内核2.6.14源码分析-双向循环链表代码分析(巨详细)
- Linux内核2.6.14源码分析-双向循环链表代码分析
- Linux内核2.6.14源码分析-双向循环链表代码分析
- Linux内核2.6.14源码分析-双向循环链表代码分析(巨详细)
- Linux内核2.6.14源码分析-双向循环链表代码分析(巨详细)
- [精华]]Linux内核2.6.14源码分析-双向循环链表代码分析(转)
- Linux内核2.6.14源码分析-双向循环链表代码分析(巨详细)
- 内核双向循环链表
- 详解Linux内核之双向循环链表(转载)
- 详解Linux内核之双向循环链表
- 详解Linux内核之双向循环链表(转)
- linux内核的数据结构:1 双向循环链表
- 详解Linux内核之双向循环链表
- css 让两个div重叠
- 3.在Ubuntu上为Android增加硬件抽象层(HAL)模块访问Linux内核驱动程序
- Fork and Exec
- 推荐!国外程序员整理的 C++ 资源大全
- Python线程池简单实现
- Linux内核双向循环链表分析
- hdu 1423 Greatest Common Increasing Subsequence 最大公共上升子序列 DP
- 4.在Ubuntu为Android硬件抽象层(HAL)模块编写JNI方法提供Java访问硬件服务接口
- 搭建PHP环境
- 5.在Ubuntu上为Android系统的Application Frameworks层增加硬件访问服务
- SQL从一张表的某个字段更新为另一张表的字段
- [LeetCode]Valid Palindrome
- 6.在Ubuntu上为Android系统内置Java应用程序测试Application Frameworks层的硬件服务
- 【数据结构】静态链表