Implementation of graphs: adjacency table
When the number of sides in the graph is small, the graph structure is realized by adjacency table, which will waste a lot of memory space. Therefore, consider another way to implement the graph structure: the adjacency table. There are two main types of node structures in the adjacency table:
Vertex nodes
Edge Node
Look directly at the code
Class definition
#include <iostream> #include <iomanip> using namespace std; Maximum power value #define MAXWEIGHT 100//Edge node typedef struct EDGENODE_TAG {int adjvex; adjacency point int weight;
The weight of the edge of the struct Edgenode_tag *next;
}edgenode; Vertex node typedef struct VERTEX_TAG {int vertex;
Vertex Edgenode *next;
}vertex;
Class Graph {private://whether with the right bool isweighted;
Whether there is a isdirected to bool;
Vertex number int numv;
Number of sides int nume;
adjacency table Vertex *adjlist; Public:/* Construction Method NUMV is the number of vertices, whether isweighted with weights, whether isdirected has direction */Graph (int numv, bool isweighted = FALSE, bool isdirected
= False);
build void Creategraph ();
Destruction Method ~graph ();
Gets the number of vertices int getvernums () {return NUMV;}
Gets the number of edges int getedgenums () {return nume;}
Insert edge void Insertedge (int tail, int head, int weight = 1);
void Insertedge (int tail, int head, int weight);
Sets the weight of the specified edge void setedgeweight (int tail, int head, int weight);
Print adjacency table void Printadjacentlist ();
Check input bool Check (int tail, int head, int weight = 1); };
Class implementation
/* Construction Method NUMV is the number of vertices, whether isweighted with weights, whether isdirected has direction/graph::graph (int numv, BOOL isweighted, bool isdirected) {while (num V <= 0) {cout << "the number of vertices entered is incorrect.
, re-enter ";
Cin >> NUMV;
} THIS->NUMV = NUMV;
this->isweighted = isweighted;
this->isdirected = isdirected;
The number of sides is initialized to 0 nume = 0; Adjlist = new VERTEX[NUMV];
pointer array for (int i = 0; i < NUMV; i++) {Adjlist[i].vertex = i;
Adjlist[i].next = NULL;
}}//Build void Graph::creategraph () {//with a new variable to represent the number of sides, Nume's modification is left to Insertedge () int numedge = 0;
cout << "Number of input sides"; while (Cin >> Numedge && Numedge < 0) cout << "incorrect input.
, re-enter ";
int I, J, W;
if (!isweighted)//no permission graph {cout << "Enter start and end point for each edge: \ n";
for (int k = 0; k < Numedge; k++) {cin >> i >> J; while (!check (i, J)) {cout << "the side of the input is not correct.
Re-enter \ n ";
Cin >> I >> J;
} Insertedge (I, j);
}} else//permission graph {cout << "Enter the start, end, and weight of each edge: \ n"; for (int k = 0; k < nUmedge;
k++) {cin >> I >> J >> W; while (!check (i, J, W)) {cout << "the side of the input is not correct.
Re-enter \ n ";
Cin >> I >> J >> W;
} Insertedge (I, J, W);
}}}//destructor method Graph::~graph () {int i;
Edgenode *p, *q;
for (i = 0; i < NUMV; i++) {if (adjlist[i].next) {p = adjlist[i].next;
while (p) {q = p->next;
Delete p;
p = q;
}}} Delete[]adjlist; }//sets the weight of the specified edge void graph::setedgeweight (int tail, int head, int weight) {if (!isweighted)//no permission graph {while (!check (tail, Head) {cout << "the side of the input is not correct.
Re-enter \ n ";
Cin >> Tail >> head;
} insertedge (tail, head); } else//permission graph {while (!check (tail, head, weight)) {cout << "input side is not correct.
Re-enter \ n ";
Cin >> Tail >> head >> weight;
} insertedge (tail, head, weight);
}}//insert edge void Graph::insertedge (int vertex, int adjvex, int weight) {Insertedge (vertex, Adjvex, weight); if (!isdirected)//graph Insertedge (aDjvex, vertex, weight);
} void Graph::insertedge (int vertex, int adjvex, int weight) {Edgenode *p, *q, *r;
p = q = r = NULL;
if (adjlist[vertex].next)//not the first node {p = Adjlist[vertex].next;
Move p to the appropriate position while (P && (P->adjvex < Adjvex)) {q = p;
p = p->next;
if (P && (P->adjvex = = Adjvex))//Modify existing edge weights p->weight = weight;
else {r = new Edgenode;
R->adjvex = Adjvex;
R->weight = weight;
R->next = p;
When a new node is added to the first position in the table if (adjlist[vertex].next = = p) Adjlist[vertex].next = R;
else Q->next = R;
nume++;
}} else {p = new Edgenode;
P->adjvex = Adjvex;
P->weight = weight;
P->next = NULL;
Adjlist[vertex].next = p;
nume++; }}//input check bool Graph::check (int tail, int head, int weight) {if (tail >= 0 && Tail < NUMV && Head
>= 0 && Head < numv && weight > 0 && weight <= maxweight) return true; else RetuRN false;
}//Print adjacency table void Graph::p rintadjacentlist () {int i;
Edgenode *edge = NULL;
for (i = 0; i < NUMV; i++) {edge = Adjlist[i].next; if (edge)//Why add an if judgment. It is related to the subsequent line break. If a vertex has no adjacency point (infinity), it will empty a row {while (edge) {cout << W (<< i <<, "<< Edge->adjvex <& Lt
") =" << edge->weight << "";
Edge = edge->next;
} cout << Endl; }
}
}
Main function
int main (void) {cout << "****** uses adjacency table to implement diagram structure ***by david***" << Endl;
BOOL isdirected, isweighted;
int NUMV;
cout << "Build map" << Endl;
cout << "Number of input vertices";
Cin >> NUMV;
cout << "side with weight, 0 (without) or 1 (with)";
Cin >> isweighted;
cout << "Is there a direction graph, 0 (no direction) or 1 (with direction)";
Cin >> isdirected;
Graph graph (NUMV, isweighted, isdirected);
cout << "This is a"; Isdirected?
cout << "direction,": cout << "non-direction"; Isweighted?
cout << "Rights graph" << endl:cout << "no power map" << Endl;
Graph.creategraph ();
cout << "Print adjacency table" << Endl;
Graph.printadjacentlist ();
cout << Endl;
int tail, head, weight;
cout << "Modifying weights for specified edges" << Endl;
if (isweighted)//For the start, end point, and weight of the input edge for the right graph {cout <<];
Cin >> Tail >> head >> weight;
Graph.setedgeweight (tail, head, weight);
} else//For the permission graph {cout << "input edge start, end point";
Cin >> Tail >> head;
Graph.setedgeweight (tail, head, 1); }
cout << "modified successfully.
"<< Endl;
cout << "Print adjacency matrix" << Endl;
Graph.printadjacentlist ();
System ("pause");
return 0; }
Run
adjacency table
Full code Download: Graph implementation: adjacency table
Reprint please indicate the source, this article address: http://blog.csdn.net/zhangxiangdavaid/article/details/38323593
If it helps, the top one OH.
Column directory:
Data structure and algorithm directory C pointer