//Function use: 选择创建各类图 Status CreateGraph(MGraph *pG) { int nSel; printf("请输入图的类型:(0:有向图 1:有向网 2.无向图 3.无向网:"); scanf("%d", &nSel); switch (nSel) { case 0: CreateDG(pG); break; case 1: CreateDN(pG); break; case 2: CreateAG(pG); break; case 3: CreateAN(pG); break; default: printf("输入有误!\n"); return ERROR; } return OK; }
Status CreateGraph(ALGrapth *G){ int kind; printf("输入图种类\n"); scanf("%d", &kind); (*G).kind = (GraphKind)kind; switch((*G).kind){ case DG: return CreateDG(&*G); case UDG: return CreateUDG(&*G); default: return ERROR; } }
void CreateGraph(MGraph &G) { printf("请输入图G的类型(有向图:0 有向网:1 无向图:2 无向网:3):"); scanf("%d", &G.kind); switch (G.kind) { case DG: CreateDG(G); break; case DN: CreateDN(G); break; case UDG: CreateUDG(G); break; case UDN: CreateUDN(G); } }
/* * @description:创建图,包括有向图,无向图,有向网,无向网 */ Status CreateGraph(MGraph *G) { printf("please enter the kind of the graph(DG:0,DN:1,UDG:2,UDN:3):"); scanf("%d",&(*G).kind); switch((*G).kind) { case UDG: return CreateUDG(G); break; case DG: return CreateDG(G); case UDN: return CreateUDN(G); break; case DN: return CreateDN(G); break; default: return ERROR; } }
int main (void) { MGraph G; CreateDG (&G); int dist[MAX]; int path[MAX][MAX] = {0}; Dijkstra (G, 0, dist, path); return 0; int i, j; for (i = 0; i < G.vernum; i++) { if (i != 0) { printf ("%c", G.vertex[0]); for (j = 1; path[i][j]; j++) printf ("->%c", G.vertex[path[i][j]]); printf ("->%c\n", G.vertex[i]); printf ("distance: %d\n", dist[i]); } } }
void main() { int j,k,n; OLGraph g; VertexType v1,v2; CreateDG(g); Display(g); printf("修改顶点的值,请输入原值 新值: "); scanf("%s%s",v1,v2); PutVex(g,v1,v2); printf("插入新顶点,请输入顶点的值: "); scanf("%s",v1); InsertVex(g,v1); printf("插入与新顶点有关的弧,请输入弧数: "); scanf("%d",&n); for(k=0;k<n;k++) { printf("请输入另一顶点的值 另一顶点的方向(0:弧头 1:弧尾): "); scanf("%s%d",v2,&j); if(j) InsertArc(g,v2,v1); else InsertArc(g,v1,v2); } Display(g); printf("删除一条弧,请输入待删除弧的弧尾 弧头:"); scanf("%s%s",v1,v2); DeleteArc(g,v1,v2); Display(g); printf("删除顶点及相关的弧,请输入顶点的值: "); scanf("%s",v1); DeleteVex(g,v1); Display(g); printf("深度优先搜索的结果:\n"); DFSTraverse(g,visit); printf("广度优先搜索的结果:\n"); BFSTraverse(g,visit); DestroyGraph(g); }