图的邻接矩阵表示-无向网图的初始化程序
| #include <stdio.h> |
| #include <stdlib.h> |
| #define MaxVertexNum 100 |
| #define INF 65535 |
| |
| typedef int Vertex; |
| typedef int WeightType; |
| typedef char DataType; |
| |
| |
| typedef struct GNode *PtrToGNode; |
| |
| struct GNode { |
| |
| int Nv; |
| int Ne; |
| |
| WeightType G[MaxVertexNum][MaxVertexNum]; |
| DataType Data[MaxVertexNum]; |
| }; |
| |
| typedef PtrToGNode MGraph; |
| |
| typedef struct ENode *PtrToENode; |
| |
| struct ENode { |
| Vertex V1, V2; |
| WeightType Weight; |
| }; |
| |
| typedef PtrToENode Edge; |
| |
| |
| |
| MGraph CreatGraph(int VertexNum) { |
| |
| |
| MGraph Graph; |
| |
| Graph = (MGraph)malloc(sizeof (struct GNode)); |
| Graph->Nv = VertexNum; |
| Graph->Ne = 0; |
| |
| for (int i = 0; i < Graph->Nv; i++) |
| for (int j = 0; j < Graph->Nv; j++) { |
| Graph->G[i][j] = INF; |
| } |
| |
| return Graph; |
| } |
| |
| |
| void InsertEdge(MGraph Graph, Edge E) { |
| |
| Graph->[E->V1][E->V2] = E->Weight; |
| |
| Graph->G[E->V2][E->V1] = E->Weight; |
| } |
| |
| MGraph BuildGraph() { |
| MGraph Graph; |
| Edge E; |
| Vertex V; |
| int Nv, i; |
| |
| scanf("%d", &Nv); |
| |
| Graph = CreatGraph(Nv); |
| |
| scanf("%d", &(Graph->Ne)); |
| |
| if (Graph->Ne != 0) { |
| |
| E = (Edge) malloc (sizeof (struct ENode)) ; |
| |
| for ( i = 0; i < Graph->Ne; i++) { |
| scanf("%d %d %d", &E->V1, &E->V2, &E->Weight ); |
| |
| |
| InsertEdge(Graph, E); |
| } |
| } |
| |
| |
| for(V=0 ; V<Graph->Nv;V++) |
| scanf("%c" , &(G->Data[V])); |
| |
| return Graph; |
| } |
| |
| |
| |
| |
| |
| |
| |
图的邻接表表示-无向图的初始化程序
| #include <stdio.h> |
| #include <stdlib.h> |
| #define MaxVertexNum 100 |
| #define INF 65535 |
| |
| |
| |
| |
| typedef int Vertex; |
| typedef int WeightType; |
| typedef char DataType; |
| |
| |
| typedef struct ENode *PtrToENode; |
| struct ENode{ |
| Vertex V1,V2; |
| WeightType weight; |
| |
| }; |
| |
| typedef PtrToENode Edge; |
| |
| |
| typedef struct AdjVNode *PtrToAdjVNode; |
| |
| struct AdjVNode{ |
| Vertex AdjV; |
| WeightType weight; |
| PtrToAdjVNode Next; |
| |
| }; |
| |
| |
| |
| typedef struct Vnode{ |
| PtrToAdjVNode FirstEdge; |
| DataType Data; |
| |
| |
| }AdjList[MaxVertexNum]; |
| |
| |
| |
| typedef struct GNode *PtrToGNode; |
| |
| struct GNode{ |
| int Nv; |
| int Ne; |
| AdjList G; |
| }; |
| |
| typedef PtrToGNode LGraph; |
| |
| |
| |
| LGraph CreatGraph(int VertexNum) |
| { |
| |
| Vertex V; |
| LGraph Graph; |
| |
| Graph=(LGraph)malloc(sizeof (struct GNode)); |
| Graph->Nv=VertexNum; |
| Graph->Ne=0; |
| |
| |
| for(V=0; V<Graph->Nv ;V++) |
| { |
| Graph->G[V].FirstEdge=NULL; |
| } |
| return Graph; |
| } |
| |
| void InsertEdge( LGraph Graph ,Edge E) |
| { |
| PtrToAdjVNode NewNode; |
| |
| |
| NewNode =(PtrToAdjVNode)malloc(sizeof (struct AdjVNode)); |
| NewNode->AdjV=E->V2; |
| NewNode->weight=E->weight; |
| |
| NewNode->Next=Graph->G[E->V1].FirstEdge; |
| Graph->G[E->V1].FirstEdge=NewNode; |
| |
| |
| |
| NewNode=(PtrToAdjVNode)malloc(sizeof (struct AdjVNode)); |
| NewNode->AdjV=E->V2; |
| NewNode->weight=E->weight; |
| NewNode->Next=Graph->G[E->V2].FirstEdge; |
| Graph->G[E->V2].FirstEdge=NewNode; |
| |
| } |
| |
| |
| LGraph BuildGraph() |
| { |
| LGraph Graph; |
| Edge E; |
| Vertex V; |
| int Nv, i; |
| |
| scanf("%d",&Nv); |
| Graph=CreatGraph(Nv); |
| |
| scanf("%d",&(Graph->Ne)); |
| if(Graph->Ne!=0) |
| { |
| E=(Edge)malloc(sizeof (struct ENode)); |
| |
| for(int i=0;i<Graph->Ne;i++){ |
| scanf("%d %d %d",&E->V1,&E->V2,&E->weight); |
| InsertEdge(Graph,E); |
| } |
| } |
| |
| |
| for(V=0;V<Graph->Nv; V++) |
| { |
| scanf("%c",&(Graph->G[V].Data)); |
| } |
| |
| return Graph; |
| |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】