簡體   English   中英

另一個結構中的結構的圖形解釋,但指向它的指針,

[英]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 Nodedest成員持有一個頂點號。
  • struct Nodenext成員指向鄰接表中的下一個元素(對應上面一個鄰接表中的逗號。對於鄰接表的最后一個元素, next持有一個空指針值NULL )來標記結束的名單。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM