Graph adjacency Matrix Storage data Structure--write data structure by itself

Source: Internet
Author: User

Header file Graph.h


#ifndef _graph_h_#define _graph_h_#define  max_ver 100#define  endless 65535typedef char vertextype;typedef int  edgetype;typedef struct _graph{    vertextype  ver[max_ver];    Edgetype    Edge[max_ver][max_ver];    int num_ver,num_edge;    } Graph,*pgraph;int Locate (pgraph pg,char ch); void Creat_array_graph (Pgraph pg); void Print_graph (graph G); #endif

Implement the file Graph.c file as follows

/**************************** file name:/graph.c time: 2014.12.28xiao_ping_ping operating Environment: dev-c++ 4.9.9.2 Content: Graph adjacency matrix Storage data structure function: Write your own data structure *****************************/#include <string.h> #include <stdlib.h>#        Include "GRAPH.H" int locate (pgraph Pg,char ch) {int i = 0;       while (ch! = Pg->ver[i]) {i++;    } return i;      }void Creat_array_graph (Pgraph PG) {int i,j;    Char cout,cin;    int num_out,num_in;    int weight;     printf ("Number of input vertices:");    scanf ("%d", &pg->num_ver);     printf ("Number of input sides:");    scanf ("%d", &pg->num_edge);     printf ("Input vertex data:");        for (i = 0;i < pg->num_ver;i++) {Pg->ver[i] = GetChar ();                   while (' \ n ' = = Pg->ver[i]) {Pg->ver[i] = GetChar ();     } printf ("Point%d:%c\n", I,pg->ver[i]); } for (i = 0;i < pg->num_ver;i++) {for (j = 0;j < pg->num_ver;j++) {pg->                    EDGE[I][J] = endless; }} for (i = 0; i < pg->num_edge;i++) {printf ("Enter the vertex to connect:");        cout = GetChar ();                   while (' \ n ' = = cout) {cout = GetChar ();        } cin = GetChar ();                   while (' \ n ' = cin) {cin = GetChar ();         } printf ("Weight:");        scanf ("%d", &weight);        Num_out = Locate (pg,cout);        num_in = Locate (pg,cin);        PG-&GT;EDGE[NUM_OUT][NUM_IN] = weight;    Pg->edge[num_in][num_out] = weight;    }} void Print_graph (graph g) {int I, J; for (i = 0; i < g.num_ver; i++) {for (j = 0; J < G.num_ver; J + +) {printf ("%d", G.E        DGE[I][J]);    } printf ("\ n"); }}

Test file TEST.c

#include <string.h> #include <stdlib.h> #include "graph.h" int main () {    graph gph;        Creat_array_graph (&GPH);    Print_graph (GPH);        Getch ();    return 0;    }

The results of the operation are as follows:


Graph adjacency Matrix Storage data Structure--write data structure by itself

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.