图的创建

1.无向图 邻接矩阵表示

 1 //创建一个无向图 邻接矩阵表示法
 2 #include<bits/stdc++.h>
 3 using namespace std;
 4 #define MAX_NUM 100
 5 typedef struct {
 6     char vertex[MAX_NUM];//顶点信息
 7     int arcs[MAX_NUM][MAX_NUM];//矩阵信息
 8     int vexnum,arcsnum;//前者顶点数 后者边数
 9 }Graph;
10 void CreateGraph(Graph *graph){//引用传递
11 
12        int i,j;
13        cout<<"输入图的顶点数"<<endl;
14        cin>>graph->vexnum;//给的信息是图的顶点数a,则矩阵就是aXa的矩阵
15        for(i=0;i<graph->vexnum;i++){
16           for(j=0;j<graph->vexnum;j++){
17              cin>>graph->arcs[i][j];//输入各个信息 0/1
18           }
19        }
20 
21 }
22 void ShowGraph(Graph *graph){
23 
24      int i,j;
25      for(i=0;i<graph->vexnum;i++){
26           for(j=0;j<graph->vexnum;j++){
27              cout<<graph->arcs[i][j]<<" ";//输出矩阵啊
28           }
29           cout<<endl;
30        }
31 }
32 int main(){
33 
34 
35      Graph *graph;
36      graph=(Graph*)malloc(sizeof(Graph));
37      CreateGraph(graph);
38      cout<<"无向图的邻接矩阵输出"<endl;
39      ShowGraph(graph);
40 
41 }
原文地址:https://www.cnblogs.com/yundong333/p/11017294.html