首页 > 编程知识 正文

带头节点的双向循环链表,链式前向星记录图

时间:2023-05-06 13:34:03 阅读:17655 作者:1463

链式正向星作用保存图中边的数据结构。 必须保存该关系,如下图所示

保存关系为以下:

说明如下。

head[i]表示以I为起点的第一条边

int to; //这一带的终点int w; //权重int next; //兄弟边的结构如下定义边的结构体。 前方之星加上边横穿前方之星边的结构体struct EDGE{ int to; //这一带的终点int w; //权重int next; //兄弟一方; EDGE edge[MAXM]; 加边方法voidaddedge(intu,int v,int w )//起点u,终点v,权重w edge[ cnt].next=head[u]; //next定义了起点u,终点v的边缘还有哪些其他的edge[cnt].w=w; edge[cnt].to=v; head[u]=cnt; //cnt是边的号码。 head[u]=cnt表示起点u的最新边缘为cnt编号的边缘。 }遍历从节点编号为st的节点开始,遍历其所有子节点。

for(intI=head[ST]; I!=0; I=edge [ I ].next } { cout ' start : ' stendl; cout 'End: ' edge[i].to endl; cout 'W: ' edge[i].w endl endl; }完整代码# includeiostreamusingnamespacestd; # define maxm 500010 # define maxn 10010结构边缘{ int to; //这一带的终点int w; //权重int next; //兄弟一方; EDGE edge[MAXM]; int n,m,cnt; int head[MAXN]; //head[i]是以I为起点的第一边voidaddedge(intu,int v,int w ) ) /起点u,终点v,权重w edge[ cnt].next=head[u]; //next定义了起点u,终点v的边缘还有哪些其他的edge[cnt].w=w; edge[cnt].to=v; head[u]=cnt; //cnt是边的号码。 head[u]=cnt表示起点u的最新边缘为cnt编号的边缘。 }void Print ()//I以第一边开始,每次指向下一边) 0作为结束标志) )如果下标从0开始,则next为-1for(intst=1; st=n; ST//n个起点for(intI=head[ST]; I!=0; I=edge [ I ].next } { cout ' start : ' stendl; cout 'End: ' edge[i].to endl; cout 'W: ' edge[i].w endl endl; }}int main () { int s,t,w; cin n m; for(intI=1; i=m; I ) { cin s t w; 添加ge (s,t,w ); (}打印); 返回0; (/*57121232343134415156457 ) /

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。