C Language Implementation diagram (graph)

Source: Internet
Author: User
Tags printf

Graph (graph) is a more complex data structure with linear table and data elements in linear table, only a direct precursor and a direct successor, in the tree structure, the data elements have a clear hierarchical relationship, and the data elements on each layer may be related to more than one element in the next layer, but only with an element in the previous layer, and in the graphics structure it appears that the data element is abnormally free, and may be related to any two elements in the diagram.

The first thing to say is about how the graph is stored, each element in the diagram is stored in a matrix, for the direction of the graph, the direction of the map, the network and the network is the same ....

Here is an example of how to create a diagram:

typedef int VRTYPE;   
typedef char InfoType;   
       
       
typedef char* VERTEXTYPE;   
typedef ENUM{DG, DN, UDG, UDN} graphkind;   
 typedef struct Arccell {vrtype adj;   
InfoType *info;   
       
       
}arccell, Adjmatrix[max_vertex_num][max_vertex_num];       typedef struct {Vertextype vexs[max_vertex_num];                       Vertex vector Adjmatrix arcs;             adjacency Matrix int vexnum, arcnum;                       The current vertex number and arc number of the graph are graphkind kind;   
       
The kind of the figure of the symbol}mgraph;   
 Returns the position of the specified vertex in the vertex vector int Locatevex (mgraph G, vertextype elem) {int i;   
          
 for (i = 0; i < G.vexnum. ++i) if (strcmp (Elem, g.vexs[i)) = = 0) return i;   
return error; }//No to net int createudn (mgraph *g) {int I, j, K, L, Incinfo, W;//incinfo indicates whether ARC has other information char S[max_info   
 ], *info;   
 Char va[5], vb[5];   
 printf ("Please enter the number of vertices, arcs, or whether the arc contains additional information (yes: 1, no: 0)"); scanf ("%d,%d,%d", & (*g). Vexnum, & (*g).Arcnum, &incinfo);   
 printf ("Enter the value of each vertex (<%d characters): \ n", max_name);   
  for (i = 0; i < (*g). Vexnum ++i)//construct vertex vector {(*g). Vexs[i] = (vertextype) malloc (sizeof (char) *5);   
  scanf ("%s", (*G). Vexs[i]);   
 GetChar (); for (i = 0; i < (*g). Vexnum ++i)//Initialize adjacency matrix for (j = 0; J < (*G). Vexnum; ++j) {(*g). arcs[i][j].a   
   DJ = 0;   
  (*g). Arcs[i][j].info = NULL;   
        
 printf ("Enter the Arc-tailed arc head (space interval) of%d arcs: \ n", (*g). Arcnum);   
  for (k = 0; k < (*g). Arcnum ++k) {scanf ("%s%s", VA, VB);//Input arc Head, ARC tail information printf ("Please enter the corresponding weight value of the arc:");   
  scanf ("%d", &w); i = Locatevex (*g, VA);//position arc End position, j = Locatevex (*g, VB);//Position arc Head position (*g). Arcs[i][j].adj = w;//weight value (*g). arcs[j][   
  I].adj = W;   
   if (incinfo) {printf ("Please enter the relevant information for the arc (<%d characters):", max_info);   
   scanf ("%s", s);   
   L = strlen (s);   
    if (l) {(*g). Arcs[i][j].info = (char *) malloc ((l+1) *sizeof (char));   
  strcpy ((*g). Arcs[i][j].info, s); }} (*g). Kind = DN;   
return true;   
int main (int argc, char *argv[]) {...; }

The above is just a step to the establishment of the net, for the other three kinds of diagram methods similar, I will not be redundant here. Hope to be in the confusion of the Buddy point help, but also look forward to master shot bricks.!!!

This article is from the "Late Evening" blog, please be sure to keep this source http://yiluohuanghun.blog.51cto.com/3407300/832537

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.