Data structure----diagram (adjacency matrix usage)

Source: Internet
Author: User

1, define the link matrix of the graph:

1 #define Vertex_max 62#define MAXVALUE 327673struct{4     int  Vertex[vertex_max]; 5     int Edges[vertex_max][vertex_max]; 6     int Vertexnum,edgesnum; 7     int Graptype; 8 }matrixgraph;

2, define the function creategraph, create the adjacency matrix of the graph:

1 voidCreategraph (Matrixgraph *g) {2     intStart,end;3printf"Please input vertexs\n");4      for(intI=0; i<g->vertexnum;i++){5printf"The %d vertex is", i+1);6scanf"%d",&g->vertex[i]);7     }8printf"\nplease input the edges. The former is, the first input is the start vertex and the second input is the end vertex!\n");9      for(intI=0; i<g->edgesnum;i++){Tenprintf"The %d edge: (Please input data like above!) ", i+1); Onescanf"%d%d",&start,&end); Ag->edges[start-1][end-1]=1; -         if(g->graptype==0) -g->edges[end-1][start-1]=1; the     } -}

3. Define the contents of the function output neighbor matrix:

1 voidPrintgraph (Matrixgraph *g) {2printf"  ");3      for(intI=0; i<g->vertexnum;i++){4printf"%d",g->vertex[i]);5     }6      for(intI=0; i<g->vertexnum;i++){7printf"\n%d",g->vertex[i]);8          for(intj=0; j<g->vertexnum;j++){9             if(g->edges[i][j]==MAXVALUE)Tenprintf"@ "); One             Else Aprintf"%d",g->edges[i][j]); -         } -     } theprintf"\ n"); -}

4. Main function:

1 intMain () {2Matrixgraph *G;3g= (Matrixgraph *)malloc(sizeof(Matrixgraph));4printf"Please select the type of grap:0 are Undigrap, 1 is digrap.\n");5scanf"%d",&g->graptype);6printf"Please input the vertexnum and edgenum of matrixgraph!\n");7scanf"%d%d",&g->vertexnum,&g->edgesnum);8      for(intI=0; i<g->vertexnum;i++){9          for(intj=0; j<g->vertexnum;j++){Teng->edges[i][j]=MAXVALUE; One         } A     } - Creategraph (g); -printf"oupt the matrixgrap. \ n"); the Printgraph (g); -      Free(g); - getch (); -     return 0; +}

Note: In the main function

Matrixgraph *g;

Can be changed to write

Matrixgraph G;
1printf"Please select the type of grap:0 are Undigrap, 1 is digrap.\n");2scanf"%d",&g.graptype);3printf"Please input the vertexnum and edgenum of matrixgraph!\n");4scanf"%d%d",&g.vertexnum,&g.edgesnum);5      for(intI=0; i<g.vertexnum;i++){6          for(intj=0; j<g.vertexnum;j++){7g.edges[i][j]=MAXVALUE;8         }9     }Ten    creategraph (&g); Oneprintf"oupt the matrixgrap. \ n"); A    printgraph (&g); - getch (); -     return 0;

However, it is necessary to refer to the struct variable later. Instead of using->, and need to make space for struct pointer variables first.

In C + +, if the object is a G, you can pass the "." To invoke the member variable in the I.
If G is a pointer, it cannot pass "." And can only be called with "--".
There is no concept of objects in the C language.
This situation arises because of the use of structures, such as:

In the program

We can use G.vertexnum to get the values in the structure.
At this point, you cannot use "-a" to invoke the "--" symbol pointer to the pointer.
The following conditions can be used

Matrixgraph *g;

At this point, G is an address pointer to a MATRIXGRAPH structure. So you can use "--" and you can't use it at this time
"." to operate. Because "." Called by a member of the "equivalent" object.

Final Display Result:

Data structure----diagram (adjacency matrix usage)

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.