链表 - list

linux内核双向链表使用list klist linux内核中大量使用了链表数据结构来存储各种数据,比如device和driver使用klist存储,下面是list和klist使用示例提示:以下是本篇文章正文内容,下面案例可供参考上面介绍了内核中list和klist和测试代码,仅供参考。 阅读详情

链表是一种存放和操作可变数量元素(常称为节点)的数据结构。

链表与数据结构的区别:链表所包含的元素可以动态的创建并插入链表,各个元素无须占用连续内存。


1.链表分类

a. 单向链表: 这其实是一个递归。

struct list_element {

      void *data;

      struct list_element *next;

}

b. 双向链表:

struct list_element {

      void *data;

      struct list_element *next;

      struct list_element *prev;

}

c. 环形链表

单向环形链表: 单向链表的末尾元素指向头元素。

双向环形链表: 双向链表的末尾元素的 向后指针 指向头元素,头元素的 向前指针 指向末尾元素。


2. linux 内核中 链表的实现方式:

链表代码得头文件位于 include/linux/list.h 中。

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

list_head结构,经常被用来插入其他数组结构中使用,如下:

struct fox {

       unsigned long tail_length;   //狐狸尾巴长度

       unsigned long weight;     //狐狸体重

       bool          is_fantastic;  //这只狐狸优秀么?

       struct list_head list;   //所有fox结构体形成的链表

}
上面的 list.next 指向下一个元素, list.prev 指向前一个元素

3. list_entry(), 依靠这种方法,内核提供了一系列操作链表的的函数。


/**
 * list_entry - get the struct for this entry
 * @ptr:	the &struct list_head pointer.
 * @type:	the type of the struct this is embedded in.
 * @member:	the name of the list_struct within the struct.
 */
#define list_entry(ptr, type, member) \
	container_of(ptr, type, member)

list_entry(), 其实就是container_of().

container_of() 宏定义,用来从链表指针中找到父结构中包含的任何变量。

/**
 * container_of - cast a member of a structure out to the containing structure
 * @ptr:	the pointer to the member.
 * @type:	the type of the container struct this is embedded in.
 * @member:	the name of the member within the struct.
 *
 */
#define container_of(ptr, type, member) ({			\
	const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
	(type *)( (char *)__mptr - offsetof(type,member) );})

因为在C语言中,一个给定结构中的变量偏移 在编译时 地址就被ABI固定下来了。

所以container_of()能轻松的,可以根据一个结构体成员地址,得到该结构体的首地址,进而可以得到所有结构体成员的地址。

4. 初始化链表 --- INIT_LIST_HEAD((struct list_head *list))

static inline void INIT_LIST_HEAD(struct list_head *list)
{
	list->next = list;
	list->prev = list;
}
链表在使用之前需要初始化,常见的方式如下:

struct fox *red_fox;  //定义一个链表节点

red_fox = kmalloc(sizeof(*red_fox), GFP_KERNEL);

red_fox -> tail_length = 40;

red_fox -> weight = 6;

red_fox -> is_fantastic = false;  //节点数据初始化
 
INIT_LIST_HEAD(&red_fox -> list);  //单节点链表初始化,这个初始化后的链表只包含一个节点

如果一个结构静态创建,其中的链表需要直接引用,则可用下面的方式:

struct fox red_fox = {

    .tail_length = 40;

    .weight = 6;

    .list = LIST_HEAD_INIT(red_fox.list);

}
5. 链表头定义 --- LIST_HEAD() :
#define LIST_HEAD_INIT(name) { &(name), &(name) }

#define LIST_HEAD(name) \
	struct list_head name = LIST_HEAD_INIT(name)

例:

static LIST_HEAD(fox_list);  //该函数定义并初始化了一个名为fox_list的链表

5. 链表操作: 内核提供了一组函数来操作链表。

a. list_add(struct list_head *new, struct list_head *head): 向链表中添加一个节点

/**
 * list_add - add a new entry
 * @new: new entry to be added
 * @head: list head to add it after
 *
 * Insert a new entry after the specified head.
 * This is good for implementing stacks.
 */
static inline void list_add(struct list_head *new, struct list_head *head)
{
	__list_add(new, head, head->next);
}
/*
 * Insert a new entry between two known consecutive entries.
 *
 * This is only for internal list manipulation where we know
 * the prev/next entries already!
 */
#ifndef CONFIG_DEBUG_LIST
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;
}
#else
extern void __list_add(struct list_head *new,
			      struct list_head *prev,
			      struct list_head *next);
