////////////////////////////////////////////////////////////adjacency table storage for graphs//////////////////////////////////////////////////////////#include <iostream>#include<stdlib.h>using namespacestd;//adjacency table notation for graphs#defineMaxvertexnum 100enumGRAPHTYPE{DG, UG, DN, UN};//Forward Graph, non-direction graph, mesh graph, non-meshtypedefstructnode{intADJV;//adjacency Point Field structNode *next;//pointer field pointing to next adjacency point//to represent the weight information on the edge, you should add a data field weight}edgenode;//Edge Table NodetypedefCharVertextype;//vertices are represented by characterstypedefstructvnode{vertextype Vertex;//Vertex FieldsEdgenode *firstedge;//Edge table Header pointer}vertexnode;typedef Vertexnode Adjlist[maxvertexnum]; //adjlist is an adjacency table typetypedefstruct{adjlist adjlist;//adjacency Table intN, E;//number of vertices and number of edges enumGraphtype GType;//type of diagram}algraph;//Algraph is a graph type that is stored in adjacency table modevoidCreatealgraph (Algraph *G) { intI, j, K; Edgenode*Edge; G->gtype = DG;//a map of the directioncout <<"Please enter the number of vertices and the number of edges (input format: Top count, number of sides): \ n"<<Endl; CIN>> g->n >> g->e;//number of vertices and edges read incout <<"Please enter the vertex information (input format: vertex number <CR>): \ n"; for(i =0; I < g->n; i++) {//Create a table of vertices with n verticesCIN >> &g->adjlist[i]. Vertex;//read-in vertex informationG->adjlist[i]. Firstedge = NULL;//the edge header pointer of the vertex is set to null} cout<<"Please enter the information for the side (input format is:i,j<cr>): \ n"; for(k =0; K < g->e; k++)//Create a side table{cin>> I >>J; Edge=NewEdgenode; Edge->ADJV =J; Edge->next = g->Adjlist[i]. Firstedge; //Insert new Edge table node edge into Benzi head of Vertex VIG->adjlist[i]. Firstedge =Edge; //In the case of an <vj graph, a node is created to represent the edge of the vi> }}voidPrint (Algraph *G) { for(inti =0; I < g->n; i++) {Edgenode*p = g->Adjlist[i]. Firstedge; cout<< G->adjlist[i]. Vertex <<" "; while(P! =NULL) {cout<< P->ADJV <<" "; P= p->Next; } cout<<Endl; }}intMain () {algraph*g =Newalgraph; Createalgraph (G); Print (G); System ("Pause"); return 0;}
Data structure (10)--adjacency table storage of graphs