C语言中图的定义

C语言中图的定义,c,C,我将邻接列表节点的定义设置为: typedef struct node_type{ int data; struct node_type *link; }node; 邻接列表的定义: typedef node *list; 图的定义:(图是邻接列表的数组) 这是一个正确的定义吗?您可以存储一个邻接列表和一个节点列表 typedef struct node_type{ int data; } node; typedef struct adj_type { node * n

我将邻接列表节点的定义设置为:

typedef struct node_type{  
int data;
struct node_type *link;
}node;
邻接列表的定义:

typedef node *list;
图的定义:(图是邻接列表的数组)


这是一个正确的定义吗?

您可以存储一个邻接列表和一个节点列表

typedef struct node_type{  
   int data;
} node;

typedef struct adj_type {
   node * n1;
   node * n2;
} adj;

typedef struct graph_type{
   int n_nodes;
   int n_adjs;
   node ** node_list;
   adj ** adj_list;
}graph;
或存储节点及其邻接:

typedef struct node_type{  
   int data;
   int n_links;
   sturct node_type ** links;
} node;

typedef struct node_type{
   int n_nodes;
   node ** node_list;
}

如果使用链表容器来存储节点/邻接,而不是指针数组,则会将图形逻辑与内存分配逻辑分开。

您的问题不清楚,但我假设您询问的是无向图形的邻接列表表示

尽量接近你最初的想法-

首先定义一个结构来表示邻接列表节点-

struct AdjListNode
{
    int dest;
    struct AdjListNode* next;
};
然后是表示邻接列表的结构-

struct AdjList
{
    struct AdjListNode *head;  // pointer to head node of list
};
然后是表示图形的结构。这里的图是邻接列表的数组。数组的大小为V(图中的顶点数)

为了更好地理解不同的图形表示,您可以在谷歌上搜索大量有用的资源。这里有几个-


不要
typedef
指针!这最终会导致混乱。这看起来像一个单链表。我猜这算是一个图,但它有点退化。不清楚你在问什么。在C语言中定义图形的方法不止一种(与任何语言一样)。如果不知道你的意图是什么,就不可能说你是否成功了。你假设得对。。谢谢你的链接
struct AdjList
{
    struct AdjListNode *head;  // pointer to head node of list
};
struct Graph
{
    int V;
    struct AdjList* array;
};