[Library] C realization map (array representation)

xiaoxiao2021-03-06  96

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/ * A simplechart implementation, only provides the most basic interface * non-shaped diagram stored in an array, this code uses a new Bool type of C99. * Compose this code * / #ifndef graph_h #define graph_h / * function return status * / #ifndef ok #define OK 1 / * Success * / #define err -1 / * failed * / #ENDIF / * Define Maximum Size * / #ifndef Max #define Max 100 #define min 0 #ndif / ** / typefin 0 #Endif / ** / typefine system * / * user Change this data type * / type; / * Data data * / int Arcn; / * graph * /} date; / * map of the graph * / typef Bool Adjmatrix [MAX] [MAX]; / * The relationship matrix type * / typef struct {date vec [max]; / * Storage vertex vector * / adjmatrix arcs; / * diagram relationship matrix * / int node_number, arc_number; / * vertex and Number of edges * /} graph; / * A graph type definition * / status creategraph (graph * gptr); / * Create a picture * / int Find_Gnode (const graph *, const elemType); / * Find a vertex * / status Print_Graph (const graph * gptr); / * Print * / #endif

--------------------- Graph.c ---------------------------------------------------------------------------------------------------- -----------------

#include

#include "graph.h"

Status Creategraph (Graph * GPTR) / * Establishment Figure * /

{

INT I, J, K;

IF (gptr == null) / * If the pointer is empty * /

Return ERR;

PRINTF ("Please enter the number of vertices:");

Scanf ("% d", & gptr-> node_number);

Printf ("Please enter the number of edges:");

Scanf ("% D", & gptr-> arc_number);

Printf ("Please enter% D data:", gptr-> node_number);

For (i = 0; i node_number; i)

{/ * Initialize and set data vectors * /

Scanf ("% d", & gptr-> vec [i] .date);

GPTR-> VEC [I] .arcn = 0;

}

For (i = 0; i node_number; i)

For (j = 0; j node_number; j) GPTR-> ARCS [i] [j] = false; / * Initialization relational matrix * /

For (k = 0; k arc_number; k)

{/ * Design relationship matrix * /

ElemType A, B;

Printf ("Please enter the data of adjacent nodes:");

Scanf ("% D% D", & A, & B);

i = find_gnode (gptr, a);

J = Find_Gnode (GPTR, B);

IF ((i <0) || (j <0))

Return ERR;

GPTR-> ARCS [I] [J] = GPTR-> ARCS [J] [I] = true;

}

For (i = 0; i node_number; i)

For (j = 0; j node_number; J)

GPTR-> VEC [I] .arcn = GPTR-> ARCS [i] [j];

/ * Calculate the degree of each vertex * /

Return OK;

}

INT FIND_GNODE (const graph * gptr, const ele mtype date)

{/ * Find vertices * /

INT I;

IF (gptr == null) / * If the pointer is empty, return negative value * /

Return -1;

For (i = 0; i node_number; i)

IF (Date == GPTR-> VEC [i] .date)

Return I;

Return -1; / * Nothing to return negative * /

}

Status Print_Graph (const graph * gptr) / * Print * /

{

INT I, J;

IF (gptr == null)

Return ERR;

Printf ("The storage data (node ​​degrees) is as follows: / n");

For (i = 0; i node_number; i)

Printf ("% d) / t", gptr-> vec [i] .date, gptr-> vec [i] .arcn);

Printf ("/ n");

Printf ("The relationship matrix of the figure is as follows: / N");

For (i = 0; i node_number; i)

{For (j = 0; j node_number; j)

Printf ("% D / T", GPTR-> ARCS [i] [j]);

Printf ("/ n");

}

Return OK;

}

转载请注明原文地址:https://www.9cbs.com/read-123607.html

New Post(0)