Undirected graph chain storage (CODE)

Source: Internet
Author: User

# Include <stdlib. h>
# Include <iostream. h>

# Define Zhi 20 // The maximum number of nodes that can be stored

Int diannum; // Number of vertices to store

// The pointer to the next vertex that stores vertex information and is related to the vertex
Struct Dian
{
Char data; // store vertex Information
Struct Bian * Next; // address of edge Storage
};

// Store the relationship between the vertex and the vertex
Struct Bian
{
Int data; // subscript that stores the vertex in a one-dimensional array
Struct Bian * Next; // address of edge Storage
};

// Initialization Diagram
Void chushihua (struct Dian * & Tu)
{
Int I;
Tu = (struct Dian *) malloc (Zhi * sizeof (struct Dian); // open up space for the Graph
 
For (I = 0; I <Zhi; I ++)
(Tu + I)-> next = NULL; // assign an initial value
}

// Input Graph Information
Void shuru (struct Dian * & Tu)
{
Int I;
Int K;
Int J; // storage flag
Struct Bian * P;

Cout <"Enter the total number of vertices in the graph :";
Cin> diannum;

// Vertex information input operation
Cout <"\ n ";
For (I = 0; I <diannum; I ++)
{
Cout <"Enter the vertex Information <I + 1 <:";
Cin> (Tu + I)-> data;
}
Cout <"vertex information input is complete! ";

// Vertex and vertex relationship Input
For (I = 0; I <diannum; I ++)
{
K = 1; // a flag
For cout <"\ n, enter the sequence numbers of all vertices that can be connected to vertex <I + 1 <! \ N ";
Cout <"Enter the number of vertex <k <" (0 indicates exit ):";
Cin> J;
While (J! = 0)
{
If (k = 1) // The operation of the response flag
{
(Tu + I)-> next = (struct Bian *) malloc (sizeof (struct Bian ));
P = (Tu + I)-> next;
P-> DATA = J;
P-> next = NULL;
}
Else
{
P-> next = (struct Bian *) malloc (sizeof (struct Bian ));
P = p-> next;
P-> DATA = J;
P-> next = NULL;
}

K = k + 1;
Cout <"Enter the number of vertex <k <" (0 indicates exit ):";
Cin> J;
}
}
Cout <"the link between the vertex and the vertex is input! \ N ";
}

// Output of Graph Information
Void shuchu (struct Dian * Tu)
{
Int I;
Struct Bian * P;
Cout <"output of Graph Information: \ n ";
// Central idea: Output all vertices directly connected to the first vertex from the first vertex.
For (I = 0; I <diannum; I ++)
{
Cout <"vertex" <(Tu + I)-> data <"is directly connected to those vertices :";

P = (Tu + I)-> next;
While (P! = NULL)
{
Cout <(Tu + I)-> data <"----" <(Tu + (p-> data-1)-> data <"";
P = p-> next;
}
Cout <"\ n ";
}
}

// Main Function
Void main ()
{
Struct Dian * Tu; // defines the graph header.
Int option; // operation for storing users
Chushihua (TU); // allocate the corresponding space to the graph header and assign the initial value to the corresponding item

Option = 0;
While (option! = 3)
{
Cout <"\ n Menu \ n ";
Cout <"1. Image Input \ n ";
Cout <"2. Figure output \ n ";
Cout <"3. Exit \ n ";
Cout <"Select Operation :";
Cin> option;
Switch (option)
{
Case 1:
Shuru (TU );
Break;
Case 2:
Shuchu (TU );
Break;
Case 3:
Break;
Default:
Break;
}
}

}


TIPS: Use of tags!

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.