Adjacency tables are another efficient means of storing representations of graphs. Each vertex u establishes a single linked list, and each node in the list represents an edge <u, V>, and an edge node. Each single-linked list is equivalent to
The row of the adjacency matrix.
The Adjvex field indicates a adjacency point V of U, and Nxtarc points to the next edge node of U. If it is a net, increase the weight on the edge of a W-domain storage.
The constructor completes the dynamic space storage allocation for one-dimensional pointer array A, and assigns an initial value of NULL to each of its elements. The destructor first releases all the nodes in the Adjacency table and finally releases the one-dimensional
The space occupied by the pointer array A.
included function exist (): If you enter the U, V is invalid, the function returns FALSE. Otherwise, starting from the Edge node indicated by A[u], search for the edge node with Adjvex value V, representing the Edge <u, V>
Succeeds, returns True, otherwise returns false.
function Insert (): If the input u, V is invalid, the insertion fails and returns failure. Otherwise, starting at the edge indicated by A[u], search for edge nodes with a Adjvex value of V, if no such edge knot exists
Point, then create a representative Edge <u, the new Edge node of the v>, and inserts it in front of the single-linked list indicated by the pointer a[u], and e++. Otherwise, <u, v> is a repeating edge, return duplicate.
Remove () function: If the input u, V is invalid, the deletion fails and returns failure. Otherwise, starting from the edge indicated by A[u], search for edge nodes with a Adjvex value of V, and if such an edge exists, delete
In addition to the edge, E--, return Success. Otherwise, the <u is not saved, V> returns notpresent.
Implementation code:
#include "iostream" #include "Cstdio" #include "CString" #include "algorithm" #include "queue" #include "stack" #include " Cmath "#include" utility "#include" map "#include" set "#include" vector "#include" list "#include" string "using namespace Std;typedef Long Long ll;const int MOD = 1e9 + 7;const int INF = 0x3f3f3f3f;enum ResultCode {underflow, Overflow, Success , Duplicate, Notpresent, Failure};template <class t>struct enode{enode () {nxtarc = NULL;} Enode (int vertex, T weight, Enode *nxt) {Adjvex = Vertex;w = Weight;nxtarc = NXT;} int Adjvex; T W; Enode *nxtarc;/* data */};template <class t>class graph{public:virtual ~graph () {}virtual ResultCode Insert (int u, I NT V, T &w) = 0;virtual resultcode Remove (int u, int v) = 0;virtual bool Exist (int u, int v) const = 0;/* data */};tem Plate <class t>class lgraph:public graph<t>{public:lgraph (int msize); ~lgraph (); ResultCode Insert (int u, int v, T &w); ResultCode Remove (int u, int v), bool Exist (int u, int v) const;int VertiCES () const {return n;} void Output ();p rotected:enode<t> **a;int N, e;/* data */};template <class t>void lgraph<t>::output () { enode<t> *q;for (int i = 0; i < n; ++i) {q = a[i];while (q) {cout << ' (' << i << ' << Q- > Adjvex << "<< Q-w << ') '; q = q-Nxtarc;} cout << Endl;} cout << Endl << Endl;} Template <class t>lgraph<t>::lgraph (int msize) {n = msize;e = 0;a = new Enode<t>*[n];for (int i = 0; I < ; N ++i) A[i] = NULL;} Template <class t>lgraph<t>::~lgraph () {enode<t> *p, *q;for (int i = 0; i < n; ++i) {p = A[i];q = P;whi Le (p) {p = p, nxtarc;delete q;q = p;}} delete []a;} Template <class t>bool lgraph<t>::exist (int u, int v) const{if (U < 0 | | V < 0 | | u > n-1 | | v > n-1 | | U = = v) return false; enode<t> *p = a[u];while (P && p, Adjvex! = v) p = p, nxtarc;if (!P) return False;return true; Template <Class T>resultcode Lgraph<t>::insert (int u, int v, T &w) {if (U < 0 | | | V < 0 | | u > n-1 | | v > n -1 | | U = = v) return failure;if (Exist (U, v)) return Duplicate; enode<t> *p = new Enode<t> (V, W, A[u]); A[u] = P;e++;return Success;} Template <class t>resultcode lgraph<t>::remove (int u, int v) {if (U < 0 | | | V < 0 | | u > n-1 | | | v > n-1 | | U = = v) return Failure; Enode<t> *p = A[u], *q = Null;while (P && P-Adjvex = v) {q = P;p = P-Nxtarc;} if (!p) return notpresent;if (q) q, Nxtarc = P, nxtarc;else a[u] = p, nxtarc;delete p;e--;return Success;} int main (int argc, char const *argv[]) {lgraph<int> LG (4); int w = 4; Lg. Insert (1, 0, W); Lg. Output (); w = 5; Lg. Insert (1, 2, W); Lg. Output (); w = 3; Lg. Insert (2, 3, W); Lg. Output (); w = 1; Lg. Insert (3, 0, W); Lg. Output (); w = 1; Lg. Insert (3, 1, W); Lg. Output (); return 0;}
The adjacency table of graphs is implemented _lgraph