繁体   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