Data structure (10)--adjacency table storage of graphs

Source: Internet
Author: User

////////////////////////////////////////////////////////////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-&GT;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-&GT;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.