windows内核驱动中的链表结构与数据结构中的链表结构在构造上有很大不同,以循环双链表为例
数据结构中的链表结构: 数据就像集装箱,可以直接放置在火车上,而节点就像每节之间的挂接装置.
内核驱动中的链表结构: 数据就像车厢,自带挂接装置(节点)
1.链表结构体不同
数据结构中的链表结构,包含有节点和数据,
struct DataList{
DataType data;
struct DataList* next;
struct DataList* prev;
};
驱动中的链表结构,仅包含有节点,没有数据。
struct DriverList{
struct DriverList* next;
struct DriverList* prev;
};
2.数据结构中的数据,可以直接封装在链表结构体中; 而在驱动链表结构中,要将链表结构封装在数据中,
struct DataEntry{
struct DriverList node;
DataType data;
};
以下是示例代码
1 #pragma once 2 3 typedef struct List { 4 struct List * next; 5 struct List * prev; 6 }List, *Plist; 7 8 void initList(Plist head); 9 bool isEmpty(Plist pHead); 10 void addFirst(Plist pHead, Plist pNewNode); 11 void addLast(Plist pHead, Plist pNewNode); 12 void traverse(Plist pHead, void (*pfun)(void *)); 13 14 void initList(Plist head) 15 { 16 head->next = head; 17 head->prev = head; 18 } 19 20 21 bool isEmpty(Plist pHead) 22 { 23 if (pHead->next == pHead || pHead->prev == pHead) 24 return true; 25 return false; 26 } 27 28 void addFirst(Plist pHead, Plist pNewNode) 29 { 30 // list is not empty 31 if (pHead->next != pHead) 32 { 33 pHead->next->prev = pNewNode; 34 pNewNode->next = pHead->next; 35 } 36 else 37 { 38 pHead->prev = pNewNode; 39 pNewNode->next = pHead; 40 } 41 pHead->next = pNewNode; 42 pNewNode->prev = pHead; 43 } 44 45 void addLast(Plist pHead, Plist pNewNode) 46 { 47 // list is not empty 48 if (pHead->next != pHead) 49 { 50 pNewNode->prev = pHead->prev; 51 pHead->prev->next = pNewNode; 52 } 53 else 54 { 55 pHead->next = pNewNode; 56 pNewNode->prev = pHead; 57 } 58 pHead->prev = pNewNode; 59 pNewNode->next = pHead; 60 }