Header file Graph.h
#ifndef _graph_h_#define _graph_h_#define max_ver 100#define endless 65535typedef char vertextype;typedef int edgetype;typedef struct _graph{ vertextype ver[max_ver]; Edgetype Edge[max_ver][max_ver]; int num_ver,num_edge; } Graph,*pgraph;int Locate (pgraph pg,char ch); void Creat_array_graph (Pgraph pg); void Print_graph (graph G); #endif
Implement the file Graph.c file as follows
/**************************** file name:/graph.c time: 2014.12.28xiao_ping_ping operating Environment: dev-c++ 4.9.9.2 Content: Graph adjacency matrix Storage data structure function: Write your own data structure *****************************/#include <string.h> #include <stdlib.h># Include "GRAPH.H" int locate (pgraph Pg,char ch) {int i = 0; while (ch! = Pg->ver[i]) {i++; } return i; }void Creat_array_graph (Pgraph PG) {int i,j; Char cout,cin; int num_out,num_in; int weight; printf ("Number of input vertices:"); scanf ("%d", &pg->num_ver); printf ("Number of input sides:"); scanf ("%d", &pg->num_edge); printf ("Input vertex data:"); for (i = 0;i < pg->num_ver;i++) {Pg->ver[i] = GetChar (); while (' \ n ' = = Pg->ver[i]) {Pg->ver[i] = GetChar (); } printf ("Point%d:%c\n", I,pg->ver[i]); } for (i = 0;i < pg->num_ver;i++) {for (j = 0;j < pg->num_ver;j++) {pg-> EDGE[I][J] = endless; }} for (i = 0; i < pg->num_edge;i++) {printf ("Enter the vertex to connect:"); cout = GetChar (); while (' \ n ' = = cout) {cout = GetChar (); } cin = GetChar (); while (' \ n ' = cin) {cin = GetChar (); } printf ("Weight:"); scanf ("%d", &weight); Num_out = Locate (pg,cout); num_in = Locate (pg,cin); PG->EDGE[NUM_OUT][NUM_IN] = weight; Pg->edge[num_in][num_out] = weight; }} void Print_graph (graph g) {int I, J; for (i = 0; i < g.num_ver; i++) {for (j = 0; J < G.num_ver; J + +) {printf ("%d", G.E DGE[I][J]); } printf ("\ n"); }}
Test file TEST.c
#include <string.h> #include <stdlib.h> #include "graph.h" int main () { graph gph; Creat_array_graph (&GPH); Print_graph (GPH); Getch (); return 0; }
The results of the operation are as follows:
Graph adjacency Matrix Storage data Structure--write data structure by itself