Node class Noded.h
No storage index Required
#pragma once#ifndef node_h#define node_h#include<iostream>using namespace std; class node{public: Node (char0); Char m_cdata; BOOL m_isvisited;}; #endif // ! Node_h
Node.cpp
Assign data to data member M_cdata, whether access is set to No
#include"Node.h"node::node (char data) { = data; false ;}
Methods that need to be implemented
Figure Class CMap.h
#pragmaOnce#ifndef Cmap_h#defineCmap_h#include"Node.h"#include<vector>classcmap{ Public: CMap (intcapacity); ~CMap (); BOOLAddNode (Node *pnode);//adding vertices (nodes) to the diagram voidResetnode ();//Reset vertices BOOLSetvaluetomatrixfordirectedgraph (intRowintColintval =1);//Setting the adjacency matrix for a map BOOLSetvaluetomatrixforundirectedgraph (intRowintColintval =1);//Setting the adjacency matrix for a no-map voidPrintmatrix ();//Print adjacency matrix voidDepthfirsttraverse (intNodeindex);//Depth-First traversal voidBreadthfirsttraverse (intNodeindex);//breadth-First traversal voidBreathfirsttraverseimpl (vector<int>Prevec);Private: BOOLGetvaluefrommatrix (intRowintColint&val);//acquiring weights from matrices voidBreathfirsttraverse (intNodeindex);//breadth-first traversal implementation functionPrivate: intm_icapacity;//the maximum number of vertices that can be accommodated in a graph intM_inodecount;//number of nodes (vertices) that have been addedNode *m_pnodearray;//used to store vertex arrays int*m_pmatrix;//used to store adjacency matrices};#endif // ! Cmap_h
constructor function:
Incoming graph capacity parameter to data member M_icapacity
Number of nodes that have been added M_inodecount to 0
Request memory for a vertex array
Matrix of Application M_icapacity*m_icapacity
Place the matrix element all at 0
Cmap::cmap (int capacity) { = capacity; 0 ; New node[m_icapacity]; New int [m_icapacity*m_icapacity]; for (int0; i < m_icapacity*m_icapacity; i++) { 0; }}
Destructors
Delete a vertex array pointer
Delete adjacency matrix pointer
cmap::~CMap () { delete []m_pnodearray; Delete []m_pmatrix;}
Add nodes
Determines whether the passed-in Pnode parameter is empty and returns an error if Pnode is empty
Pnode Data part M_cdata into an array of vertices indexed with the number of nodes already added
Added node number + +
return correct results
bool cmap::addnode (Node *pnode) { if (pnode = = NULL ) {return false; } = pnode->m_cdata; M_inodecount++ ; return true ;}
Reset Nodes
Set the m_isvisited of the node that has been added as not accessed
void Cmap::resetnode () { for (int0; i < m_inodecount; i++) { false ; }}
Setting the adjacency matrix for a map
Judging the legitimacy of the ranks
Returns an error if the row is less than 0 and the row is greater than or equal to the maximum capacity
Returns an error if the column is less than 0 and the column is greater than or equal to the maximum capacity
The figure is as follows:
The adjacency matrix is as follows:
To (A, b) that is (0,1), 0 rows 1 columns, 0*8+1=1.
Indexes that meet row*m_icapacity calculations
BOOLCmap::setvaluetomatrixfordirectedgraph (intRowintColintval) { if(row<0|| row>=m_icapacity) { return false; } if(Col <0|| Col >=m_icapacity) { return false; } M_pmatrix[row*m_icapacity + col] =Val; return true;}
Setting the adjacency matrix for a no-map
Logic Ibid.
Col*m_icapacity and Row*m_icapacity+col are axisymmetric with the main diagonal
BOOLCmap::setvaluetomatrixforundirectedgraph (intRowintColintval) { if(row<0|| Row >=m_icapacity) { return false; } if(Col <0|| Col >=m_icapacity) { return false; } M_pmatrix[row*m_icapacity + col] =Val; M_pmatrix[col*m_icapacity + row] =Val;}
An array of C + + implementations of a forward graph and a non-graph