C风格的邻接表模板,链式前向星

今天和某大神交流了下图的存储,咱给出了lk大神教给咱的链式前向星代码,这里贴一下好了。
const int maxn = 100; //最大点数
const int maxm = 500; //最大边数

//边定义
struct Edge
{
	int v, next; //v记录指向的节点,next记录下一条记录的数组下标
	int cost; //记录距离,也可以是其他东西
} e[Maxm];

//需要的其他参数
int h[maxn] = {0}, tot = 0; //h[x] 表示 x 节点的第一条边的数组下标(为0时表示不存在), tot表示总边数

//加单向边
void add(int u, v, c)
{
	e[++tot].v = v;
	//插入到h数据链表的第一位
	e[tot].next = h[a];
	h[u] = tot;

	e[tot].cost = c;
}

//x节点上边的遍历
for (int i = h[x]; i; i = e[i].next )
{
	int v = e[i].v;//v为目标节点
}

C风格的邻接表模板,链式前向星》上有2条评论

发表评论

邮箱地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>