首页 > 代码库 > 链式前向星储存
链式前向星储存
#include <iostream> #include <cstring> using namespace std; #define E 10000 #define V 100 struct Edge{ int to_node; int edge_val; int next_edge; Edge(){} Edge( int to, int val, int next ){ to_node = to; edge_val = val; next_edge = next; } }; Edge edges[E]; int heads[V]; int nEdgeNum, nNodeNum; int nEdgeCount; void addEdge( int from, int to, int dist ){ edges[nEdgeCount].to_node = to; edges[nEdgeCount].edge_val = dist; edges[nEdgeCount].next_edge = heads[from]; heads[from] = nEdgeCount; nEdgeCount++; } void showGraph(){ for( int i = 1; i <= nNodeNum; ++i ){ cout << i; for( int j = heads[i]; ~j; j = edges[j].next_edge ){ cout << "-->" << edges[j].to_node << "(" << edges[j].edge_val << ")"; } cout << "-->NULL" << endl; } } int main(){ int from, to, dist; while( cin >> nNodeNum >> nEdgeNum ){ memset( heads, -1, sizeof( heads ) ); for( int i = 1; i <= nEdgeNum; ++i ){ cin >> from >> to >> dist; addEdge( from, to, dist ); } showGraph(); } return 0; }
链式前向星储存
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。