#endif
list_add_tail(struct list_head *new, struct list_head *head):项链表尾部添加一个节点。

/**
 * list_add_tail - add a new entry
 * @new: new entry to be added
 * @head: list head to add it before
 *
 * Insert a new entry before the specified head.
 * This is useful for implementing queues.
 */
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
	__list_add(new, head->prev, head);
}
b. list_del(struct list_head *entry) :从链表中删除一个节点

/**
 * list_del - deletes entry from list.
 * @entry: the element to delete from the list.
 * Note: list_empty() on entry does not return true after this, the entry is
 * in an undefined state.
 */
#ifndef CONFIG_DEBUG_LIST
static inline void list_del(struct list_head *entry)
{
	__list_del(entry->prev, entry->next);
	entry->next = LIST_POISON1;
	entry->prev = LIST_POISON2;
}
#else
extern void list_del(struct list_head *entry);
#endif
/*
 * Delete a list entry by making the prev/next entries
 * point to each other.
 *
 * This is only for internal list manipulation where we know
 * the prev/next entries already!
 */
static inline void __list_del(struct list_head * prev, struct list_head * next)
{
	next->prev = prev;
	prev->next = next;
}
list_del_init(struct list_head *entry) : 从链表中删除一个节点并对其重新初始化
/**
 * list_del_init - deletes entry from list and reinitialize it.
 * @entry: the element to delete from the list.
 */
static inline void list_del_init(struct list_head *entry)
{
	__list_del(entry->prev, entry->next);
	INIT_LIST_HEAD(entry);
}

c. list_move(struct list_head *list, struct list_head *head): 把节点list, 从一个链表中移除,并移到另一个链表的head节点后。

/**
 * list_move - delete from one list and add as another's head
 * @list: the entry to move
 * @head: the head that will precede our entry
 */
static inline void list_move(struct list_head *list, struct list_head *head)
{
	__list_del(list->prev, list->next);
	list_add(list, head);
}

list_move_tail(struct list_head *list, struct list_head *head) :把节点从一个链表移到另一个链表的末尾
/**
 * list_move_tail - delete from one list and add as another's tail
 * @list: the entry to move
 * @head: the head that will follow our entry
 */
static inline void list_move_tail(struct list_head *list,
				  struct list_head *head)
{
	__list_del(list->prev, list->next);
	list_add_tail(list, head);
}

d. list_empty(const struct list_head *head) : 判断链表是否为空

/**
 * list_empty - tests whether a list is empty
 * @head: the list to test.
 */
static inline int list_empty(const struct list_head *head)
{
	return head->next == head;
}

e. list_splice(const struct list_head *list, struct list_head *head):合并两个链表

/**
 * list_splice - join two lists, this is designed for stacks
 * @list: the new list to add.
 * @head: the place to add it in the first list.
 */
static inline void list_splice(const struct list_head *list,
				struct list_head *head)
{
	if (!list_empty(list))
		__list_splice(list, head, head->next);
}
static inline void __list_splice(const struct list_head *list,
				 struct list_head *prev,
				 struct list_head *next)
{
	struct list_head *first = list->next;
	struct list_head *last = list->prev;

	first->prev = prev;
	prev->next = first;

	last->next = next;
	next->prev = last;
}
list_splice_init(struct list_head *list,  struct list_head *head) : 合并两个链表并初始化
/**
 * list_splice_init - join two lists and reinitialise the emptied list.
 * @list: the new list to add.
 * @head: the place to add it in the first list.
 *
 * The list at @list is reinitialised
 */
static inline void list_splice_init(struct list_head *list,
				    struct list_head *head)
{
	if (!list_empty(list)) {
		__list_splice(list, head, head->next);
		INIT_LIST_HEAD(list);
	}
}

6. 遍历链表: 利用 链表 移动并访问 数据结构体。

a. 基本方法: list_for_each()宏;

/**
 * list_for_each	-	iterate over a list
 * @pos:	the &struct list_head to use as a loop cursor.
 * @head:	the head for your list.
 */
#define list_for_each(pos, head) \
	for (pos = (head)->next; prefetch(pos->next), pos != (head); \
        	pos = pos->next)
head --- 需要被遍历的 链表头节点。

每次遍历中,第一个参数不断移动指向下一个元素,知道链表中所有元素都被方位为止。

用法如下:

struct list_head *p;

list_for_each(p, list) {

     //p --- 指向链表中的元素

    //list --- 是被遍历链表的头节点
    //这其实是一个for循环
}

