Recently has been busy with postgraduate examination, a long time has not updated the blog, today write a data structure of the storage.
Cross-linked list storage representations for graphs//Xin Yang # include <iostream> #include <cstdio> #include <stdlib.h> #include <cstring >using namespace std; #define Max_vertex_num 20#define overflow-2#define OK 1typedef int status;typedef Char Vertextype [Max_vertex_num];typedef Char infotype;//arc (EDGE) of the structure of the typedef struct ARCBOX{INT Tailvex,headvex; The end of the arc and the position of the head point struct Arcbox *hlink, *tlink; The chain domain InfoType *info of arc with same arc head and arc tail respectively; The information about the arc of the pointer}arcbox;//vertex of the structure of the typedef struct VEXNODE{VERTEXTYPE data; Arcbox *firstin, *firstout; The first in and out arcs of the vertex are respectively pointed to the struct typedef struct {Vexnode Xlist[max_vertex_num] of the}vexnode;//graph; Table Head vector int vexnum, arcnum; The current vertex number and number of arcs of the}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;} Tectonic graph G; Status createdg (olgraph &g) {int i,j,k;printf ("Please enter the number of vertices of the graph and number of arcs: \ n"); scanf ("%d%d", &g.vexnum, &g.arcnum); printf ("Please enter a value of%d vertices separated by a space: \ n", G.vexnum), for (i=0; i<g.vexnum; ++i)//construct the header vector {getchar (); scanf ("%s", G.xlisT[i].data); Input vertex value g.xlist[i].firstin = NULL; G.xlist[i].firstout = NULL;} Vertextype V1,v2; Arcbox *p;printf ("Enter the two vertices (input format: v1 v2) \ n", G.arcnum), for (k=0; k < g.arcnum; ++k)//input each arc and construct a cross-linked list {getchar (); scanf ("%s%s", V1,v2), I=locatevex (G,V1), J=locatevex (g,v2);p = (Arcbox *) malloc (sizeof (Arcbox)), if (!p) exit (OVERFLOW) ;p->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; Finish inserting}getchar () in the Arc and arc chain head; 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 ("\ n");} Degree of vertex: in degrees + out int vexdegree (olgraph &g, Vertextype v) {int k = Locatevex (g,v), if (k<0) exit (OVERFLOW); int id=0,od=0; In degrees, out of arcbox *pin = G.xlist[k].firstin; Arcbox *pout = G.xlist[k].firstout;while (PIN)//seek in degree {++id;pin = Pin->hlink;} while (pout) Find the degree {++od;pout = Pout->tlink;} return id+od; The degree of the vertex}int main () {int flag = 1;olgraph G; CREATEDG (g);p rintf ("=========================================\n");p rintf ("The arc of the graph is as follows: \ n");D Isplayarc (g); Vertextype v;printf ("\n=========================================\n"), while (1) {if (flag = = 0) break;printf ("Please enter any vertex, The value of the vertex degree will be output: "), scanf ("%s ", V), GetChar ();p rintf (" vertex%s is:%d\n ", V,vexdegree (g,v));p rintf (" End Please enter 0 to end with enter "); CIN >>flag;} printf ("=========================================\n"); return 0;}
Results:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Data structure---The cross-linked list storage representation of C + + language implementation graphs