1、实现图的邻接矩阵和邻接表的存储。 (1)建立图1的邻接表存储结构并输出; (2)建立图1的邻接矩阵存储结构并输出。 图1c++
时间: 2024-03-01 18:52:14 浏览: 90
以下是实现图的邻接矩阵和邻接表的C++代码:
```c++
#include <iostream>
#include <vector>
using namespace std;
// 邻接表存储结构
struct EdgeNode {
int adjvex; // 邻接点的索引
EdgeNode* next; // 指向下一个邻接点的指针
};
struct VertexNode {
char data; // 顶点的数据
EdgeNode* first; // 指向第一个邻接点的指针
};
vector<VertexNode> createGraphByAdjList() {
vector<VertexNode> graph;
int n = 6; // 图的顶点数
char vertexData[] = {'A', 'B', 'C', 'D', 'E', 'F'};
int edges[][2] = {{0, 1}, {0, 2}, {1, 2}, {1, 3}, {2, 4}, {3, 4}, {3, 5}, {4, 5}};
// 初始化顶点
for (int i = 0; i < n; i++) {
VertexNode vertex = {vertexData[i], nullptr};
graph.push_back(vertex);
}
// 添加边
for (int i = 0; i < sizeof(edges) / sizeof(edges[0]); i++) {
int from = edges[i][0];
int to = edges[i][1];
EdgeNode* edge = new EdgeNode;
edge->adjvex = to;
edge->next = graph[from].first;
graph[from].first = edge;
}
return graph;
}
vector<vector<int>> createGraphByAdjMatrix() {
int n = 6; // 图的顶点数
int edges[][2] = {{0, 1}, {0, 2}, {1, 2}, {1, 3}, {2, 4}, {3, 4}, {3, 5}, {4, 5}};
// 初始化邻接矩阵
vector<vector<int>> matrix(n, vector<int>(n, 0));
// 添加边
for (int i = 0; i < sizeof(edges) / sizeof(edges[0]); i++) {
int from = edges[i][0];
int to = edges[i][1];
matrix[from][to] = 1;
matrix[to][from] = 1; // 无向图需要加上这一行
}
return matrix;
}
void printGraphByAdjList(const vector<VertexNode>& graph) {
cout << "Graph by Adjacency List:" << endl;
for (int i = 0; i < graph.size(); i++) {
cout << graph[i].data << ": ";
EdgeNode* edge = graph[i].first;
while (edge != nullptr) {
cout << graph[edge->adjvex].data << " ";
edge = edge->next;
}
cout << endl;
}
}
void printGraphByAdjMatrix(const vector<vector<int>>& graph) {
cout << "Graph by Adjacency Matrix:" << endl;
for (int i = 0; i < graph.size(); i++) {
for (int j = 0; j < graph[i].size(); j++) {
cout << graph[i][j] << " ";
}
cout << endl;
}
}
int main() {
vector<VertexNode> graph1 = createGraphByAdjList();
printGraphByAdjList(graph1);
vector<vector<int>> graph2 = createGraphByAdjMatrix();
printGraphByAdjMatrix(graph2);
return 0;
}
```
这段代码实现了:
1. 创建了一个以邻接表方式存储的图,并输出了它的邻接表;
2. 创建了一个以邻接矩阵方式存储的图,并输出了它的邻接矩阵。
注意,这里的图是无向图,如果是有向图需要修改相应的代码。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)