Data Structure-exercise 9 graph storage-connected list

Source: Internet
Author: User

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 the figure: both the outbound and inbound cross-linked lists of the directed graph can only locate the nodes or inbound nodes. It is very easy to locate the nodes at the same time, combine the two. I will not introduce it any more.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.