Graph is an important and relatively complex data structure, which is very useful in practical programming. Adjacency table is one of the main representations of graphs, and is a kind of link table representation method.
#include <stdio.h>
#include <stdlib.h>
The maximum number of vertices for the #define Max 10//graph is 10
typedef struct NODE//Edge table junction (ARC)
{
int adjvex;//number of connected vertices
int weight;//Benquan
struct node *pnext;//points to the next edge table node
}edgenode;
typedef struct VERTEXNODE//Vertex table node
{
int adjvex;//vertex number
Edgenode *pfirst;//side Table head pointer
}vertexnode;
typedef vertexnode ADJ_LIST[MAX]; Table Header Array
adjacency table Structure of typedef struct ADJLIST_GRAPH//graphs
{
int cnt_edges; Number of sides
int cnt_nodes; Top points
Adjacency table Header of Adj_list adjlist;//graph
}adjlist_graph;
void Create_graph (adjlist_graph* graph)//create diagram in adjacency table mode
{
edgenode* pnewnode;//New Benzi node
int i,j,k;
printf ("Number of vertices and sides of the input graph: \ n");
scanf ("%d%d", &graph->cnt_nodes,&graph->cnt_edges);
printf ("The vertex of the graph has%d, the number of sides is%d \ n",
Graph->cnt_nodes,graph->cnt_edges);
for (i=0;i<graph->cnt_nodes;i++)//initialization graph adjacency table vertex
{
graph->adjlist[i].adjvex=i;
graph->adjlist[i].pfirst=null;
}
for (k=0;k<graph->cnt_edges;k++)//Loop input graph each edge
{//no-map scenario
printf ("Enter a new edge: \ n");
scanf ("%d%d", &i,&j);
Pnewnode= (edgenode*) malloc (sizeof (Edgenode));
pnewnode->adjvex=j;
pnewnode->weight=0;
pnewnode->pnext=graph->adjlist[i].pfirst;
graph->adjlist[i].pfirst=pnewnode;
Diagram-free case with symmetry
Pnewnode= (edgenode*) malloc (sizeof (Edgenode));
pnewnode->adjvex=i;
pnewnode->weight=0;
pnewnode->pnext=graph->adjlist[j].pfirst;
graph->adjlist[j].pfirst=pnewnode;
}
}
void Show_adjlist_graph (adjlist_graph* graph)//show vertices in the graph and all sides associated with them
{
int i;
Edgenode* Pnode;
for (i=0;i<graph->cnt_nodes;i++)
{
pnode=graph->adjlist[i].pfirst;
printf ("\ n vertex number%d:", Graph->adjlist[i].adjvex);
while (Pnode!=null)
{
printf ("side:%d<->%d", Graph->adjlist[i].adjvex,pnode->adjvex);
pnode=pnode->pnext;
}
printf ("\ n");
}
}
int main (void)
{
Adjlist_graph *pgraph;
Pgraph= (adjlist_graph*) malloc (sizeof (adjlist_graph));
Create_graph (pgraph);
Show_adjlist_graph (pgraph);
}
Establishment of graphs--adjacency table representation (C language +vc6.0 platform)