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:
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.
struct algarph{ int A, b; Fixednode adjlist[];} 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.
voidcreat (Algarph *0) { intI,x,y; printf ("Please enter the number of vertices and the amount of edges:"); scanf ("%d%d", &s->a,&s->b);//getting information to vertices and edges for(i=0; i<s->a;i++) {printf ("enter vertices to create a table of vertices:");//Create a vertex table. scanf"%d",&s->Adjlist[i].fixedvex); S->adjlist[i].firsthead= NULL;//Initialize FirstheadS->adjlist[i]. Mark =0;//Initialize Tags} printf ("Create a side table \ n");//Create side tables that correspond to their relationships as they are built for(i=0; i<s->b;i++) {printf ("read IN (VI-VJ):"); scanf ("%d%d",&x,&y); Verandedge* G = (Verandedge *) malloc (sizeof(Verandedge));//Creating a side table structure//g->mark = 0;//mark as zero indicates no accessg->adjvex=y; G->next= s->adjlist[x].firsthead;//Insert to headS->adjlist[x].firsthead=G; //because it is a graph, the relationship of the node is changed, and then it is generated again. g= (Verandedge *) malloc (sizeof(Verandedge)); //g->mark = 0;g->adjvex=x; G->next= s->adjlist[y].firsthead;//Insert to headS->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*//*Depth-first traversal of graphs represented by adjacency tables*//*This code was rewritten on the basis of yesterday, adding the DFS traversal of the 2 functions, 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 nodetypedefstructnode{intAdjvex; //int Mark;//used to mark whether it has been accessedNode *Next;} Verandedge; //Vertex Tabletypedefstructfixed{intMark; intFixedvex; Node*Firsthead;} Fixednode;//Save individual vertex information. typedefstructalgarph{intb; Fixednode adjlist[ -];} Algarph;voidcreat (Algarph *);voidAdj_dfs (Algarph *);voidAdj_visit (Algarph *,int); intMainvoid){ 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;} voidcreat (Algarph *S) { intI,x,y; printf ("Please enter the number of vertices and the amount of edges:"); scanf ("%d%d", &s->a,&s->b);//getting information to vertices and edges for(i=0; i<s->a;i++) {printf ("enter vertices to create a table of vertices:");//Create a vertex table. scanf"%d",&s->Adjlist[i].fixedvex); S->adjlist[i].firsthead= NULL;//Initialize FirstheadS->adjlist[i]. Mark =0;//Initialize Tags} printf ("Create a side table \ n");//Create side tables that correspond to their relationships as they are built for(i=0; i<s->b;i++) {printf ("read IN (VI-VJ):"); scanf ("%d%d",&x,&y); Verandedge* G = (Verandedge *) malloc (sizeof(Verandedge));//Creating a side table structure//g->mark = 0;//mark as zero indicates no accessg->adjvex=y; G->next= s->adjlist[x].firsthead;//Insert to headS->adjlist[x].firsthead=G; //because it is a graph, the relationship of the node is changed, and then it is generated again. g= (Verandedge *) malloc (sizeof(Verandedge)); //g->mark = 0;g->adjvex=x; G->next= s->adjlist[y].firsthead;//Insert to headS->adjlist[y].firsthead=G; }}voidAdj_dfs (Algarph *S) { intVI; printf ("Please enter the source point: \ n"); scanf ("%d",&VI); printf ("The traversal results are as follows:"); Adj_visit (S,VI);}voidAdj_visit (Algarph * S,intVI) { 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)