邻接矩阵
/*邻接矩阵*/
#include<stdio.h>
#include<stdlib.h>
#define INFINITY INT_MAX
#define MAX_VERTER_NUM 20
typedef enum{DG,DN,UDG,UDM}GraphKind;
typedef struct ArcCell
{
char adj;
char info[20];
}ArcCell,AdjMatrix[MAX_VERTER_NUM][MAX_VERTER_NUM];
typedef struct MGraph
{
int vexs[MAX_VERTER_NUM];
AdjMatrix arcs;
int vexnum,arcnum;
GraphKind kind;
}MGraph;
void Print_MGraph(MGraph * G)
{
int b,q;
printf("the graphic is:\n");
for(q=0;q<G->vexnum;q++)
{
for(b=0;b<G->vexnum;b++)
{
if(G->arcs[q][b].adj!='a')
{
printf("%d <----> %d , W<%d,%d>=%d",G->vexs[q],G->vexs[b],G->vexs[q],G->vexs[b],G->arcs[q][b].adj);
printf("\n");
}
}
}
}
int LocateVex(MGraph * G,int v) /*寻找节点V的位置*/
{
int k,n;
for(k=0;k<G->vexnum;k++){
if(G->vexs[k]==v){
n=k;
break;
}
}
return n;
}
void Init_MGraph(MGraph * G)
{
int v1,v2;
int w,vexnum,arcnum,incinfo;
int e,i,j,k,l,h,r,p,n,m,q,b;
printf("please input vexter & arcnum:\n");
scanf("%d,%d",&G->vexnum,&G->arcnum);
printf("\nplease input vexnum :\n");
for(e=0;e<G->vexnum;e++){printf("Vex%d: ",(e+1));scanf("%d",&G->vexs[e]);}
for(i=0;i<G->vexnum;i++)
for(j=0;j<G->vexnum;j++)
{
G->arcs[i][j].adj='a';
}
for(k=0;k<G->arcnum;k++)
{
printf("please input vexter :\n");
scanf("%d,%d",&v1,&v2);
printf("\nW<%d,%d>=",v1,v2);scanf(" %d",&w);
n=LocateVex(G,v1);
m=LocateVex(G,v2);
G->arcs[n][m].adj=w;
G->arcs[m][n]=G->arcs[n][m];
}
printf("the AdjMatrix is:\n");
for(r=0;r<G->vexnum;r++)
{
for(p=0;p<G->vexnum;p++)
{printf("%d ",G->arcs[r][p].adj);}
printf("\n");
}
Print_MGraph(G);
}
int main(int argc,char **argv)
{
MGraph G;
Init_MGraph(&G);
return 0;
}