[英]graphical explanation of a struct within another struct but a pointer to it,
我是 C 和結構的新手,我有一個我想理解的代碼,有人可以用圖形向我解釋這兩個結構如何相互關聯,就像結構圖中的結構節點
struct Graph {
// An array of pointers to Node to represent adjacency list
struct Node* head[N];
};
// A data structure to store adjacency list nodes of the graph
struct Node {
int dest;
struct Node* next;
};
Graph
有一個指向Node
指針數組(可能為空)。
每個Node
都有一個(可能為空)指向另一個Node
指針。
例如,一張圖可能如下所示:
+-----+-----+-...-+-----+-----+
Graph: | | | ... | | |
| | | 0 | ... | 0 | | |
+--|--+-----+-...-+-----+--|--+
| |
| V
| +---+ +---+ +---+
| Node: | ---->| ---->| 0 |
| +---+ +---+ +---+
|
V
+---+ +---+
Node: | ---->| 0 |
+---+ +---+
考慮下圖有 6 個節點,編號為 0 到 5:
0---1---2---5
| \ | /
| \ | /
| \|/
3-------4
這可以用 6 個鄰接表來表示:
[0] = 1, 3
[1] = 0, 2, 4
[2] = 1, 4, 5
[3] = 0, 4
[4] = 1, 2, 3, 5
[5] = 2, 4
其中[i] =
后面的列表列出了連接到節點[i]
的頂點。
將上述內容與您的數據結構相關聯:
N
為 6。struct Graph
中的head
是鄰接列表的集合(數組)。struct Graph
head[i]
指向頂點i
的鄰接列表的第一個元素(對應於上面的一個鄰接列表中的[i] =
)。 它可以為空列表保存一個空指針值( NULL
)。struct Node
代表一個鄰接表中的一個元素。struct Node
的dest
成員持有一個頂點號。struct Node
的next
成員指向鄰接表中的下一個元素(對應上面一個鄰接表中的逗號。對於鄰接表的最后一個元素, next
持有一個空指針值( NULL
)來標記結束的名單。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.