邻接矩阵存储方式

#include<bits/stdc++.h>

#define MAXV 5

#define INF 32767 //定义无穷 

typedef char InfoType;

typedef struct

{

 int no;

 InfoType info;

} VertexType;

typedef struct

{

 int edges[MAXV][MAXV];

 int n,e;

 VertexType vexs[MAXV];

}MatGraph;

void GreateMat(MatGraph &g,int A[MAXV][MAXV],int n,int e)

{

 int i,j;

 g.n=n;

 g.e=e;

 for(i=0;i<g.n;i++)

 {

  for(j=0;j<g.n;j++)

  {

   g.edges[i][j]=A[i][j];

  }

 }

}

void DisMat(MatGraph g)

{

 int i,j;

 for(i=0;i<g.n;i++)

 {

  for(j=0;j<g.n;j++)

  {

   if(g.edges[i][j]!=INF)

   {

    printf("%4d",g.edges[i][j]);

   }

   else

   {

    printf("%4s","∞");

   }

  }

  printf("\n");

 }

}

int main()

{

 MatGraph g;

 int A[MAXV][MAXV]={ {0,8,32767,5,32767},

                    {32767,0,3,32767,32767},

                    {32767,32767,0,32767,6},

                    {32767,32767,9,0,32767},

        {32767,32767,32767,32767,0}}; 

 int n=5,e=5;

 GreateMat(g,A,n,e);

 DisMat(g);

 return 0;

}

猜你喜欢

转载自blog.csdn.net/2302_77099705/article/details/130789451
今日推荐