分类: C/C++
2007-01-18 20:31:28
#include#include #define MAX_VERTEX_NUM 20 typedef enum {DG, DN, AG, AN}GraphKind; typedef struct ArcNode { int adjvex; struct ArcNode *nextarc; // int info; }ArcNode; typedef struct VNode { char data; struct ArcNode *firstarc; }VNode, AdjList[MAX_VERTEX_NUM]; typedef struct { AdjList vertices; int vernum, arcnum; GraphKind kind; }Graph; int Locate(Graph G, char ch) { int i; for(i=0; i kind); getchar(); printf("\nPlease input the number of vertices:"); scanf("%d", &G->vernum); getchar(); printf("\nPlease input the number of arcs:"); scanf("%d", &G->arcnum); getchar(); switch(G->kind) { case DG: for(i=0; i vernum; i++) { printf("\nPlease input the data of the %dth vertices:", i); scanf("%c", &G->vertices[i].data); getchar(); G->vertices[i].firstarc = NULL; } for(i=0; i arcnum; i++) { printf("\nPlease input the start-point and end-point of the %dth arc:", i); scanf("%c%c", &v1, &v2); getchar(); j = Locate(*G, v1); k = Locate(*G, v2); p = (ArcNode *)malloc(sizeof(ArcNode)); p->adjvex = k; p->nextarc = G->vertices[j].firstarc; G->vertices[j].firstarc = p; } break; default: break; } } void display(Graph G) { int i; ArcNode *p; for(i=0; i adjvex]); p = p->nextarc; } printf("\n"); } } main() { Graph graph; Create(&graph); display(graph); }