# Include <stdio. h> # include <stdlib. h> # define OK 1 # define error-1 # define overflow 0 # define maxver 20 // defines the maximum number of vertices # define maxint 200 // defines an infinite typedef Enum {udg, UDN, DG, DN} graphkind; // defines an undirected graph directed to a directed graph char vertype; // defines the vertex type typedef int status; typedef struct {vertype verx [maxint]; // defines the vertex int arcs [maxver] [maxver]; // defines the arc int vernum and arcsnum; // defines the maximum vertex count and the arc graphkind kind; // category} mgraph; int visited [maxver] = {0 }; // Vertex has not been accessed at the beginning // find the subscript int locate (mgraph g, vertype ch) {int I; for (I = 0; I <G. vernum & Ch! = G. verx [I]; I ++); return I ;}// create the undirected network status createudn (mgraph & G, Int & V) {int I, j, W, K; vertype vertex, CH2; printf ("Enter the number of undirected vertices and arcs in the format of 2 3 \ n"); scanf ("% d", & G. vernum, & G. arcsnum); fflush (stdin); printf ("Enter the vertex symbol \ n"); for (I = 0; I <G. vernum; I ++) {scanf ("% C", & G. verx [I]); fflush (stdin) ;}for (I = 0; I <G. vernum; I ++) {for (j = 0; j <G. vernum; j ++) g. ARCs [I] [J] = maxint; // The initial value is infinite} printf ("Enter the vertex symbol and weight: format a B 3 \ n "); for (I = 0; I <G. arcsnum; I ++) {printf ("Enter the % d pair value \ n", I + 1); scanf ("% C % d", & found, & CH2, & W); // enter the vertex symbol and weight fflush (stdin); k = locate (G, priority); // obtain the vertex subscript J = locate (G, CH2); G. ARCs [k] [J] = W; // assign a value to the critical matrix G. ARCs [J] [k] = G. ARCs [k] [J]; // returns OK for an undirected graph} Return OK;} // creates a directed network status createdn (mgraph & G, Int & V) {int I, j, W, K; vertype vertex, CH2; printf ("Enter the number of vertices and arcs for the Directed Network: Format: 2 3 \ n "); scanf ("% d", & G. vernum, & G. arcsnum); // Number of vertices and number of arcs input fflush (stdin); // clear the carriage return printf ("Enter the vertex Symbol: \ n"); for (I = 0; I <G. vernum; I ++) {scanf ("% C", & G. verx [I]); // vertex symbol input fflush (stdin) ;}for (I = 0; I <G. vernum; I ++) {for (j = 0; j <G. vernum; j ++) g. ARCs [I] [J] = maxint; // The initial value is infinite} printf ("Enter the vertex symbol and weight: format a B 3 \ n "); for (I = 0; I <G. arcsnum; I ++) {printf ("Enter the % d pair value \ n", I + 1); scanf ("% C % d", & found, & CH2, & W); // enter the vertex symbol and weight fflush (stdin); k = locate (G, priority); // obtain the vertex subscript J = locate (G, CH2); G. ARCs [k] [J] = W; // value for the critical matrix // G. ARCs [J] [k] = G. ARCs [k] [J]; // returns OK for an undirected graph} Return OK;} // perform depth-first traversal of void DFS (mgraph g, int V) {// parameter 1 Fig 2 passed in subscript int I; printf ("% C", G. verx [v]); visited [v] = 1; for (I = 0; I <G. vernum; I ++) if (visited [I] = 0 & G. ARCs [v] [I] <maxint) {// determines whether the access is a weight or edge DFS (G, I );}} // graph traversal void dfstravers (mgraph g) {int I; for (I = 0; I <G. vernum; I ++) {visited [I] = 0 ;}for (I = 0; I <G. vernum; I ++) {If (visited [I] = 0) DFS (G, I) ;}} int main () {mgraph g; int V = 0; if (createudn (G, V) {// create an undirected graph printf ("traversal result: \ n"); dfstravers (g ); // perform undirected graph traversal} If (createdn (G, V) {// create a directed graph printf ("traversal result: \ n"); dfstravers (g ); // traverse Directed Graphs} return 0 ;}