b. list_for_each_entry(); 遍历包含该链表的所有数据结构体

/**
 * list_for_each_entry	-	iterate over list of given type
 * @pos:	the type * to use as a loop cursor.
 * @head:	the head for your list.
 * @member:	the name of the list_struct within the struct.
 */
#define list_for_each_entry(pos, head, member)				\
	for (pos = list_entry((head)->next, typeof(*pos), member);	\
	     prefetch(pos->member.next), &pos->member != (head); 	\
	     pos = list_entry(pos->member.next, typeof(*pos), member))
靠list_entry()得到包含该链表的数据结构体的首地址。

使用方法:

struct fox *f;

list_for_each_entry(f, &fox_list, list) (

      //on each iteration, "f" points to the next fox structure.

)

例 inode_find_handle():

/*
 * inotify_find_handle - find the watch associated with the given inode and
 * handle
 *
 * Callers must hold inode->inotify_mutex.
 */
static struct inotify_watch *inode_find_handle(struct inode *inode,
					       struct inotify_handle *ih)
{
	struct inotify_watch *watch;  //定义变量,用来指向下一个inotify_watch数据结构

	list_for_each_entry(watch, &inode->inotify_watches, i_list) { //这其实是个for循环
		if (watch->ih == ih) //判断是不是在链表中,找到了ih数据结构,如果找到了则返回该结构
			return watch;
	}

	return NULL;
}
这里 inotify_watch 定义如下:

/*
 * struct inotify_watch - represents a watch request on a specific inode
 *
 * h_list is protected by ih->mutex of the associated inotify_handle.
 * i_list, mask are protected by inode->inotify_mutex of the associated inode.
 * ih, inode, and wd are never written to once the watch is created.
 *
 * Callers must use the established inotify interfaces to access inotify_watch
 * contents.  The content of this structure is private to the inotify
 * implementation.
 */
struct inotify_watch {
	struct list_head	h_list;	/* entry in inotify_handle's list */
	struct list_head	i_list;	/* entry in inode's list */
	atomic_t		count;	/* reference count */
	struct inotify_handle	*ih;	/* associated inotify handle */
	struct inode		*inode;	/* associated inode */
	__s32			wd;	/* watch descriptor */
	__u32			mask;	/* event mask for this watch */
};

list_for_each_entry_reverse(pos, head, member) : 反向遍历链表

/**
 * list_for_each_entry_reverse - iterate backwards over list of given type.
 * @pos:	the type * to use as a loop cursor.
 * @head:	the head for your list.
 * @member:	the name of the list_struct within the struct.
 */
#define list_for_each_entry_reverse(pos, head, member)			\
	for (pos = list_entry((head)->prev, typeof(*pos), member);	\
	     prefetch(pos->member.prev), &pos->member != (head); 	\
	     pos = list_entry(pos->member.prev, typeof(*pos), member))
原理与list_for_each_entry() 一样。

c. list_for_each_entry_safe(pos, n, head, member) : 遍历的同时删除节点。

/**
 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
 * @pos:	the type * to use as a loop cursor.
 * @n:		another type * to use as temporary storage
 * @head:	the head for your list.
 * @member:	the name of the list_struct within the struct.
 */
#define list_for_each_entry_safe(pos, n, head, member)			\
	for (pos = list_entry((head)->next, typeof(*pos), member),	\
		n = list_entry(pos->member.next, typeof(*pos), member);	\
	     &pos->member != (head); 					\
	     pos = n, n = list_entry(n->member.next, typeof(*n), member))

n --- next , 与pos类型一样,用来将下一节点存进表中,以使能安全删除当前项。

例:inotify_inode_is_dead(struct inode *inode):

/**
 * inotify_inode_is_dead - an inode has been deleted, cleanup any watches
 * @inode: inode that is about to be removed
 */
void inotify_inode_is_dead(struct inode *inode)
{
	struct inotify_watch *watch, *next; //定义2个变量,用来遍历

	mutex_lock(&inode->inotify_mutex); //上锁
	list_for_each_entry_safe(watch, next, &inode->inotify_watches, i_list) { //开始遍历
		struct inotify_handle *ih = watch->ih; //保存链表节点
		mutex_lock(&ih->mutex);
		inotify_remove_watch_locked(ih, watch); //删除 watch 节点
		mutex_unlock(&ih->mutex);
	}
	mutex_unlock(&inode->inotify_mutex);
}
EXPORT_SYMBOL_GPL(inotify_inode_is_dead);

