1.1. 什么是链表

    a. 链表是一种常见的基础数据结构

    b. 链表是由节点(结构体)组成的,节点中包含:有效数据和指针。

    c. 链表的内存要求比较灵活,一般不能用栈,也不能用data数据段。只能用堆内存。

  1.2. 链表与数组差别  

    1.2.1. 链表就是用来解决数组的大小不能动态扩展的问题,所以链表其实就是当数组用的

    1.2.2. 数组定义好长度后不能重新改变数组长度,而链表可以不受限制扩展

二. 内核中链表分析

  2.1. 链表头文件分析

    a. 此文件是参考linux内核中相关链表文件

    b. 链表结构体中只有前链表指针和后链表指针,如此实现链表指针和数据分离

#ifndef __MYLIST__H
#define __MYLIST__H

#define offsetof(type,member) ((int)&(((type *)0)->member))


//通过结构体成员指针获取结构体指针位置
#define container_of(ptr, type, member) ({            \
    const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
    (type *)( (char *)__mptr - offsetof(type,member) );})
 
 
//链表结构体
struct list_head {
    struct list_head *next, *prev;
};
 
//链表初始化
static inline void INIT_LIST_HEAD(struct list_head *list)
{
    list->next = list;
    list->prev = list;
}
 
#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
 
 
//添加至链表首部
static inline void list_add(struct list_head *new, struct list_head *head)
{
    __list_add(new, head, head->next);
}
 
//添加到链表尾部
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
    __list_add(new, head->prev, head);
}
 
//判断链表是否为空
/**
 * list_empty - tests whether a list is empty
 * @head: the list to test.
 */
 /* if empty return 1,else 0 */
static inline int list_empty(const struct list_head *head)
{
    return head->next == head;
}
 
/*
 * 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;
}
 
 
//删除操作
static inline void list_del(struct list_head *entry)
{
    __list_del(entry->prev, entry->next);
    entry->next = NULL;
    entry->prev = NULL;
}
 
//获取链表的数据指针
#define list_entry(ptr, type, member) \
    container_of(ptr, 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)
            
 
//遍历链表元素
#define list_for_each_entry(pos, head, member)                \
    for (pos = list_entry((head)->next, typeof(*pos), member);    \
         &pos->member != (head);     \
         pos = list_entry(pos->member.next, typeof(*pos), member))
         
#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))
         
//取第一个元素
#define list_first_entry(ptr, type, member) \
        list_entry((ptr)->next, type, member)
 
#endif
View Code

相关文章:

  • 2022-12-23
  • 2021-11-06
  • 2022-03-06
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-17
  • 2021-09-12
猜你喜欢
  • 2022-12-23
  • 2021-06-26
  • 2022-12-23
  • 2021-11-16
  • 2021-09-30
  • 2022-12-23
相关资源
相似解决方案