Get the App
SLTechnology News&Howtos  ›  Development  › 

Using the adjacency table method to create a graph. How to write the complete C code?

Shulou Source: shulou.com Published: 2022-06-03 05:41:39 09月24日 Update

This article will explain in detail how to write the complete C code for using the adjacency table method to create a graph. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

/ * the C code implementation of creating graph by adjacency table method of undirected graph * / # include # define MaxSize 20 / / maximum number of vertices of graph typedef char VertexType;// global variable, recording the number of vertices of graph int VertexNum;// defining graph vertex typedef struct GraphNode {VertexType ver; struct GraphNode * next;} GraphNode;// creating graph void CreateGraph (GraphNode * * g) {VertexType ch by adjacency table method / / used to receive vertex names int I = 0; GraphNode * p, * Q; (* g) = (GraphNode *) malloc (sizeof (GraphNode) * MaxSize); / / assign a structure array printf ("Please enter vertices of the graph:\ n") / / store vertices of the graph scanf ("% c", & ch); while ('\ n'! = ch) {(* g) [I]. Ver = ch; (* g) [I]. Next = NULL; igraphs; scanf ("% c", & ch) } VertexNum = I; / / record the number of vertices for (iDe0; iver); scanf ("% c", & ch); while ('\ n'! = ch) {p = (GraphNode *) malloc (sizeof (GraphNode)) P-> ver = ch; Q-> next = p; Q = p; Q-> next = NULL; scanf ("% c", & ch) Void PrintGraph (GraphNode * g) {GraphNode * p; printf ("the vertex of the graph is:\ n"); / / print vertex for (int iTun0; inext;} printf ("\ n");}} int main () {GraphNode * g) CreateGraph (& g); PrintGraph (g); return 0;}

Diagram of the test:

Test result

On the realization of using the adjacency table method to create a graph, how to write the complete C code is shared here. I hope the above content can be helpful to everyone and learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Vertex code content quantity article more knowledge article test yes maximum global variable name array point structure result node quality Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Tech Info Apple Linux macOS