list_for_each_entry_safe_reverse(pos, n, head, member):反向遍历删除节点用这个

/**
 * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
 * @pos:	the type * to use as a loop cursor.
 * @n:		another type * to use as temporary storage
 * @head:	the head for your list.
 * @member:	the name of the list_struct within the struct.
 *
 * Iterate backwards over list of given type, safe against removal
 * of list entry.
 */
#define list_for_each_entry_safe_reverse(pos, n, head, member)		\
	for (pos = list_entry((head)->prev, typeof(*pos), member),	\
		n = list_entry(pos->member.prev, typeof(*pos), member);	\
	     &pos->member != (head); 					\
	     pos = n, n = list_entry(n->member.prev, typeof(*n), member))

还有其他的链表操作函数 尽在/include/linux/list.h

Python全栈入门到实战【进阶篇 1】类与对象:面向对象编程的基石 Python中定义类使用**class关键字**,创建对象使用**类名()**(类似调用函数),语法简洁,我们从最基础的空类开始,逐步拆解。class# 定义空的学生类,pass表示占位,无实际内容 class Student : pass编码规范:类名遵循大驼峰命名法(每个单词首字母大写,无下划线),如StudentCarPhone,这是Python官方推荐的PEP8规范,便于区分类和其他标识符。实例方法必须定义在类内部;实例方法的第一个参数必须是self(名称可自定义,如this。 阅读详情

相关推荐

Xilinx FPGA 100G RDMA开发实战:基于AXI Lite配置的ROCE V2高速通信系统搭建

本文详细介绍了在Xilinx FPGA上搭建100G ROCE V2高速通信系统的实战过程。通过分析RDMA技术优势与FPGA实现的必要性,重点阐述了基于AXI Lite接口配置ERNIC IP核的关键步骤,包括硬件系统构建、驱动开发及与商用网卡的互通测试,旨在实现兼具高性能与灵活性的网络加速方案。

js777的博客 576

FreeRTOS学习——链表list

FreeRTOS学习——链表(列表)list,仅用于记录自己阅读与学习源码*list_t只能存储指向list_item_t的指针。每个list_item_t都包含一个数值(xItemValue)。大多数时候,列表是按项目值升序排列的。*创建的列表已包含一个列表项。此项的值是可以存储的最大值,因此它始终位于列表的末尾,并充当标记。列表成员pxHead始终指向此标记,即使它位于列表的末尾。这是因为尾部包含一个返回指针,指向列表的真正头部。

rerrick_rose的博客 1518

【立创开源】太阳能板充电方案单节3.7v锂电池充电

