Graphs are extensions of trees. This blog post mainly describes the storage of graphs.
Graph storage includes: the graph's Adjacent matrix, the graph's adjacent linked list, and the cross linked list.
1. the adjacent matrix of the graph is nothing more than a one-dimensional array and a two-dimensional array. A one-dimensional array is used to store vertex information of a graph. A two-dimensional array is used to store the link information of a graph. Previously, a sensor network project was created. Remember to use a two-dimensional array to store information between routes. Because the routing information is undirected, only half of the information was used at that time. This is very simple, skip it directly;
2. The connected list, array, and linked list of the graph. As follows:
Figure 1
The graph is divided into backward and undirected ones, for example, Fig 2 and Fig 3.
Figure 2
Figure 3
Figure 2 corresponds to figure 1. We know that to store figure 2, if we store it in Figure 1, there must be two types of data structures: static arrays and linked lists.
The subscript of the array indicates the node order, which stores the corresponding data and a pointer to the linked list.
Defined data structure:
Struct edgenode;/* define the struct * // define the data structure struct datanode {char saveddata; // int weight; the weight of node edgenode * Next; datanode () {saveddata = '\ 0'; next = NULL ;}}; struct edgenode {int sequence; // subscript for storing arrays, indicating the relationship between data edgenode * Next ;}; // number of nodes const int num = 7; datanode graphlist [num];
Create a chart:
// Create the figure void creategraph (datanode * graphl) {cout <"Enter seven data nodes:" <Endl; char inputchar; int nodeone; int nodetwo; for (INT I = 0; I <num; ++ I) {CIN> inputchar; graphl [I]. saveddata = inputchar;} // create an edge cout <"Enter the two nodes to be linked:" <Endl; while (1) {CIN> nodeone; cin> nodetwo; If (nodeone =-1 & nodetwo =-1) return; edgenode * newedgen1 = new edgenode (); newedgen1-> sequence = nodetwo; newedgen1-> next = graphl [nodeone]. next; graphl [nodeone]. next = newedgen1; edgenode * newedgen2 = new edgenode (); newedgen2-> sequence = nodeone; newedgen2-> next = graphl [nodetwo]. next; graphl [nodetwo]. next = newedgen2 ;}}
// Release the memory void deletemem (datanode * graphl) {edgenode * P = NULL; edgenode * temp = NULL; For (INT I = 0; I <num; ++ I) {P = graphlist [I]. next; while (p) {temp = P; P = p-> next; Delete temp ;}}}
Test:
Int main () {creategraph (graphlist); cout <"output chain table sequence:" <Endl; For (INT I = 0; I <num; ++ I) {edgenode * P = graphlist [I]. next; cout <I <":" <graphlist [I]. saveddata <"; while (p) {cout <p-> sequence <", "; P = p-> next ;} cout <"\ n" ;}return 0 ;}result: Enter the character as shown in figure 2. The entered characters are not v0, v1 ......
3. Cross-linked list of a graph: the outbound and inbound degrees of a directed graph can only be located at or into nodes. To locate the inbound and outbound nodes at the same time, it is very easy to combine them. I will not introduce it any more.