Data Structure --- using the C language to store and represent arrays (adjacent matrices) of Graphs
// Storage representation of the graph array (Adjacent matrix) # include
# Include
# Define limit 50 typedef char VertexType; typedef enum {DG, UDG} GraphType; typedef struct {VertexType vexs [MAX_VEX_NUM]; int arcs [MAX_VEX_NUM] [limit]; 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 diagram 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 create an 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 :);} else {printf (Graph type: Undirect graph :);} printf (Graph vertex number: % d, MG. vexnum); printf (Graph arc number: % d, MG. arcnum); printf (Vertex set :); for (I = 1; I <= MG. vexnum; I ++) printf (% c, MG. vexs [I]); printf (Adjacency Matrix :); 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 () ;}} int main () {MGraph MG; create_MG (& MG); print_MG (MG); return 0 ;}