首页 > 其他 > 详细

链式前向星

时间:2019-12-14 00:40:59      阅读:94      评论:0      收藏:0      [点我收藏+]

链式前向星,类似于头插法的邻接链表。

①int tot:用于计算边的编号

②head[MAX]:用来存储以i为起点的最新加入的边在edge[]中的存储位置

③struct Node{ int to; int w; int next}; 

to表示终点;w是权值;next表示下一条边在edge[]数组中的存储位置

Node edge[MAX]:用来存储边的信息

int head[maxn];    //存以i为起点的最近加入的一条边的存储位置 
int tot;        //边的编号 

struct Node{        //链式前向星 struct
    int to;        //边的终点     
    int w;        //权值 
    int next;    //相同起点的上一次加入的边的存储位置 (指向下一条边在edge数组中的位置)
}edge[maxn*2];

void init(){
    tot = 0;
    memset(head,-1,sizeof(head));    //将head初始化为-1 
}

void add_edge(int from, int to, int w){        //from起点, to终点, w权值 
    edge[tot].to=to;
    edge[tot].w=w;
    edge[tot].next=head[from]; //head[from]:上一次加入的边的位置 
    head[from]=tot++;          //更新以from为起点的最新加入的边的编号 
}

链式前向星

原文:https://www.cnblogs.com/shiliuxinya/p/12037675.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!