For example, to store a forward graph:
When entering 6 vertices, 8 arcs
1 2
1 3
2 4
3 4
3 5
4 1
4 6
5 6
The flowchart for establishing the adjacency table is:
Implementation code:
/* Map the graph of n vertex m arcs with adjacency table */#include <stdio.h> #include <stdlib.h> #define MAX 10005typedef struct arcnode{int Adjvex; struct Arcnode * NEXTARC;} arcnode;typedef struct vnode{int vertex; Arcnode * FIRSTARC;} Vnode;int n,m; Vnode v[max];void init () {Arcnode * p; for (int i=1;i<=n;i++) {v[i].vertex=i; V[i].firstarc=null; } for (int i=0;i<m;i++) {int A, B; scanf ("%d%d", &a,&b); p= (arcnode*) malloc (sizeof (Arcnode)); p->adjvex=b; p->nextarc=v[a].firstarc; V[a].firstarc=p; }}int Main () {while (scanf ("%d%d", &n,&m)!=eof&& (n| | m)) {init (); Arcnode * p; Arcnode * q; for (int i=1;i<=n;i++) {printf ("%d", V[i].vertex); P=v[i].firstarc; while (P!=null) {printf ("%d", P->adjvex); Q=p; p=p->nextarc; Free (q); } printf ("\ n"); }} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
A direction graph for storing n vertex m arcs with adjacency table