Construct a directed graph
边关系:5 a b 7 a d 4 b c 8 c a 9 c f 5 d c 6 d f 5 e d 1 f e 3 f a
#include <stdio.h> #include <stdlib.h> #define MaxVertexNum 100 typedef char VertexType; // data type vertex node typedef int EdgeType; // Data type edge weight value typedef struct { VertexType Vertex [MaxVertexNum]; // vertex table EdgeType edge [MaxVertexNum] [MaxVertexNum]; // adjacency matrix, edge list int vertexnum, arcnum; // number of vertices and edges } MGraph; // store adjacency matrix structure typedef struct ArcNode { int adjvex; / / the arc pointed position of the vertex struct* Next ArcNode; // points to the next pointer arc } ArcNode; // edge node table typedef struct vnode { VertexType Data; // vertex information ArcNode * First; // pointer to the vertex of the arc attachment Article } VNode , AdjList [MaxVertexNum]; // vertex node table typedef struct { AdjList the vertices; // adjacency list int vexnum, arcnum; // number of vertices and the number of arcs of FIG } ALGraph; // storage structure adjacent to the table int the Locate (V VertexType ) { IF (V> = 97 ) { return ( int ) V- 97;} The else { return ( int ) V- 65 ;} } // will be converted to the corresponding position of the vertex void CreatGraph (MGraph & M) { EdgeType E; VertexType V1, V2; the printf ( " input nodes and edges: " ); Scanf ( " % D% D " , & M.vertexnum, & M.arcnum); the printf ( " input node: " ); for ( int I = 0 ; I <M.vertexnum; I ++ ) { getchar (); scanf ( " % c ", & M.Vertex [I]); } for ( int I = 0 ; I <M.vertexnum; I ++ ) { for ( int J = 0 ; J <M.vertexnum; J ++ ) { M.Edge [I] [ J] = 0 ; } } fflush (stdin); // clear the cache, to prevent the next scanf () function fails the printf ( " weights and two input nodes of the path: \ n- " ); for ( int I = 0 ; I <M.arcnum; I ++ ) { Scanf ( " % C% C% D " , E &, & V1, & V2); M.Edge [the Locate (V1)] [the Locate (V2)] = E; } } // construct a directed graph void PrintGraph (MGraph & M) { the printf ( " adjacency matrix \ n- " ); for ( int I = 0 ; I <M.vertexnum; I ++ ) { the printf ( " % C " , M.Vertex [I]); for ( int J = 0 ; J <M.vertexnum; J ++ ) { IF (M.Edge [I] [J]! = 0 ) { the printf ( " % D " , M.Edge [I] [J]); } else{printf("0 ");} } printf("\n"); } }//打印邻接矩阵 void CreatMGtoNG(MGraph &M,ALGraph &G){ ArcNode *p; G.vexnum=M.vertexnum; G.arcnum=M.arcnum; for(int i=0;i<G.vexnum;i++){ G.vertices[i].data=M.Vertex[i]; G.vertices[i].first=NULL; } for (int I = 0 ; I <G.vexnum; I ++ ) { for ( int J = 0 ; J <G.vexnum; J ++ ) { IF ! (M.Edge [I] [J] = 0 ) { P = ( * ArcNode) the malloc ( the sizeof (ArcNode)); P -> adjvex = J; P -> Next = G.vertices [I] .first; G.vertices [I] .first = P; } } } } // will abutting adjacency matrix table is converted into void PrintALGraph (& ALGraph G) { ArcNode *p; printf("邻接表\n")
for(int i=0;i<G.vexnum;i++){ p=G.vertices[i].first; printf(" %c",G.vertices[i].data); while(p){ printf("->%c<%d>",p->adjvex+'a',p->adjvex); p=p->next; } printf("\n"); } } // print adjacency list int main () { MGraph M; ALGraph G; CreatGraph (M); PrintGraph (M); CreatMGtoNG (M, G); PrintALGraph (G); return 0 ; }