编程实现有向图图的邻接表存储,并计算给定结点的入度和出度。样例输入:(所有数据从键盘输入) 4 4 A B C D A B A D B C C A A 【样例输出】 This graph has 4 vertexs, and 4 edges. The information of vertexs are: A 0 B 1 C 2 D 3 The adjacent table of graph is: A:D->B B:C C:A D: The in-degree of A is 1 The out-degree of A is 2 【样例说明】 【评分标准】 #include<iostream> #include<string> #include<fstream> using namespace std; const int MAXL = 20; //结点信息 struct Node { string data;//结点值 int no;//结点编号 Node(string data):data(data),no(0){} Node() { data = ""; no = 0; } }; //边结点 struct EdgeNode { Node nextnode;//终止结点信息 EdgeNode* nextedge;//下一条邻接边 EdgeNode(string s) { this->nextnode.data = s; this->nextnode.no = 0; this->nextedge = NULL; } EdgeNode() { this->nextnode.data = ""; this->nextnode.no = 0; this->nextedge = NULL; } }; //表头结点 struct HNode { Node hnode;//起始结点信息 EdgeNode* firstedge;//第一条邻接边 HNode(string s) { hnode.data = s; hnode.no = 0; firstedge = NULL; } HNode() { hnode.data = ""; hnode.no = 0; firstedge = NULL; } }; //邻接表 struct ALGraph { int n, e;//分别表示顶点数和边数 HNode hnode[MAXL];//头结点数组 ALGraph() { n = e = 0; for (int i = 0; i < MAXL; i++) hnode[i].firstedge = NULL; } };
时间: 2024-02-10 09:14:35 浏览: 142
matlab实现矩阵乘法代码-Multi-view_Graph_Learning:论文“通过一致性和不一致的联合建模进行多视图图学习”的代码
好的,以下是实现有向图邻接表存储,并计算给定节点入度和出度的代码:
```c++
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
const int MAXL = 20; // 最大节点数
// 结点信息
struct Node {
string data; // 结点值
int no; // 结点编号
Node(string data) : data(data), no(0) {}
Node() { data = ""; no = 0; }
};
// 边结点
struct EdgeNode {
Node nextnode; // 终止结点信息
EdgeNode* nextedge; // 下一条邻接边
EdgeNode(string s) {
this->nextnode.data = s;
this->nextnode.no = 0;
this->nextedge = NULL;
}
EdgeNode() {
this->nextnode.data = "";
this->nextnode.no = 0;
this->nextedge = NULL;
}
};
// 表头结点
struct HNode {
Node hnode; // 起始结点信息
EdgeNode* firstedge; // 第一条邻接边
HNode(string s) {
hnode.data = s;
hnode.no = 0;
firstedge = NULL;
}
HNode() {
hnode.data = "";
hnode.no = 0;
firstedge = NULL;
}
};
// 邻接表
struct ALGraph {
int n, e; // 分别表示顶点数和边数
HNode hnode[MAXL]; // 头结点数组
ALGraph() {
n = e = 0;
for (int i = 0; i < MAXL; i++) {
hnode[i].firstedge = NULL;
}
}
};
// 获取节点编号
int get_node_no(ALGraph& G, string data) {
for (int i = 1; i <= G.n; i++) {
if (G.hnode[i].hnode.data == data) {
return i;
}
}
return 0;
}
// 添加一条从s到e的边
void add_edge(ALGraph& G, string s, string e) {
int u = get_node_no(G, s);
int v = get_node_no(G, e);
if (u == 0 || v == 0) {
return;
}
EdgeNode* p = new EdgeNode(e);
p->nextedge = G.hnode[u].firstedge;
G.hnode[u].firstedge = p;
}
int main() {
ALGraph G;
string s, e;
cin >> G.n >> G.e;
for (int i = 1; i <= G.n; i++) {
cin >> s;
G.hnode[i] = HNode(s);
G.hnode[i].hnode.no = i;
}
for (int i = 1; i <= G.e; i++) {
cin >> s >> e;
add_edge(G, s, e); // 添加一条从s到e的边
}
// 输出图的信息
cout << "This graph has " << G.n << " vertexs, and " << G.e << " edges." << endl;
cout << "The information of vertexs are: ";
for (int i = 1; i <= G.n; i++) {
cout << G.hnode[i].hnode.data << " " << G.hnode[i].hnode.no << " ";
}
cout << endl;
// 输出图的邻接表
cout << "The adjacent table of graph is: " << endl;
for (int i = 1; i <= G.n; i++) {
cout << G.hnode[i].hnode.data << ":";
EdgeNode* p = G.hnode[i].firstedge;
while (p != NULL) {
cout << p->nextnode.data << "->";
p = p->nextedge;
}
cout << endl;
}
// 计算给定节点的入度和出度
string node;
cin >> node;
int inDegree = 0, outDegree = 0;
int nodeNo = get_node_no(G, node);
if (nodeNo == 0) {
cout << "Node not found!" << endl;
}
else {
// 计算入度
for (int i = 1; i <= G.n; i++) {
EdgeNode* p = G.hnode[i].firstedge;
while (p != NULL) {
if (p->nextnode.data == node) {
inDegree++;
break;
}
p = p->nextedge;
}
}
// 计算出度
EdgeNode* p = G.hnode[nodeNo].firstedge;
while (p != NULL) {
outDegree++;
p = p->nextedge;
}
// 输出结果
cout << "The in-degree of " << node << " is " << inDegree << endl;
cout << "The out-degree of " << node << " is " << outDegree << endl;
}
return 0;
}
```
以上是实现有向图邻接表存储,并计算给定节点入度和出度的代码。
阅读全文