#include<stdio.h>/*2009.10.25晚写于白鹿原*/ #include <malloc.h>/*邻接矩阵表示法的实现*/ #include <conio.h> #define MAX_VERTEX_NUM 20 #define INFINITY 2768 #define True 1 #define False 0 #define Error -1 #define Ok 1 typedef enum{DG,DN,UDG,UDN}GraphKind; typedef char VertexData; typedef struct ArcNode{ int adj; int info; }ArcNode; typedef struct{ VertexData vertex[MAX_VERTEX_NUM]; ArcNode arcs[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; int vexnum,arcnum; GraphKind kind; }AdjMatrix; int LocateVertex(AdjMatrix *G,VertexData v) /*求顶点位置函数*/ { int j=Error,k; for(k=0;k<G->vexnum;k++) if(G->vertex[k]==v) { j=k; break; } return(j); } int CreateDN(AdjMatrix *G) /*创建一个有向网*/ { int i,j,k,weight; VertexData v1,v2; printf("输入图的弧数和顶点数(num1,num2)/n"); fflush(stdin); scanf("%d,%d",&G->arcnum,&G->vexnum); /*输入图的顶点数和弧数*/ for(i=0;i<G->vexnum;i++) /*初始化邻接矩阵*/ for(j=0;j<G->vexnum;j++) G->arcs[i][j].adj=INFINITY; for(i=0;i<G->vexnum;i++) { printf("输入图的顶点/n"); fflush(stdin); scanf("%c",&G->vertex[i]); /* 输入图的顶点*/ } for(k=0;k<G->arcnum;k++) { printf("输入一条弧的两个顶点及权值(char,char,int)/n"); fflush(stdin); scanf("%c,%c,%d",&v1,&v2,&weight);/*输入一条弧的两个顶点及权值*/ i=LocateVertex(G,v1); j=LocateVertex(G,v2); G->arcs[i][j].adj=weight; /*建立弧*/ } return(Ok); } void main() { int i; AdjMatrix G; i=CreateDN(&G); if(i) printf("图建立成功!"); else printf("图建立不成功!"); }