標籤:node src || 結構體 cout windows plist driver bool
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 empty31 if (pHead->next != pHead)32 {33 pHead->next->prev = pNewNode;34 pNewNode->next = pHead->next;35 }36 else37 {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 empty48 if (pHead->next != pHead)49 {50 pNewNode->prev = pHead->prev;51 pHead->prev->next = pNewNode;52 }53 else54 {55 pHead->next = pNewNode;56 pNewNode->prev = pHead;57 }58 pHead->prev = pNewNode;59 pNewNode->next = pHead;60 }
List.h
1 #include "stdafx.h" 2 #include <cstdlib> 3 #include <iostream> 4 #include <cstring> 5 #include "List.h" 6 7 #define SIZE 20 8 #define ARLEN 10 9 10 typedef struct Student {11 List listEntry;12 int age;13 char name[SIZE];14 double weight;15 }Student, *Pstudent;16 17 using namespace std;18 19 void printStudent(Plist pHead);20 21 int main()22 {23 List head;24 initList(&head);25 Pstudent pStu[ARLEN];26 for (int i = 0; i < ARLEN; i++)27 {28 pStu[i] = (Pstudent)malloc(sizeof(Student));29 pStu[i]->age = i;30 pStu[i]->weight = i*1.2;31 sprintf_s(pStu[i]->name, "%s%d","student",i);32 addLast(&head, &pStu[i]->listEntry);33 }34 printStudent(&head);35 }36 37 void printStudent(Plist pHead)38 {39 Pstudent pStu;40 Plist current = pHead->next;41 while (current != pHead)42 {43 pStu = (Pstudent)(current);44 cout << "stu->name = " << pStu->name << ", stu->age = " 45 << pStu->age << ", stu->weight = " << pStu->weight << endl;46 current = current->next;47 }48 }
main.c
output:
stu->name = student0, stu->age = 0, stu->weight = 0
stu->name = student1, stu->age = 1, stu->weight = 1.2
stu->name = student2, stu->age = 2, stu->weight = 2.4
stu->name = student3, stu->age = 3, stu->weight = 3.6
stu->name = student4, stu->age = 4, stu->weight = 4.8
stu->name = student5, stu->age = 5, stu->weight = 6
stu->name = student6, stu->age = 6, stu->weight = 7.2
stu->name = student7, stu->age = 7, stu->weight = 8.4
stu->name = student8, stu->age = 8, stu->weight = 9.6
stu->name = student9, stu->age = 9, stu->weight = 10.8
請按任意鍵繼續. . .
windows核心驅動中的鏈表結構