newwy
奋斗在IT路上的小蜗牛。一步一步往上爬,爬到小牛,在到大牛,然后是神牛,然后是犇,然后就可以离开IT行业,回归大自然了。 远离IT,珍爱生命!!! 记录学习的点滴。
/*******************************
*	
*	Author: Wang Yong
*	Blog: http://www.cnblogs.com/newwy
*
********************************/
#include <stdio.h>

#define MAXV  100
//定义最大顶点个数

//邻接矩阵的数据类型
typedef char ElemType;
typedef struct
{
	int no;			//顶点编号 
	ElemType info;	//顶点其他信息 
}VertexType;		//顶点类型

typedef struct		//图的定义 
{
	int edges[MAXV][MAXV];		//邻接矩阵
	int n,e;					//分别为顶点数和边数
	VertexType vexs[MAXV];		//存放顶点信息 
}MGraph;

void CreateMat(MGraph &g,int A[][MAXV],int n)//创建图的邻接矩阵;
{
	int i,j;
	g.n = n;
	g.e = 0;
	for(i = 0 ; i < n; i++)
		for(j = 0 ; j < n ; j++)
		{
			g.edges[i][j] = A[i][j];
			if(g.edges[i][j] != 0)
				g.e++;			//边数+1 
		}
}

void DisMat(MGraph g)
{
	int i,j;
	for(i = 0 ; i < g.n;i++)
	{
		for(j = 0 ; j < g.n;j++)
			printf("%4d",g.edges[i][j]);
		printf("\n");
	}
}
int main()
{
	MGraph g;
	printf("请输入顶点的数目:");
	int n;
	scanf("%d",&n);
	int A[100][100];
	int i,j;
	for(i = 0; i < n;i++)
		for(j = 0 ; j < n;j++)
			scanf("%d",&A[i][j]);
	CreateMat(g,A,n);
	DisMat(g);
} 
posted on 2010-10-30 22:09  newwy  阅读(1369)  评论(0编辑  收藏  举报