C语言链表的操作

初学

初学C语言时,对于链表节点的定义一般是这样的:

typedef struct node {
    int data;
    struct node *next;
} Node;

向链表中添加节点:

void addNode(Node **head, int data) {
    Node *newNode = (Node*)malloc(sizeof(Node));
    newNode->data = data;
    newNode->next = *head;
    *head = newNode;
}

 删除节点和遍历节点,就不列举了

进阶

但是到了工作中,对链表的操作一般不这么玩

节点定义

工作中链表节点的定义:

struct dlist {
    struct dlist *next;
    struct dlist *prev;
};

也就是说是一个双向链表

  • next 指向下一个节点
  • prev 指向上一个节点
  • 由于是 双向循环链表,最后一个节点的next指向头节点,头节点的prev指向最后一个节点

然后我们在实际使用时,一般定义一个包含struct dlist结构的自定义结构,例如

struct my_struct {
    int data;
    struct dlist list;
};

初始化

在使用的时候,一般先初始化

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

//调用这个接口,传入节点的地址即可
struct my_struct test;
dlist_init(&test.list);

添加节点

添加节点一般有两个接口:dlist_add和dlist_add_tail

static inline void __dlist_add(struct dlist *entry, struct dlist *prev, struct dlist *next)
{
    entry->next = next;
    entry->prev = prev;
    next->prev = entry;
    prev->next = entry;
}

/* Add a new entry to the head of list */
static inline void dlist_add(struct dlist *list, struct dlist *entry)
{
    __dlist_add(entry, list, list->next);
}

/* Add a new entry to the tail of list */
static inline void dlist_add_tail(struct dlist *list, struct dlist *entry)

删除节点

删除节点调用dlist_del接口

static inline void __dlist_del(struct dlist *entry)
{
    entry->next->prev = entry->prev;
    entry->prev->next = entry->next;
}

/* Delete an entry from list */
static inline void dlist_del(struct dlist *entry)
{
    __dlist_del(entry);
    entry->next = NULL;
    entry->prev = NULL;
}

遍历节点

遍历节点有dlist_for_each和dlist_for_each_entry,两者的区别参考:

linux之list_for_each和list_for_each_entry函数 - 裸睡的猪 - 博客园

dlist_for_each
dlist_for_each_entry

还有一个是安全遍历,在删除节点的时候使用不使用这个接口的话,不能直接对链表进行操作,只能读取

dlist_for_each_entry_safe

通常我们要自定义一个结构体,然后结构体中包含struct list_head。list_head 本质上就是链表中的一个节点,而且是一个 通用节点结构,用来把自己的结构体组织成链表。可以把struct list_head理解为“钩子”,用来将节点一个个挂到链表上,这就是我们在遍历的时候要用list_for_each_entry的原因

参考:

Linux内核链表_linux的链表-CSDN博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

映秀小子

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

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

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

打赏作者

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

抵扣说明:

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

余额充值