直接给图的邻接矩阵代码:
//图的两种创建方法-邻接矩阵、邻接表
//邻接矩阵
#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;
*/
}
}