[LinuxAPI]_01_LIST的使用

1. list_head

双向列表list_head结构体定义:

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

2. 创建链表

下列的接口都可以用于链表的初始化。

#define LIST_HEAD_INIT(name) { &(name), &(name) }

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

#define INIT_LIST_HEAD(ptr) do { \
	(ptr)->next = (ptr); (ptr)->prev = (ptr); \
} while (0)

2.1 例子

可以通过LIST_HEAD(student_list)初始化一个链表,student_listnextprev都是指向自己。
将这个student_list放到一个有意义的结构体重,作为这个结构体的宿主。

//初始化链表
LIST_HEAD(student_list);

//声明应用结构体
typedef struct student
{
	string name;
	int id;
	struct list_head student_list;
}Student;

3. 加入节点

3.1. __list_add

在已知的两个节点中创建插入一个新的节点

/*
* Insert a new entry between two known consecutive entries.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
static  void __list_add(struct list_head *newer, struct list_head *prev, struct list_head *next)
{
	next->prev = newer;
	newer->next = next;
	newer->prev = prev;
	prev->next = newer;
}

3.2. list_add

在头结点之后插入一个新节点

/**
* 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  void list_add(struct list_head *newer, struct list_head *head)
{
	__list_add(newer, head, head->next);
}

3.3. list_add_tail

在链表尾部插入新节点

/**
* 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  void list_add_tail(struct list_head *newer, struct list_head *head)
{
	__list_add(newer, head->prev, head);
}

3.4. 例子

//创建一个节点
Student stu1 = {"stu1",1,LIST_HEAD_INIT(stu1.student_list)};

//加到头结点后面
//student_list相当于一个头结点,在后面创建一个stu1,进行连接
__list_add(&stu1.student_list,&student_list,&student_list);

4. 删除节点

4.1. __list_del

/*
* 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  void __list_del(struct list_head *prev, struct list_head *next)
{
	next->prev = prev;
	prev->next = next;
}

4.2. list_del

删除链表中的任意节点,

/**
* 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.
*/
static  void list_del(struct list_head *entry)
{
	__list_del(entry->prev, entry->next);
	entry->next = (struct list_head *)0;
	entry->prev = (struct list_head *)0;
}

4.3. list_del_init

从链表中删除任一节点,并将其初始化。

/**
* list_del_init - deletes entry from list and reinitialize it.
* @entry: the element to delete from the list.
*/
static  void list_del_init(struct list_head *entry)
{
	__list_del(entry->prev, entry->next);
	INIT_LIST_HEAD(entry);
}

5. 遍历链表

5.1. list_for_each

以链表的头结点为标志位,从前往后循环遍历链表

static inline void prefetch(const void *x) { ; }

/**
* list_for_each	-	iterate over a list
* @pos:	the &struct list_head to use as a loop counter.
* @head:	the head for your list.
*/
#define list_for_each(pos, head) \
	for (pos = (head)->next, prefetch(pos->next); pos != (head); \
        	pos = pos->next, prefetch(pos->next))

5.2. list_for_each_prev

以链表的头结点为标志位,从后向前遍历链表

/**
* list_for_each_prev	-	iterate over a list backwards
* @pos:	    the &struct list_head to use as a loop counter.
* @head:	the head for your list.
*/
#define list_for_each_prev(pos, head) \
	for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
        	pos = pos->prev, prefetch(pos->prev))

5.3. list_for_each_safe

更安全的方式遍历链表,n是一个临时变量,用来存储pos的下一个变量,
作用是,在遍历过程中如果删除了pos所指的变量,这个函数可以继续遍历下去

/**
* list_for_each_safe	-	iterate over a list safe against removal of list entry
* @pos:	    the &struct list_head to use as a loop counter.
* @n:		another &struct list_head to use as temporary storage(临时变量)
* @head:	the head for your list.
*/
#define list_for_each_safe(pos, n, head) \
	for (pos = (head)->next, n = pos->next; pos != (head); \
		pos = n, n = pos->next)

5.4. list_for_each_entry

