diff --git "a/\345\233\276\347\232\204\345\237\272\347\241\200\344\275\234\344\270\232.txt" "b/\345\233\276\347\232\204\345\237\272\347\241\200\344\275\234\344\270\232.txt" new file mode 100644 index 0000000000000000000000000000000000000000..73b10d4479a52e32bbf57de9a725e287b42705de --- /dev/null +++ "b/\345\233\276\347\232\204\345\237\272\347\241\200\344\275\234\344\270\232.txt" @@ -0,0 +1,165 @@ +邻接矩阵表示法 +#define MaxVertexNum 100 +#define INFINITY 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 GNode *PtrToGNode; +struct GNode{ + int Nv; + int Ne; + WeightType G[MaxVertexNum][MaxVertexNum]; + DataType Data[MaxVertexNum]; + +}; +typedef PtrToGNode MGraph; + + + +MGraph CreateGraph( int VertexNum ) +{ + Vertex V, W; + MGraph Graph; + + Graph = (MGraph)malloc(sizeof(struct GNode)); + Graph->Nv = VertexNum; + Graph->Ne = 0; + for (V=0; VNv; V++) + for (W=0; WNv; W++) + Graph->G[V][W] = INFINITY; + + return Graph; +} + +void InsertEdge( MGraph Graph, Edge E ) +{ + Graph->G[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 = CreateGraph(Nv); + + scanf("%d", &(Graph->Ne)); + if ( Graph->Ne != 0 ) { /* 如果有边 */ + E = (Edge)malloc(sizeof(struct ENode)); + for (i=0; iNe; i++) { + scanf("%d %d %d", &E->V1, &E->V2, &E->Weight); + InsertEdge( Graph, E ); + } + } + for (V=0; VNv; V++) + scanf(" %c", &(Graph->Data[V])); + + return Graph; +} +图的邻接表表示法 + +#define MaxVertexNum 100 +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 CreateGraph( int VertexNum ) +{ + Vertex V; + LGraph Graph; + + Graph = (LGraph)malloc( sizeof(struct GNode) ); + Graph->Nv = VertexNum; + Graph->Ne = 0; + + for (V=0; VNv; 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->V1; + 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 = CreateGraph(Nv); + + scanf("%d", &(Graph->Ne)); + if ( Graph->Ne != 0 ) { + E = (Edge)malloc( sizeof(struct ENode) ); + + for (i=0; iNe; i++) { + scanf("%d %d %d", &E->V1, &E->V2, &E->Weight); + InsertEdge( Graph, E ); + } + } + for (V=0; VNv; V++) + scanf(" %c", &(Graph->G[V].Data)); + + return Graph; +} +图遍历 \ No newline at end of file