有向图的十字链表
我们都知道,邻接表只能知道某个点的出度是多少,逆邻接表只能知道某个顶点的入度是多少,因此产生了十字链表代替邻接表,可以直接算出一个顶点的度。
基本思想:
在顶点表中加入一个指针指向入度边,在边表中加入指向入度边的指针域和弧尾顶点的下标。在构造十字链表的时候,弧头顶点的入度指针域指向边表的对应的结点。同样也使用头插法。
基本准备:
#include <stdio.h>
#include <stdlib.h>
#define MAXVEX 30
typedef char VexType;
typedef struct arcList {
int tailvex;
int headvex;
struct arcList *headlink, *taillink;
}arcList;
typedef struct vexList {
VexType vex;
arcList *firststin, *firstout;
}vexList, vexarr[MAXVEX];
typedef struct AdList {
int arcsnum, vexsnum;
vexarr vexs;
}AdList;
核心算法:
int Create(AdList *G) {
printf("\n请输入要加入的边数和顶点数:");
scanf("%d %d", &G->arcsnum, &G->vexsnum);
getchar