/**
* list_for_each_entry	-	iterate over list of given type
* @pos:	    the type * to use as a loop counter.
* @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),	\
		     prefetch(pos->member.next))

6. 找出宿主结构体

6.1. list_entry

如上,typedef struct student { string name; int id; struct head_list student_list }Student;结构体中,对我们有意义的是nameid字段,student_list只是一个用于遍历的手段,所以我们需要借助一个函数来获得宿主结构体的其他数据。

/**
* list_entry - get the struct for this entry
* @ptr:	    the &struct list_head pointer.(list_head结构,)
* @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) \
	((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))

6.2. list_entry举例

struct head_list *pos = NULL;
//取值pos指针所在的宿主结构体的name字段
string name = list_entry(pos,Student,name)

7. 宿主结构体的遍历

7.1. list_for_each_entry

/**
* list_for_each_entry	-	iterate over list of given type
* @pos:	    the type * to use as a loop counter.
* @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),	\
		     prefetch(pos->member.next))

7.2. list_for_each_entry_safe

/**
* 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 counter.
* @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))

8. 大作业

8.1 Windows上编译通过

Windows上无法识别typeof()所以没有使用宿主结构体遍历的函数。

#include "sdp_list.h"
#include <iostream>
#include <mutex>
#include <string>
using namespace std;
mutex sdp_lock;

//初始化链表
LIST_HEAD(student_list);

//声明应用结构体
typedef struct student
{
	string name;
	int id;
	struct list_head list;
}Student;

int main()
{
	Student stu1 = { "name1",1, LIST_HEAD_INIT(stu1.list) };
	Student stu2 = { "name2",2, LIST_HEAD_INIT(stu2.list) };
	Student stu3 = { "name3",3, LIST_HEAD_INIT(stu3.list) };

	sdp_lock.lock();
	__list_add(&stu1.list, &student_list, &student_list);
	list_add_tail(&stu2.list, &student_list);
	list_add_tail(&stu3.list, &student_list);

	struct list_head *pos = NULL, *tmp = NULL;
	Student *stu = NULL;
	//遍历
	list_for_each_safe(pos, tmp, &student_list)
	{
		stu = list_entry(pos, Student, list);
		{
			if (stu)
			{
				cout << stu->name << "   " << stu->id << endl;
			}
		}
	}
	//删除第二个元素
	list_del(&stu2.list);
	//遍历
	list_for_each_safe(pos, tmp, &student_list)
	{
		stu = list_entry(pos, Student, list);
		{
			if (stu)
			{
				cout << stu->name << "   " << stu->id << endl;
			}
		}
	}

	system("pause");
	return 0;
}

8.2 Linux上运行通过

使用了list_for_each_entry_safe函数

#include <linux/types.h>
#include "list.h"
#include <stdio.h>
//初始化链表
LIST_HEAD(student_list);

//声明应用结构体
typedef struct student
{
        char name[10];
        int id;
        struct list_head list;
}Student;

int main()
{
        Student stu1 = { "name1",1, LIST_HEAD_INIT(stu1.list) };
        Student stu2 = { "name2",2, LIST_HEAD_INIT(stu2.list) };
        Student stu3 = { "name3",3, LIST_HEAD_INIT(stu3.list) };

        __list_add(&stu1.list, &student_list, &student_list);
        list_add_tail(&stu2.list, &student_list);
        list_add_tail(&stu3.list, &student_list);

        struct list_head *pos = NULL, *tmp = NULL;
        Student *stu = NULL;
        //遍历
        list_for_each_safe(pos, tmp, &student_list)
        {
                stu = list_entry(pos, Student, list);
                {
                        if (stu)
                        {
                                printf("name = %s,id = %d\n",stu->name,stu->id);
                        }
                }
        }
        list_del(&stu2.list);

        Student *stu_tmp = NULL;
        list_for_each_entry_safe(stu, stu_tmp, &student_list, list)
        {

                if (stu)
                {
                        printf("name = %s,id = %d\n",stu->name,stu->id);
                }

        }
        return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fantongl

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值