linux核心的雙向鍊錶

2021-09-26 08:06:34 字數 1483 閱讀 9304

一、概述

linux核心中大量使用了鍊錶這個基本資料結構,因此有必要去窺探一下其「葫蘆裡賣的是什麼藥」。先來些基本知識點吧:

1.資料元素間是一對一關係;

2.鍊錶中的元素個數是有限的;

3.同一表中各資料元素的型別和長度相同。

二、實現

先上**,有個感性的認識,後面再解釋。

#include

#include

//煉表頭結構

struct list_head

;//#define list_head_init(name) 

//#define list_head(name)  struct list_head name = list_head_init(name)

//煉表頭初始化

void init_list_head(struct list_head *list)

//真正實現鍊錶插入操作

void _list_add(struct list_head *nnew,struct list_head *prev,struct list_head *next)

//向鍊錶插入乙個節點

void list_add(struct list_head *nnew,struct list_head *head)

#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_entry(ptr,type,member) \

((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))

//真正實現鍊錶刪除操作

void _list_del(struct list_head *prev,struct list_head *next)

//刪除鍊錶中的乙個節點

void list_del(struct list_head *entry)

//預設鍊錶長度

#define n  10

//定義節點的資料結構

struct numlist

;//定義煉表頭節點

struct numlist numhead;

int main()

printf("\n\n\n\n");

//遍歷鍊錶

list_for_each(pos,&numhead.list)

list_for_each_safe(pos,t,&numhead.list)

return 0;

}

linux核心雙向迴圈鍊錶例項

ifndef list h define list h 核心裡的雙向迴圈鍊錶 是乙個只有指標域而沒有資料域的結構 struct list define list head init name define list head name struct list name list head init ...

核心雙向迴圈鍊錶

include include include include include 煉表頭結構 struct list head 真正實現鍊錶插入操作 void list add struct list head nnew,struct list head prev,struct list head n...

linux核心原始碼「雙向鍊錶list head」

摘要 linux核心原始碼真是好東東,是眾多高手思維的結晶,在 linux 源 中有個頭檔案為 list.h 很多 linux 下的源 都會使用這個標頭檔案,它裡面定義了乙個結構 struct list head 如果您之前學過雙向鍊錶,那麼當你看到這個結構的時候,會覺得似曾相識。豈止似曾相識,如果...