Adjacent table-creating undirected graphs, undirected networks, directed graphs, and directed Networks

Source: Internet
Author: User

# Include <stdio. h>
# Include <stdlib. h>
# Define max_vertex_num 20
# Define OK 1
# Define error 0

Typedef int infotype;/* pointer type of the arc-related information */
Typedef char vertextype;/* vertex type */

Typedef struct arcnode/* structure of the arc node */
{
Int adjvex;/* position of the vertex pointed to by the arc */
Struct arcnode * nextarc;/* pointer to the next arc */
Infotype * Info;/* pointer to the arc-related information (think it should be a record of some remarks), such as the weight */
} Arcnode;

Typedef struct vnode/* vertex node Structure */
{
Vertextype data;/* vertex information */
Arcnode * firstarc;/* pointer to the first arc attached to the vertex */
} Vnode, adjlist [max_vertex_num];

Typedef struct/* structure definition of the graph's adjacent table */
{
Adjlist vertices;/* array for storing vertices */
Int vexnum, arcnum;/* Number of vertices and arcs in the graph */
Int kind;/* chart type flag */
} Algraph;

Int createudg (algraph & G)/* establish an undirected graph in the adjacent table */
{
Int I, S, D;
Arcnode * P, * q;
Scanf ("% d", & G. vexnum, & G. arcnum);/* Number of input nodes and edges */
Getchar ();
For (I = 1; I <= G. vexnum; I ++)/* input vertex */
{
Scanf ("% C", & G. vertices [I]. data);/* input vertex */
Getchar ();
G. vertices [I]. firstarc = NULL;/* initialize to null first */
}

For (I = 1; I <= G. arcnum; I ++)
{
Scanf ("% d", & S, & D);/* enter the start and end numbers of an edge attachment */
Getchar ();
P = (struct arcnode *) malloc (sizeof (struct arcnode ));
Q = (struct arcnode *) malloc (sizeof (struct arcnode ));

P-> adjvex = D;/* Save the vertex position pointed to by the arc */
Q-> adjvex = s;/* Save the vertex position pointed to by the arc */

P-> nextarc = G. vertices [s]. firstarc;
G. vertices [s]. firstarc = P;

Q-> nextarc = G. vertices [D]. firstarc;
G. vertices [D]. firstarc = Q;
}
Return OK;
}

Int createudn (algraph & G)/* establish an undirected network in the adjacent table */
{
Int I, S, D, W;
Arcnode * P, * q;
Scanf ("% d", & G. vexnum, & G. arcnum);/* Number of input nodes and edges */
Getchar ();
For (I = 1; I <= G. vexnum; I ++)/* input vertex */
{
Scanf ("% C", & G. vertices [I]. data);/* input vertex */
Getchar ();
G. vertices [I]. firstarc = NULL;/* initialize to null first */
}

For (I = 1; I <= G. arcnum; I ++)
{
Scanf ("% d", & S, & D, & W);/* enter the start and end numbers of an edge attachment */
Getchar ();
P = (struct arcnode *) malloc (sizeof (struct arcnode ));
Q = (struct arcnode *) malloc (sizeof (struct arcnode ));

P-> info = (infotype *) malloc (sizeof (infotype ));
Q-> info = (infotype *) malloc (sizeof (infotype ));

P-> adjvex = D;/* Save the vertex position pointed to by the arc */
Q-> adjvex = s;/* Save the vertex position pointed to by the arc */

* (P-> info) = W;/* Save the weight to a node */
* (Q-> info) = W;/* Save the weight to a node */

P-> nextarc = G. vertices [s]. firstarc;
G. vertices [s]. firstarc = P;


Q-> nextarc = G. vertices [D]. firstarc;
G. vertices [D]. firstarc = Q;
}
Return OK;
}

Int createdg (algraph & G)/* establish a directed graph in the adjacent table */
{
Int I, S, D;
Arcnode * P;
Scanf ("% d", & G. vexnum, & G. arcnum);/* Number of input nodes and edges */
Getchar ();
For (I = 1; I <= G. vexnum; I ++)/* input vertex */
{
Scanf ("% C", & G. vertices [I]. data);/* input vertex */
Getchar ();
G. vertices [I]. firstarc = NULL;/* initialize to null first */
}

For (I = 1; I <= G. arcnum; I ++)
{
Scanf ("% d", & S, & D);/* enter the start and end numbers of an edge attachment */
Getchar ();
P = (struct arcnode *) malloc (sizeof (struct arcnode ));

P-> adjvex = D;/* Save the end point pointed to by the arc */

/* The two statements are equivalent to the insert operation of a single-chain table */
P-> nextarc = G. vertices [s]. firstarc;/* Save the end point of the vertex */
G. vertices [s]. firstarc = P;
}
Return OK;
}

