This example for you to share the C + + implementation of directed graph adjacency table construction code, for your reference, the specific content is as follows
Data structure inside a basic problem, share their own writing, verification can run.
#include<iostream>
#include<string>
const int MAX = 20;
using namespace std;
Struct arcnode {// arc node
Int adjvex = - 1; // vertex position
Arcnode * nextarc = nullptr; // next pointer
size_ T info = 0; // arc information
};
Struct vnode {// vertex
string data = "0";
Arcnode * firstarc = nullptr; // pointer to the first arc attached to the vertex
};
Structure graph {// graph structure
Vnode vertices [Max]; // all vertices
Int vexnum, arcnum; // number of vertices and arcs
Graph(int m, int n) :vexnum(m), arcnum(n) {};
Graph() :vexnum(0), arcnum(0) {};
};
int main()
{
int vnum, anum, tempanum = 0;
Cout < "input vertex number":;
cin >> vnum;
Cout < "input arc number":;
cin >> anum;
cout << "\n\n";
Graph G(vnum, anum);
for (int i = 0; i != vnum; ++i) {
Cout < "input node" < I < "information:;
cin >> G.vertices[i].data;
if (tempanum != anum)
Cout < "enter the information of the arc depending on this node (enter - 1 to stop): \ n"; ";
else
Cout < "all arc information has been entered! \n";
bool first = true;
ArcNode *p, *temp;
for (int j = 0; (j != anum) && (tempanum != vnum); ++j) {
int pointto;
Cout < < input arc < < tempanum < < vertex position pointed to:;
cin >> pointto;
if (pointto == -1) break;
else {
++tempanum;
if (first == true) {
first = false;
G.vertices[i].firstarc = new ArcNode;
G.vertices[i].firstarc->adjvex = pointto;
p = G.vertices[i].firstarc;
}
else {
temp = new ArcNode;
temp->adjvex = pointto;
p->nextarc = temp;
p = temp;
}
}
}
cout << endl;
}
for (int i = 0; i != anum; ++i) {
Cout < "vertex" < I < ": | < g.vertices [i]. Data <" | ";
if (G.vertices[i].firstarc) {
cout << " -> " << G.vertices[i].firstarc->adjvex;
auto pt = G.vertices[i].firstarc->nextarc;
while (pt) {
cout << " -> " << pt->adjvex;
pt = pt->nextarc;
}
cout << "-> ^";
}
else
cout << " -> ^";
cout << endl;
}
return 0;
}
Because it only constructs the basic adjacency table of the directed graph with no right values, the arc information in the arc structure is not utilized.
The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.