Back to list

Linux Kernel - Linked List

Lv.5782@mukitaro13 playsDec 31, 2025

Linux kernel doubly-linked list implementation. A fundamental data structure pattern used throughout the kernel.

preview.c
C
1struct list_head {
2 struct list_head *next, *prev;
3};
4
5#define LIST_HEAD_INIT(name) { &(name), &(name) }
6#define LIST_HEAD(name) struct list_head name = LIST_HEAD_INIT(name)
7
8static inline void INIT_LIST_HEAD(struct list_head *list)
9{
10 list->next = list;
11 list->prev = list;
12}
13
14static inline void __list_add(struct list_head *new,
15 struct list_head *prev,
16 struct list_head *next)
17{
18 next->prev = new;
19 new->next = next;
20 new->prev = prev;
21 prev->next = new;
22}
23
24static inline void list_add(struct list_head *new, struct list_head *head)
25{
26 __list_add(new, head, head->next);
27}
28
29static inline void list_add_tail(struct list_head *new, struct list_head *head)
30{
31 __list_add(new, head->prev, head);
32}

Custom problems are not included in rankings