Int createdn (algraph & G)/* establish a Directed Network for the adjacent table */
{
Int I, S, D, W;
Arcnode * P;
Scanf ("% d", & G. vexnum, & G. arcnum);/* Number of input nodes and edges */
Getchar ();
For (I = 1; I <= G. vexnum; I ++)/* input vertex */
{
Scanf ("% C", & G. vertices [I]. data);/* input vertex */
Getchar ();
G. vertices [I]. firstarc = NULL;/* initialize to null first */
}

For (I = 1; I <= G. arcnum; I ++)
{
Scanf ("% d", & S, & D, & W);/* enter the start and end numbers of an edge attachment */
Getchar ();
P = (struct arcnode *) malloc (sizeof (struct arcnode ));

P-> adjvex = D;/* Save the end point pointed to by the arc */

P-> info = (infotype *) malloc (sizeof (infotype ));
* (P-> info) = W;

/* The two statements are equivalent to the insert operation of a single-chain table */
P-> nextarc = G. vertices [s]. firstarc;/* Save the end point of the vertex */
G. vertices [s]. firstarc = P;
}
Return OK;
}

Int printalgraphudg (algraph * G)/* print the single-chain table of each node in an undirected graph */
{
Int I;
Printf ("Print undirected graph/N ");
For (I = 1; I <= G-> vexnum; I ++)
{
Printf ("% C:", G-> vertices [I]. data );
While (G-> vertices [I]. firstarc-> nextarc! = NULL)
{
Printf ("% d", G-> vertices [I]. firstarc-> adjvex );
G-> vertices [I]. firstarc = G-> vertices [I]. firstarc-> nextarc;
}
Printf ("% d/N", G-> vertices [I]. firstarc-> adjvex );
}
Return OK;
}

Int printalgraphudn (algraph * G)/* print a single-chain table for each node of the undirected network */
{
Int I, J;
Printf ("Print undirected network/N ");
For (I = 1; I <= G-> vexnum; I ++)
{
While (G-> vertices [I]. firstarc-> nextarc)
{
Printf ("% C -->", G-> vertices [I]. data );
J = G-> vertices [I]. firstarc-> adjvex;
Printf ("% C", G-> vertices [J]. data );

Printf ("/tweight: % d/N", * (G-> vertices [I]. firstarc-> info ));
G-> vertices [I]. firstarc = G-> vertices [I]. firstarc-> nextarc;
}
Printf ("% C -->", G-> vertices [I]. data );
J = G-> vertices [I]. firstarc-> adjvex;
Printf ("% C", G-> vertices [J]. data );
Printf ("/tweight: % d/N", * (G-> vertices [I]. firstarc-> info ));
Printf ("------------------------------------------/N ");
}
Return OK;
}

Int printalgraphdg (algraph * G)/* print the single-chain table of each node of the directed graph */
{
Int I;
Printf ("Print directed graph/N ");
For (I = 1; I <= G-> vexnum; I ++)
{
Printf ("% C:", G-> vertices [I]. data );
If (G-> vertices [I]. firstarc = NULL)
{
Printf ("/N ");
Continue;
}
While (G-> vertices [I]. firstarc)
{
Printf ("% d", G-> vertices [I]. firstarc-> adjvex );
G-> vertices [I]. firstarc = G-> vertices [I]. firstarc-> nextarc;
}
Printf ("/N ");
}
Return OK;
}

Int printalgraphdn (algraph * G)/* print a single-chain table directed to each node of the Network */
{
Int I, J;
Printf ("Print Directed Network/N ");
For (I = 1; I <= G-> vexnum; I ++)
{
While (G-> vertices [I]. firstarc)
{
Printf ("% C -->", G-> vertices [I]. data );
J = G-> vertices [I]. firstarc-> adjvex;/* get the end point */
Printf ("% C", G-> vertices [J]. data );
Printf ("/tweight = % d/N", * (G-> vertices [I]. firstarc-> info ));
G-> vertices [I]. firstarc = G-> vertices [I]. firstarc-> nextarc;
}
}
Return OK;
}

Void main ()
{
Algraph g;
// Createudg (g);/* Create an undirected graph */
// Printalgraphudg (& G);/* print an undirected graph */
// Createdg (g);/* Create a directed graph */
// Printalgraphdg (& G);/* print directed graph */

// Createudn (g);/* Create a undirected network */
// Printalgraphudn (& G);/* print the undirected network */
Createdn (g);/* Create a Directed Network */
Printalgraphdn (& G);/* print Directed Network */
}

 

Figure 5 4 a B C D E 1 2 1 3 1 5 4 5

 

Network Test Data: 5 4 a B C D E 1 2 10 1 3 15 1 5 20 4 5 30

 

Tip: paste it to the console and press enter to run it!

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.