Data Structure --- using C ++ to implement cross-linked list storage and representation of Graphs

Source: Internet
Author: User

Data Structure --- using C ++ to implement cross-linked list storage and representation of Graphs

I have been busy with postgraduate review recently and haven't updated my blog for a long time. Today I am writing a piece of data structure storage.

 

 

// The cross-linked list storage representation of the directed graph // Yang Xin # include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
Using namespace std; # define MAX_VERTEX_NUM 20 # define OVERFLOW-2 # define OK 1 typedef int Status; typedef char VertexType [MAX_VERTEX_NUM]; typedef char InfoType; // arc (edge) the struct typedef struct ArcBox {int tailvex, headvex; // the tail of the arc and the position of the header vertex struct ArcBox * hlink, * tlink; // The Chain Domain InfoType * info of the arc with the same arc header and the same arc tail respectively; // pointer of the arc-related information} ArcBox; // The vertex struct typedef struct VexNode {VertexType data; ArcBox * firstin, * firstout; // point to the first input and output arc of the vertex respectively} VexNode; // directed graph struct typedef struct {VexNode xlist [MAX_VERTEX_NUM]; // header vector int vexnum, arcnum; // The number of current vertices and arcs of the directed graph} OLGraph; int LocateVex (OLGraph & G, VertexType u) {for (int I = 0; I <G. vexnum; ++ I) if (strcmp (G. xlist [I]. data, u) = 0) return I; return-1 ;}// construct directed graph G; Status CreateDG (OLGraph & G) {int I, j, k; printf (enter the number of vertices and the number of arcs in the directed graph :); scanf (% d, & G. vexnum, & G. arcnum); printf (enter % d vertex values, separated by spaces:, G. vexnum); for (I = 0; I
     
      
Tailvex = I; p-> headvex = j; p-> hlink = G. xlist [j]. firstin; p-> tlink = G. xlist [I]. firstout; p-> info = NULL; G. xlist [j]. firstin = G. xlist [I]. firstout = p; // complete insertion in the inner and outgoing arc chunks} getchar (); return OK;} void DisplayArc (OLGraph & G) {ArcBox * p; for (int I = 0; I <G. vexnum; ++ I) {p = G. xlist [I]. firstout; while (p) {printf (<% s, % s>, G. xlist [p-> tailvex]. data, G. xlist [p-> headvex]. data); p = p-> tlink;} printf ();} // vertex degree: Inbound + outbound int VexDegree (OLGraph & G, VertexType v) {int k = LocateVex (G, v); if (k <0) exit (OVERFLOW); int id = 0, od = 0; // inbound, outbound ArcBox * pin = G. xlist [k]. firstin; ArcBox * pout = G. xlist [k]. firstout; while (pin) // evaluate the inbound level {+ + id; pin = pin-> hlink;} while (pout) // evaluate the degree {++ od; pout = pout-> tlink;} return id + od; // vertex degree} int main () {int flag = 1; OLGraph G; CreateDG (G ); printf (====================================================== === ); printf (the arc of the directed graph is as follows: :); DisplayArc (G); VertexType v; printf (====================================================== === ); while (1) {if (flag = 0) break; printf (enter any vertex and output the vertex degree value :); scanf (% s, v ); getchar (); printf (the degree of vertex % s is: % d, v, VexDegree (G, v); printf (enter 0 to end with the Enter key ); cin> flag ;} printf (====================================================== === ); return 0 ;}
     
    
   
  
 

 

 

Result:


 

 

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.