// 实验:;linux内核嵌入式链表
#include <iostream>
#include <string>
struct list_head {
struct list_head* next;
struct list_head *prev;
};
#if 0
// 案例1: struct list_head StuLis放在 struct Student的首部
struct Student
{
struct list_head StuList;
std::string m_name;
int m_age;
};
int main()
{
struct list_head lsitHead = { NULL, NULL };
// 创建两个学生对象
struct Student xiaoming {lsitHead, "xiaoming", 10 };
struct Student zhangsan {lsitHead, "zhangsan", 20 };
xiaoming.StuList.next = &zhangsan.StuList;
zhangsan.StuList.prev = &xiaoming.StuList;
std::cout << sizeof(struct Student) << std::endl;
std::cout << ((struct Student*)(xiaoming.StuList.next))->m_name << std::endl;
std::cout << ((struct Student*)xiaoming.StuList.next)->m_age << std::endl;
return 0;
}
#endif
#if 1
// 案例2: struct list_head StuLis放在 struct Student的尾部
struct Student
{
char* m_name;
int m_age;
struct list_head StuList;
};
int main()
{
struct list_head lsitHead = { NULL, NULL };
// 创建两个学生对象
struct Student xiaoming {"xiaoming", 10, lsitHead};
struct Student zhangsan {"zhangsan", 20, lsitHead};
xiaoming.StuList.next = &zhangsan.StuList;
zhangsan.StuList.prev = &xiaoming.StuList;
std::cout << sizeof(struct Student) << std::endl;
std::cout << ((struct Student*)(xiaoming.StuList.next - 1))->m_name << std::endl;
std::cout << ((struct Student*)(xiaoming.StuList.next - 1))->m_age << std::endl;
return 0;
}
#endif
linux内核侵入式链表
最新推荐文章于 2023-08-25 17:12:11 发布