Data Structure --- using the C language to store and represent arrays (adjacent matrices) of Graphs

Source: Internet
Author: User

Data Structure --- using the C language to store and represent arrays (adjacent matrices) of Graphs

// Storage representation of the graph array (Adjacent matrix) # include
 
  
# Include
  
   
# Define limit 50 typedef char VertexType; typedef enum {DG, UDG} GraphType; typedef struct {VertexType vexs [MAX_VEX_NUM]; int arcs [MAX_VEX_NUM] [limit]; int vexnum, arcnum; graphType type;} MGraph; // locate int getIndexOfVexs (char vex, MGraph * MG) {int I; for (I = 1; I <= MG-> vexnum; I ++) {if (MG-> vexs [I] = vex) {return I ;}return 0 ;}// create a diagram void create_MG (MGraph * MG) {int I, j, k; int v1, V2, type; char c1, c2; printf (Please input graph type DG (0) or UDG (1) :); scanf (% d, & type ); if (type = 0) MG-> type = DG; else if (type = 1) MG-> type = UDG; else {printf (Please input correct graph type DG (0) or UDG (1 )!); Return;} printf (Please input vexmun:); scanf (% d, & MG-> vexnum); printf (Please input arcnum:); scanf (% d, & MG-> arcnum); getchar (); for (I = 1; I <= MG-> vexnum; I ++) {printf (Please input % dth vex (char) :, I); scanf (% c, & MG-> vexs [I]); getchar () ;}// initialize the adjacent matrix for (I = 1; I <= MG-> vexnum; I ++) {for (j = 1; j <= MG-> vexnum; j ++) {MG-> arcs [I] [j] = 0 ;}// enter the edge information and create an adjacent matrix for (k = 1; k <= MG-> arcnum; k ++) {printf (Please input % dth arc v1 (char) v2 (char):, k); scanf (% c, & c1, & c2 ); v1 = getIndexOfVexs (c1, MG); v2 = getIndexOfVexs (c2, MG); if (MG-> type = 1) MG-> arcs [v1] [v2] = MG-> arcs [v2] [v1] = 1; else MG-> arcs [v1] [v2] = 1; getchar () ;}/ *** print the adjacent matrix and vertex information */void print_MG (MGraph MG) {int I, j; if (MG. type = DG) {printf (Graph type: Direct graph :);} else {printf (Graph type: Undirect graph :);} printf (Graph vertex number: % d, MG. vexnum); printf (Graph arc number: % d, MG. arcnum); printf (Vertex set :); for (I = 1; I <= MG. vexnum; I ++) printf (% c, MG. vexs [I]); printf (Adjacency Matrix :); for (I = 1; I <= MG. vexnum; I ++) {j = 1; for (; j <MG. vexnum; j ++) {printf (% d, MG. arcs [I] [j]);} printf (% d, MG. arcs [I] [j]); printf () ;}} int main () {MGraph MG; create_MG (& MG); print_MG (MG); return 0 ;}
  
 

 

 

Related Article

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.