源代码:
//
// graph.c
// 程序目的:有向图的十字链表存储
// Created by zhen7216 on 2016/12/11.
// Copyright © 2016年 chenzhen. All rights reserved.
//
#include <stdio.h>
#define MaxNum 50 //图的最大顶点数为50
typedef struct arcnode { //定义弧结点类型,假设不考虑权值
int tailvex, headvex; //弧尾顶点和弧头顶点的位置
struct arcnode *hlink, *tlink; //指向相同弧头和弧尾的其他弧
} arcNode;
typedef struct vexnode { //定义顶点结点类型
char vertex[3];
arcNode *firstin, *firstout; //分别指向该顶点第一条入弧和出弧
} vexNode;
typedef struct { //定义十字链表存储的图类型
vexNode vlist[MaxNum];
int vexnum, arcnum;
} olGraph;
int Locate(olGraph *G, char vex[]) { //确定顶点在图中的位置
int i;
for(i = 0; i < G->vexnum; i++)
if(strcmp(G->vlist[i].vertex, vex) == 0)
return i;
return 0;
}
void createOlg(olGraph *G) { //采用十字链表,构造有向图G
int i, j, k;
char vex1[3], vex2[3];
arcNode *s;
printf("\n输入有向图的顶点数和弧数(用逗号隔开)