linux内核链表数据结构的分析与实现

本文深入探讨了C++中链表数据结构的实现与应用,通过具体代码示例展示了链表的基本操作,如初始化、插入和删除节点,以及如何遍历链表。特别关注了链表在内存管理中的作用,提供了理解链表内部机制的关键洞察。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

源码+调试+注释 = 学会! 

#include<Windows.h>
#include<stdio.h>
#include<stdlib.h>
#include<iostream>

/* stl */
#include<vector>
#include<algorithm>
#include<queue>
#include<deque>
#include<stack>
#include<list>
#include<map>


using namespace std;

struct list_head {
	struct list_head* next;
	struct list_head* prev;
};

struct fox {

	unsigned long tail_length;	// 尾巴长度,以厘米为单位
	unsigned long weight;		// 重量,以千克为单位
	bool is_fantastic;		// 这只狐狸奇妙吗?
	struct list_head list;
};

#define LIST_HEAD_INIT(name) { &(name), &(name) }
#define LIST_HEAD(name) struct list_head name = LIST_HEAD_INIT(name)

static inline void INIT_LIST_HEAD(struct list_head* list)
{
	list->next = list;
	list->prev = list;
}

/**
* Insert a new entry between two known consecutive entries.
*/
static inline 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;
}

/**
* Insert a new entry after the specified head.
*/
static inline void list_add(struct list_head* _new, struct list_head* head)
{
	__list_add(_new, head, head->next);
}

static inline void __list_del(struct list_head* prev, struct list_head* next)
{
	next->prev = prev;
	prev->next = next;
}

#define POISON_POINTER_DELTA 0
#define LIST_POISON1  ((void *) 0x00100100 + POISON_POINTER_DELTA)
#define LIST_POISON2  ((void *) 0x00200200 + POISON_POINTER_DELTA)

static inline void __list_del_entry(struct list_head* entry)
{
	__list_del(entry->prev, entry->next);
}

static inline void list_del(struct list_head* entry)
{
	__list_del(entry->prev, entry->next);
	//entry->next = LIST_POISON1;
	//entry->prev = LIST_POISON2;
}

// 结构体成员的偏移地址(偏移量)
#define _offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)


/**
* 分析:
* @ptr为成员的地址
* @type为容器结构体类型
* @member为容器结构体的成员
* typeof( ((type *)0)->member )得到结构体type的member成员的类型
* const typeof( ((type *)0)->member ) *__mptr = (ptr) 指向容器结构体list成员的指针(地址)
* (type *)( (char *)__mptr - offsetof(type, member) 计算指向容器结构体实例的指针
*/
//#define container_of(ptr, type, member) (\
//	{			\
//		const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
//		(type *)( (char *)__mptr - _offsetof(type, member) );\
//	}\
//	)

#ifndef container_of
#define container_of(ptr, type, member) \
    (type *)((char *)(ptr) - (char *) &((type *)0)->member)
#endif

/**
* list_entry - get the struct for this entry
*/
#define list_entry(ptr, type, member) \
	container_of(ptr, type, member)

#define list_first_entry(ptr, type, member) \
    list_entry((ptr)->next, type, member)

#define list_last_entry(ptr, type, member) \
    list_entry((ptr)->prev, type, member)

#define list_for_each(pos, head) \
	for (pos = (head)->next; pos != (head); pos = pos->next)


#define list_for_each_safe(pos, n, head) \
	for (pos = (head)->next, n = pos->next; pos != (head); \
		pos = n, n = pos->next)


/* 深刻掌握内核链表数据结构的设计思想 */

int main(int argc, char* argv[])
{
	fox* red_fox = new fox;
	red_fox->tail_length = 40;
	red_fox->weight = 6;
	red_fox->is_fantastic = false;

	INIT_LIST_HEAD(&red_fox->list);
	list_head* head = &red_fox->list;

	fox* red_fox_1 = new fox;
	red_fox_1->tail_length = 41;
	red_fox_1->weight = 7;
	red_fox_1->is_fantastic = true;

	__list_add(&red_fox_1->list, head, head->next);

	fox* tmp = list_entry(head, fox, list);

	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值