#include<stdio.h> #include<stdlib.h> #include<string.h> #define Max 20 typedef char vertextype[Max]; typedef int InfoType; typedef struct ArcBox { int tailvex,headvex; struct ArcBox *hlink,*tlink; InfoType info; }ArcBox; typedef struct vexnode { vertextype data; ArcBox *firstin,*firstout; }vexnode; typedef struct { vexnode xlist[Max]; int vexnum,arcnum; }OLGraph; OLGraph G; int LocateVex(OLGraph *H,vertextype c) { int i; for(i=0;i<G.vexnum;i++) { if(strcmp(c,H->xlist[i].data)) return i; } return 0; } main() { vertextype a,b; InfoType intout; ArcBox *p; int i,j,k; printf("please input the number of vertex and arcnode,IncInfo(0 or 1):\n"); scanf("%d%d%d",&G.vexnum,&G.arcnum,&intout); fflush(stdin); printf("\n please input the vertex:\n"); for(i=0;i<G.vexnum;i++) { scanf("%s",&G.xlist[i].data); G.xlist[i].firstin=NULL; G.xlist[i].firstout=NULL; } printf("\n please input the arcs:\n"); for(k=0;k<G.arcnum;++k) { scanf("%s%s",a,b); i=LocateVex(&G,a); j=LocateVex(&G,b); p=(ArcBox *)malloc(sizeof(ArcBox)); if(!p) { printf("overflow!\n"); exit(0); } else { p->tailvex=i; p->headvex=j; G.xlist[j].firstin=G.xlist[i].firstout=p; if(intout) { printf("please input the information:\n"); scanf("%d",p->info); } } } }

评论