【1】C++实现内核链表功能
来源:互联网 发布:如何做一个网络问卷 编辑:程序博客网 时间:2024/05/20 09:26
内核链表:
特点:节点的指针域指向下一个节点/上一个节点的指针域,由此可以统一指针的类型,具有一般通用性,使用指针域跟数据入口地址【设为0地址】的offset来寻找数据入口,从而输出节点数据;内核链表一般为双向循环链表。
C++实现内核链表功能的例子,由此此处是参考C语言编写而成的,其中形参所用的为指针形参而不是引用,后期有时间再来修改
/*******************************************************************Author:OJ_GDUTFunction:imitate the linked list of kernelDate:2016-03-27********************************************************************/#include <iostream>#include <typeinfo>using namespace std;/* *define the pointer field */struct list_head{list_head *prev;list_head *next;};list_head head_list;//define the linked list head,此处不能声明为指针,因为如果声明为指针则并没有为struct list_head分配栈空间/* *test parameters */#if 1class score{friend int main(void);private:int num;int english;int math;public:list_head list;}stu1, stu2, stu3;#elsestruct score{int num;int english;int math;list_head list;}stu1, stu2, stu3;#endiflist_head *pos;//define the loop cursor,此处可以声明为指针,因为之后会对其进行替换 score *tmp;//define the a pointer that points to score,此处可以声明为指针,因为之后会对其进行替换 /*****************************************************Function name: INIT_LIST_HEADFunction:init the linked list head Parameters:struct list_head *list:the list_head that will be init*******************************************************/static inline void INIT_LIST_HEAD(list_head *list){list->next = list;list->prev = list;//如果不对head_list进行初始化,此处的list->next/prev会无定义,从而出现访问冲突}/*****************************************************Function name: list_add_tailFunction:add the list from the tailParameters:list_head *l_new: the new nodelist_head *head:the list head*******************************************************/void list_add_tail(list_head *l_new, list_head *head){list_head *prev = head->prev;list_head *next = head;prev->next = l_new;next->prev = l_new;l_new->prev = prev;l_new->next = next;}/*****************************************************Function name: list_for_eachFunction:add the list from the tailParameters:list_head *pos:the &struct list_head to use as a loop cursorlist_head *head:the head for your list*******************************************************/#define list_for_each(pos, head) for(pos = head->next; pos != head; pos = pos->next)/*****************************************************Function name: list_delFunction:deletes entry from listParameters:list_head *entry:the element to delete from the list*******************************************************/static inline void list_del(list_head *entry){list_head *prev = entry->prev;list_head *next = entry->next;next->prev = prev;prev->next = next;entry->prev = nullptr;entry->next = nullptr;}/*****************************************************Function name: mainFunction:test the linked_list Parameters:void*******************************************************/int main(void){/*1.初始化表头*/INIT_LIST_HEAD(&head_list);/*2.定义节点并插入节点*/stu1.num = 1;stu1.english = 91;stu1.math = 81;list_add_tail(&(stu1.list), &head_list);stu2.num = 2;stu2.english = 92;stu2.math = 82;list_add_tail(&(stu2.list), &head_list);stu3.num = 3;stu3.english = 93;stu3.math = 83;list_add_tail(&(stu3.list), &head_list);/*3.遍历节点并取出节点*//* * 基本思路:通过decltype获取数据类型,并定义一个位于0地址的该数据, * 通过计算指针域到0地址处的差值,可以得到数据入口与指针 * 域的差值,从而可以通过指针域-差值的方式来读取节点中的 * 数据,从而达到通用双向循环链表的作用,此处是精华。 */list_for_each(pos, (&head_list)){decltype(tmp) test = 0;unsigned i = (unsigned)(&(test->list)) - (unsigned)test;tmp = (decltype(tmp))((unsigned)pos - i);cout << "No." << tmp->num << " " << "english is " << tmp->english << "," << "math is " << tmp->math << endl;}/*4.删除节点*/list_del(&(stu1.list));list_del(&(stu2.list));list_del(&(stu3.list));/*5.再次取出节点,检验节点是否删除*/list_for_each(pos, (&head_list)){decltype(tmp) test;unsigned i = (unsigned)(&(test->list)) - (unsigned)test;tmp = (decltype(tmp))((unsigned)pos - i);cout << "No." << tmp->num << " english is " << tmp->english << ",math is " << tmp->math << endl;}system("pause");return 0;}//#define l_offsetof(TYPE, MEMBER) ((size_t)&((TYPE*)0)->MEMBER)//#define container_of(ptr, type, member) ({const typeid(((type *)0)->member)*_mptr = (ptr); (type*)((char*)_mptr-l_offsetof(type,member));})/*****************************************************Function name: list_entryFunction:get the data of listParameters:list_head *prt: the &struct list_head pointertype:the type of the struct this embeded inmember:the name of the list_struct within the struct *******************************************************///#define list_entry(ptr, type, member) ({type *test; unsigned i = test - &(test->member); ptr - i;})
遇到的问题:
1.
此处的使用到了强制转换,能不能想办法去掉强制转换,暂时还没想到办法,之后想到了再来解决。
0 0
- 【1】C++实现内核链表功能
- 用C实现基本链表功能
- java中链表功能的实现
- 数据结构链表功能的实现
- mybatis实现分表功能
- Python中实现map+链表功能的方法
- 单链表功能大全
- 链表功能的增加
- 单链表功能大全
- php + mysql 实现分表功能【copy】
- C语言-----循环双向链表(增加文件读写链表功能)
- 数据结构--环队实现线性表功能实现…
- c语言数据结构线性表之顺序表功能函数
- j2ee实现循环更新表功能,该如何解决
- NPOI导出Excel表功能实现(多个工作簿)
- sharding-jdbc结合mybatis实现分库分表功能
- 自己写了一个链表功能还不完善但是简单的增删改查功能都已经实现了
- [转]mysql临时表功能
- 嵌入式C语言一些用法
- linux下启动tomcate
- template使用总结
- 基于hive的日志数据统计实战
- Android 之窗口小部件高级篇--App Widget 之 RemoteViews
- 【1】C++实现内核链表功能
- mysql ------ 存储过程
- myeclipse 常用快捷键
- 计算机病毒
- 对于要不断使用的uiview一般通过懒加载的形式,而不是创建一个销毁一个
- 20个设计模式和软件设计面试问题
- 张老师:4.传统线程同步通讯技术
- 利用metaclass实现python的aop
- IOException while loading persisted sessions异常