Data Structure --- using the c language to store and represent arrays (adjacent matrices) of graphs; --- using the c matrix
// Map array (Adjacent matrix) Storage representation # include <stdio. h> # include <stdlib. h> # define limit 50 typedef char VertexType; typedef enum {DG, UDG} GraphType; typedef struct {VertexType vexs [limit]; int arcs [MAX_VEX_NUM] [MAX_VEX_NUM]; int vexnum, arcnum; GraphType type;} MGraph; // locate int getIndexOfVexs (char vex, MGraph * MG) {int I; for (I = 1; I <= MG-> vexnum; I ++) {if (MG-> vexs [I] = vex) {return I ;}} return 0 ;} // Create a void create_MG (MGraph * MG) {int I, j, k; int v1, v2, type; char c1, c2; printf ("Please input graph type DG (0) or UDG (1):"); scanf ("% d", & type); if (type = 0) MG-> type = DG; else if (type = 1) MG-> type = UDG; else {printf ("Please input correct graph type DG (0) or UDG (1 )! "); Return;} printf (" Please input vexmun: "); scanf (" % d ", & MG-> vexnum); printf (" Please input arcnum :"); scanf ("% d", & MG-> arcnum); getchar (); for (I = 1; I <= MG-> vexnum; I ++) {printf ("Please input % dth vex (char):", I); scanf ("% c", & MG-> vexs [I]); getchar ();} // initialize the adjacent matrix for (I = 1; I <= MG-> vexnum; I ++) {for (j = 1; j <= MG-> vexnum; j ++) {MG-> arcs [I] [j] = 0 ;}/// enter the edge information and establish the adjacent matrix for (k = 1; k <= MG-> arcnum; k ++) {printf ("Please input % dth arc v1 (char) v2 (char):", k ); scanf ("% c", & c1, & c2); v1 = getIndexOfVexs (c1, MG); v2 = getIndexOfVexs (c2, MG ); if (MG-> type = 1) MG-> arcs [v1] [v2] = MG-> arcs [v2] [v1] = 1; else MG-> arcs [v1] [v2] = 1; getchar () ;}/ *** print the adjacent matrix and vertex information */void print_MG (MGraph MG) {int I, j; if (MG. type = DG) {printf ("Graph type: Direct graph: \ n");} else {printf ("Graph type: Undirect graph: \ n ");} printf ("Graph vertex number: % d \ n", MG. vexnum); printf ("Graph arc number: % d \ n", MG. arcnum); printf ("Vertex set:"); for (I = 1; I <= MG. vexnum; I ++) printf ("% c", MG. vexs [I]); printf ("\ n"); printf ("Adjacency Matrix: \ n"); for (I = 1; I <= MG. vexnum; I ++) {j = 1; for (; j <MG. vexnum; j ++) {printf ("% d", MG. arcs [I] [j]);} printf ("% d", MG. arcs [I] [j]); printf ("\ n") ;}} int main () {MGraph MG; create_MG (& MG); print_MG (MG ); return 0 ;}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.