Create a cross linked list to find the inbound degree of each vertex in the directed graph and output the arc C ++ implementation related to it.

Source: Internet
Author: User

Example input:

A B c d // the name of each vertex, char type

(CTRL + Z) // indicates that the input is complete.

7 // Number of arcs in a Directed Graph

0 1 // This number pair represents an arc 0-> 1 from the vertex numbered 0 to the vertex numbered 1.

0 2 // numbers of a, B, c, and d are 0, 1, 2, and 3, respectively.

2 0

2 3

3 0

3 1

3 2

Sample output:

 

 

 


Directed Graph example

 

 


"Head. h"

 


# Include <iostream>
# Define MAX_VERTEX_NUM 20
Using namespace std;

Class ArcBox // arc
{
Public:
ArcBox ();
Int tailvex, headvex;
ArcBox * hlink, * tlink;
Char info;
};

ArcBox: ArcBox ()
{
Tailvex = headvex = 0;
Hlink = tlink = NULL;
}

Class VexNode // Vertex
{
Public:
VexNode ();
Char data;
ArcBox * firstin, * firstout;
};

VexNode: VexNode ()
{
Firstin = firstout = NULL;
}

Class OlGraphMessage // cross linked list
{
Public:
OlGraphMessage ();
VexNode xlist [MAX_VERTEX_NUM];
Int vexnum, arcnum;
};

OlGraphMessage: OlGraphMessage ()
{
Vexnum = arcnum = 0;
}

Class OLGRAPH
{
Public:
Void OlGraphConstructor (); // create a cross linked list
Private:
Void GetVertex (); // obtain vertex information.
Void GetArcNum (); // obtain the number of radians
Void CreatOl (); // creates a cross linked list based on vertex information.
Void OlPrinter (); // output vertex Information
OlGraphMessage ol;
};

Void OLGRAPH: OlGraphConstructor ()
{
Cout <"OlGraphConstructor Called! "<Endl;
GetVertex (); // obtain vertex information.
GetArcNum (); // obtain the number of radians
CreatOl (); // create a cross linked list based on vertex Information
OlPrinter (); // output vertex Information
}

Void OLGRAPH: GetVertex () // obtain vertex information.
{
Cout <"GetVertex Called! "<Endl;
Cout <"Please Enter The Vertex" <endl;
While (cin> ol. xlist [ol. vexnum]. data)
Ol. vexnum ++;
Cin. clear ();
}

Void OLGRAPH: GetArcNum () // obtain the number of radians
{
Cout <"GetArcNum Called! "<Endl;
Cout <"Please Enter The Number Of The Arc" <endl;
Cin> ol. arcnum;
}
Void OLGRAPH: CreatOl () // create a cross linked list based on vertex Information
{
Cout <"CreatOl Called! "<Endl;
Cout <"Please Input The ArcMessage:" <endl;
Int a, B;
ArcBox * insert, * p;
While (ol. arcnum --)
{
Cin> a> B;
Insert = new ArcBox;
Insert-> headvex =;
Insert-> tailvex = B;
P = ol. xlist [a]. firstout;
If (p = NULL)
{
Ol. xlist [a]. firstout = insert;
}
Else
{
While (p-> tlink! = NULL)
P = p-> tlink;
P-> tlink = insert;
}
P = ol. xlist [B]. firstin;
If (p = NULL)
{
Ol. xlist [B]. firstin = insert;
}
Else
{
While (p-> hlink! = NULL)
P = p-> hlink;
P-> hlink = insert;
}
}
Cout <"CreatOl Succeed! "<Endl;
}

Void OLGRAPH: OlPrinter () // output vertex Information
{
Cout <"OlPrinter Called! "<Endl;
ArcBox * p;
Int od, id;
For (int I = 0; I <ol. vexnum; I ++)
{
Cout <ol. xlist [I]. data <":" <endl; // output vertex
Od = id = 0; // initialization outbound and inbound
// Obtain the arc ending with this vertex.
P = ol. xlist [I]. firstout;
While (p! = NULL)
{
Cout <ol. xlist [p-> headvex]. data <"->" <ol. xlist [p-> tailvex]. data <endl;
P = p-> tlink;
Od ++;
}
Cout <"OutDegree =" <od <endl;
// Calculate the arc with this vertex as the arc Header
P = ol. xlist [I]. firstin;
While (p! = NULL)
{
Cout <ol. xlist [p-> tailvex]. data <"<-" <ol. xlist [p-> headvex]. data <endl;
P = p-> hlink;
Id ++;
}
Cout <"InDegree =" <id <endl;
}
}

 

 


"Main. cpp"

 

 

 

# Include "head. h"
Int main ()
{
OLGRAPH ol;
Ol. OlGraphConstructor ();
System ("pause ");
}

Author: "Dreamer Thinker Doer"
 

Related Article

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.