First, briefly list the data structures needed for the implementation process.
Such as
For this diagram, its adjacency table can be expressed in this way, of course, the form can be varied, this is just a random representation of me.
Vertex table Edge table
We refer to the first table, the table labeled Fixedvex, as the vertex table, followed by the edge table.
As shown, the structure of the edge table should be written like this:
Contains a Adjvexthat holds a node;
One next points to the next side table structure, which forms the connection between each vertex.
Now that you have the structure of the edge table, the structure of the vertex table should look like this:
Vertex table typedef struct fixed{ int Mark; int Fixedvex; node* Firsthead;} Fixednode;
Contains a Fixedvex save vertex;
As we know from the graph, the vertex table points to the edge table, so it takes a pointer to the edge table for granted: a firsthead;
With these 2 structures, we can begin to implement it.
The first is to have a few nodes, that is, to have a few Fixedvex , because each Fixedvex has its own specific relationship, so we should generate an array of structures to save the content, so we set up the following structure to save, why create this structure? The following will be clear.
Save individual vertex information. typedef struct algarph{ int a , b; Fixednode adjlist[100];} algarph;
Now that you want to create an adjacency table, write a function.
void creat (Algarph *);
The first thing you have to do is pass in the function, then we create a structure and pass in
Algarph *s = (algarph*) malloc (sizeof (ALGARPH));
Creat (S);
The creat is exactly how to write, the following direct code, the reader through the comments themselves understand.
void Creat (Algarph *S) {intI,x,y; printf ("Please enter the number of vertices and sides:"); scanf ("%d%d", &s->a,&s->b); Gets the information for the vertex and edge for (i=0;i<s->a;i++) {printf ("input vertex, build vertex table:"); Create a vertex table. scanf ("%d",&s->Adjlist[i].fixedvex); S->adjlist[i].firsthead= NULL; Initialize Firsthead S->adjlist[i]. Mark = 0;//Initialize tag } printf ("Create edge table \ n");//create side tables that correspond to their relationships for (i=0;i<s->b;i++) {printf ("read in (VI-VJ)") at the same time they are established ; scanf ("%d%d",&x,&y); Verandedge * G = (Verandedge *) malloc (sizeof (Verandedge)); Generate a side table structure//G->mark = 0; A mark of zero means no access to g->adjvex= y; g->next= s->adjlist[x].firsthead; Insert into the head s->adjlist[x].firsthead= G;//Because it is a no-map, so the relationship of the Exchange node, generated again. g= (Verandedge *) malloc (sizeof(Verandedge));//G->mark = 0; g->adjvex= x; g->next= s->adjlist[y].firsthead; Insert to head s->adjlist[y].firsthead= G;}}
Once created, the adjacency table is output.
Verandedge * current; for (i=0;i<s->a;i++) { printf ("%d->",s->Adjlist[i].fixedvex); Current = S->adjlist[i].firsthead; while (current!=NULL) { printf ("%d->",current->Adjvex); current= current->Next;} printf ("\ n");}
OK, so the complete program is as follows:
/*2014/12/1*//* adjacency table represents the depth-first traversal of the graph *//* This code was rewritten on the basis of yesterday, adding 2 functions of DFS Traversal, comments really feel that there is no need to add. It's a recursive approach anyway.#include <stdio.h>#include <stdlib.h>//define the structure of a side table node typedef structnode{intAdjvex; int Mark; Used to mark if a node has been visited *Next;} Verandedge; Vertex table typedef structfixed{intMark; IntFixedvex; node*Firsthead;} fixednode;//Save individual vertex information. typedef structalgarph{intb; Fixednode adjlist[100];} Algarph;void creat (Algarph *); void Adj_dfs (Algarph *); void Adj_visit (ALGARPH *, int); int main (void) {intI Algarph * S = (Algarph *) malloc (sizeof(Algarph)); Creat (S); Verandedge *Current for (i=0;i<s->a;i++) {printf ("%d->",s->Adjlist[i].fixedvex); Current = S->Adjlist[i].firsthead; while (current!=NULL) {printf ("%d->",current->Adjvex); Current= current->Next } printf ("\ n"); } adj_dfs (S); return 0;} void Creat (Algarph *S) {intI,x,y; printf ("Please enter the number of vertices and sides:"); scanf ("%d%d", &s->a,&s->b); Gets information to vertices and edges for (i=0;i<s->a;i++{printf ("input vertex, create vertex table:"); Create a vertex table. scanf ("%d",&s->Adjlist[i].fixedvex); S->adjlist[i].firsthead= NULL; Initialize Firsthead S->adjlist[i]. Mark = 0;//Initialization token} printf ("Create a side table \ n");//create side tables that correspond to their relationships for (i=0;i<s->b;i++{printf ("read-In (VI-VJ):")); scanf ("%d%d",&x,&y); Verandedge * G = (Verandedge *) malloc (sizeof (Verandedge)); Generate a side table structure//G->mark = 0; Mark as zero means no access to g->adjvex=y; g->next= s->adjlist[x].firsthead; Insert into the head s->adjlist[x].firsthead= G;//Because it is a no-map, so the relationship of the Exchange node, generated again. g= (Verandedge *) malloc (sizeof(Verandedge));//G->mark = 0; g->adjvex= x; g->next= s->adjlist[y].firsthead; Insert to head s->adjlist[y].firsthead= G;}} void Adj_dfs (Algarph *S) {int vi; printf ("Enter source point: \ n"), scanf ("%d",&vi); printf ("The result of the traversal is as follows:"); Adj_visit (S,VI);} void Adj_visit (Algarph * S,int VI) {if (S->adjlist[vi]. Mark = = 0) {printf ("%-2d",s->Adjlist[vi].fixedvex); S->ADJLIST[VI]. Mark = 1;} Verandedge * current = s->adjlist[vi].firsthead, for (;current!=null;current=current->next) {if (S- >adjlist[current->adjvex]. Mark==0) adj_visit (s,current->Adjvex);}}
The operation results are as follows
adjacency Table of graphs stores C implementations (DFS traversal)