图的邻接矩阵

直接给图的邻接矩阵代码:

//图的两种创建方法-邻接矩阵、邻接表
//邻接矩阵
#include <stdio.h>
#include <stdlib.h>
#define Inf 10000000//无穷大
#define Max 20 //图中顶点个数最多为20
typedef char type_data;
typedef enum type_grap{DG,UDG,DN,UDN};
typedef struct grap{
	type_data node[Max+1];
	int adjmatrix[Max+1][Max+1];
	int nodenum,arcnum;
	type_grap type;
}Grap,*Gpivot;
int Locate(Gpivot p,char str){
	for(int i=1;i<=p->nodenum;i++)
		if(p->node[i]==str)
			return i;
}

void Creat_grap(Gpivot p){
	printf("please input the num of node\n");
	scanf("%d",&p->nodenum);
	printf("please input the num of arc\n");
	scanf("%d",&p->arcnum);
	int i;
	printf("please input the data of node \
	       ,their number is %d\n",p->nodenum);
    for(i=1;i<=p->nodenum;i++){
		p->node[i]=getchar();
		getchar();
	}
	printf("please input the arc,their number is %d\n",p->arcnum);
	int s,t,value;
	char sstr,tstr;
	for(i=1;i<=p->nodenum;i++)
		for(int j=1;j<=p->nodenum;j++)
			p->adjmatrix[i][j]=Inf;
	for(i=1;i<=p->arcnum;i++){
	/*sstr=getchar();
	getchar();
	tstr=getchar();
	getchar();*/
	    scanf("%c %c%d",&sstr,&tstr,&value);
		getchar();
	s=Locate(p,sstr);
	t=Locate(p,tstr);
	p->adjmatrix[s][t]=value;
	/* 无向图
	if(s!=t)
	p->adjmatrix[t][s]=value;
	*/
	}
}

	
	
	
		
	



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值