一直对太阳能板感兴趣,老是感觉充电好麻烦,要是放在那里就可以自己充电多好,所以尝试做了一个太阳能充电板。因为这几天都是阴天,所以测试效果不太好,但是下雨天也是能充电的,但是基本只能维持电量不掉,电压差不多4.2v以上,电流只有2mA。中午出太阳的时候,隔着玻璃放在太阳下测试,电流有12mA以上。能充起一点点电。等大晴天看一看能不能达到卖家说50mA。┗( T﹏T )┛感觉达不到。这个东西只能当一个玩具(;′⌒`)。不过如果汽车上一些行车记录仪加一个这个,汽车经常在太阳下停车,可能再也不用充电了。

贾saisai的博客 1万+

链表之LIST

文章目录LIST 示意图接口和实现举例代码分析LIST_ENTRY 和 LIST_HEADLIST_INIT 和 LIST_INSERT_HEADLIST_INSERT_AFTERLIST_INSERT_BEFORELIST_FOREACHLIST_REMOVELIST_FIRST 和 LIST_NEXT参考资料 上一篇博文 链表之SLIST_车子(chezi)-优快云博客 说过,queue.h 里面有 5 种链表,分别是: SLIST LIST STAILQ TAILQ CIRCLEQ 这篇文章说

车子(chezi) 1584

TwinCAT3实战:从零搭建倍福PLC的Modbus-TCP Server

本文详细介绍了如何使用TwinCAT3从零搭建倍福PLC的Modbus-TCP Server,包括环境准备、工程配置、参数设置、客户端连接测试及常见问题排查。通过实战步骤和实用技巧,帮助工程师快速掌握倍福PLC与Modbus-TCP的通信实现,提升工业自动化项目的开发效率。

weixin_30797199的博客 173

iTunes备份文件路径修改方法

因为以前的很多帖子iTunes都不是从微软下载的,而现在的iTunes一般都是微软商店里面的,它的路径为:“C:\Users\ 你的用户名 \Apple\MobileSync” (注:用户名,根据个人电脑名称更改)打开iTunes备份路径:“C:\Users\txh\AppData\Roaming\Apple Computer\MobileSync”,其中文件夹MobileSync就是用来存放备份文件的。创建你想备份纯在的目录创建备份文件目录(我的是G:/APPLE/Backup)2.粘贴的剪切文件目录。

尘玥的故事 1万+

C++——list链表

list链表介绍、以及各种库函数的使用。重点了解list与前面vector、string的区别 了解什么场景下适合选择哪种容器。

2403_87935067的博客 1138

Android oom_adj 更新原理(一)

通过之前的两篇博文《lmkd 机制详解》和《lmkd中adj score的算法剖析》中,根据计算出来的 min_score_adj 选择大于该值的 oom_adj 的进程进行 kill 处理。oom_adj 随着应用状态的不同其值也是伴随着变化的。本文将通过源码剖析 oom_adj 更新的原理。

私房菜 2653

Go语言双向链表list.List详解

Go语言中的list.List是包提供的双向链表实现,具有高效的插入和删除操作能力。

liushen0528的博客 809

AUTOSAR OS Counter讲解

Counter(计数器)以tick为单位记录操作系统中发生了多少“事情”。tick(滴答)是一个抽象的单位。时间,例如毫秒,微妙,分钟等,然后计数器计算过去了多少时间旋转,例如以度或分钟为单位,计数器计算物体旋转了多少按钮按下,计数器计算按钮按下多少次错误,计数器计算错误发生的频次中断(有时是任务)用于驱动一个计数器。该驱动程序负责进行正确的OS API调用来滴答计数器,或者告诉OS计数器已经滴答到一个所需的值。

酒无忧的博客 1070

Arduino ESP32S3 基础入门教学大纲

ESP32环境、Arduino

追梦 3545

c++容器链表list的使用

链表和list 咳咳,不是每一朵鲜花都能代表爱情,但是玫瑰做到了;不是每一颗树都能耐的住干渴,但是白杨做到了;也不是每一个人都会追求上进,挑战自我,但是我相信我们也能做到。今天给大家带来的是c++容器,list链表。 list作为c++里面的容器哈,它是一个双向链表,空间可以不连续,通过指针进行数据的访问哈,他可以高效率的完成在任意地方的删插改。 但是list链表不支持随机访问哦! 然后我们来看看他的用法: 写法 说明 list < int > mlist; 定义一个链表

qq_43514659的博客 2333

解锁 Bootloader 不再难!HyperOS澎湃系统小米与红米手机通用

必要时,可以在工具右上角安装驱动模块,手动安装驱动。解决方法:确保手机升级到最新稳定版,登录同一个小米账号,并在。新机型解锁时需要等待一段时间,通常为 7 天。如果账号安全评分较低,解锁等待时间会更长。如果解锁失败提示权限不足,说明账号存在安全问题。出现这个错误时,通常是因为网络不稳定。如果解锁次数已满,需要等到下个月。解锁后,手机会重启,首次开机会稍微慢一些,等耐心等待即可。按钮,开始解锁过程,注意:解锁会清除手机内所有数据。7次,开启开发者选项。,右键解压到指定文件夹。,使用小米账号登录。

代码简单说 Vue、JAVA、PHP、Node.js 熟练运用,接口、架构、性能全搞定。 7221

【算法竞赛】链表和 list

链表是算法竞赛与数据结构中不可或缺的基础组件,其灵活的内存管理和高效的插入删除操作使其在特定场景下比数组更具优势。本文系统梳理了链表的核心概念、分类及实现方式,涵盖单链表、双向链表和循环链表的模拟实现细节,包括节点定义、初始化、插入删除操作及遍历查找等关键逻辑。同时对比动态链表(如 C++ STL中的 list 容器),分析其接口特性与实际应用场景。通过代码示例与问题思考,帮助读者深入理解链表的底层机制与性能权衡,为算法设计与优化奠定基础。两个足够大的数组,一个用来存数据,一个用来存下一个结点的位置。

遇见,获得,失去,成长,释怀,完结。我与旧事归于尽,来年依旧迎花开。 896

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

动态数组,链表以及List类

二 链表一 动态数组。

2401_87353928的博客 1467

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

链表(双向链表)— list

​list是 C++ 标准模板库(STL)中的一个序列容器,它允许在容器的任意位置快速插入和删除元素。与数组或向量(vector)不同,list不需要在创建时指定大小,并且可以在任何位置添加或删除元素,而不需要重新分配内存。

2401_86469695的博客 1068

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

C++list链表

在插入和删除操作(尤其是在链表的中间)时提供了更好的性能,因为它不需要重新分配内存或移动现有元素。不支持随机访问,所以不能使用下标运算符(是一种序列容器,它实现了双向链表。您需要使用迭代器或范围。

Qhumaing的博客 474

链表与list

线性表的链式存储就是链表。它是将元素存储在物理上任意的存储单元中,由于⽆法像顺序表⼀样通过下标保证数据元素之间的逻辑关系,链式存储除了要保存数据元素外,还需额外维护数据元素之间的逻辑关系,这两部分信息合称结点(node)。即结点有两个域:保存数据元素的数据域和存储逻辑关系的指针域。

w1114waini的博客 395

链表和 list

在单链表的模拟实现中,我们可以发现虽然一个节点是由其数据域和指针域组成的,但真正代表该节点信息的是数据域和下标,指针域只是为了连接下一个节点的工具(其实就是下一个节点的下标,本质上是属于下一个节点的)。STL 里面的 list 的底层就是动态实现的双向循环链表,增删会涉及 new 和 delete,因为new 和 delete 是非常耗时的操作,效率不高,在竞赛中一般不会使用。当然第二种方式是有局限性的,一是存的数据过大会导致崩溃,二是不能存在相同的数据,否则这个数组中的部分数据会进行刷新。

shdbdndj的博客 1499

NS2自身的LIST链表

 NS2 LIST 链表本人在看NS2源代码的时候发现有LIST_HEAD,LIST_ENTRY,LIST_INIT,LIST_REMOVE等等,经过查看源代码和上网翻阅资料,发现NS2有其自身定义的链表结构,定义在bsd-list.h中,于是写了个简短的说明,方便他人使用.由于是自己看源代码后自己理解的,如有错误,请告知 本文档分三部分 1.源码解释2.源码3.应

lyplyz的专栏 4184

C++链表双杰:list与forward_list

本文深入对比了C++中的链表容器std::list和std::forward_list。std::list是双向链表,支持高效中间插入/删除操作(O(1))和双向遍历,适合需要频繁修改序列中间位置的场景;std::forward_list是单向链表,内存开销更小但功能受限,适合内存敏感且只需单向遍历的场合。与vector对比,链表在中间操作和迭代器稳定性方面占优,但在随机访问和缓存友好性上表现较差。选择时应根据具体需求:频繁随机访问选vector,频繁中间修改选list,内存受限选forward_list。

2301_78884150的博客 979

内核链表 list

本文主要是参考公众号一口linux, 参考大佬写的文章,中间对其增加了一些自己的笔记,做一下记录 在Linux内核中,提供了一个用来创建双向循环链表的结构 list_head。虽然linux内核是用C语言写的,但是list_head的引入,使得内核数据结构也可以拥有面向对象的特性,通过使用操作list_head 的通用接口很容易实现代码的重用,有点类似于C++的继承机制。 首先找到list_head结构体定义,kernel/inclue/linux/types.h 如下: struct list_head

qq_39575672的博客 362

list链表容器

插入与删除指定元素 排序与去重

qq_62932762的博客 487

list链表

STL中vector和list的用法大差不差,不用刻意去记,熟练的使用就可以了。

2301_79853066的博客 447

【C++】list 链表的使用+模拟实现

本文主要介绍C++【STL】容器中的 list,包括接口说明和模拟实现。其中讲解了迭代器功能上的分类,让你了解为何 list 要实现这个接口。

x_p96484685433的博客 1793

C++ STL 之双向链表:list

slist是 C++ STL 中提供的双向链表(Doubly Linked List)容器。与vector的底层实现不同,list并不是一块连续的内存,而是由一个个节点(Node)通过指针连接而成,每个节点存储数据以及前驱、后继指针。双向链表结构:每个元素(节点)包含指向前后节点的指针非连续内存:元素分散存储在内存中插入/删除高效任意位置插入/删除时间复杂度为O(1)无需移动其他元素不支持随机访问不能像数组一样直接用list[i]访问访问第n个元素需要O(n)时间额外内存开销。

m0_74405593的博客 768
上一篇: 共享内存
下一篇: gcc 及其参数
lamdoc
博客等级 码龄18年 155粉丝 655原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值