分别以邻接矩阵和邻接表作为存储结构,实现以下图的基本操作: 1.增加一个新顶点v,InsertVex(G, v); 2.删除顶点v及其相关的边,DeleteVex(G, v); 3.增加一条边<v,w>,InsertArc(G, v, w); 4.删除一条边<v,w>,DeleteArc(G, v, w)。全部代码
时间: 2024-03-17 18:44:49 浏览: 22
以下是邻接矩阵和邻接表实现图的基本操作的完整代码:
邻接矩阵:
```c++
#include <iostream>
#include <vector>
using namespace std;
const int MAXN = 100;
class Graph {
public:
Graph(int n = MAXN) : n(n) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
matrix[i][j] = 0;
}
}
}
void InsertVex(int v) {
matrix[v][v] = 0;
++n;
}
void DeleteVex(int v) {
for (int i = 0; i < n; ++i) {
matrix[i][v] = 0;
matrix[v][i] = 0;
}
--n;
}
void InsertArc(int v, int w) {
matrix[v][w] = 1;
}
void DeleteArc(int v, int w) {
matrix[v][w] = 0;
}
void Print() {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
private:
int matrix[MAXN][MAXN];
int n;
};
int main() {
Graph g(3);
g.InsertArc(0, 1);
g.InsertArc(0, 2);
g.InsertArc(1, 2);
g.Print();
g.InsertVex(3);
g.Print();
g.DeleteArc(0, 1);
g.Print();
g.DeleteVex(2);
g.Print();
return 0;
}
```
邻接表:
```c++
#include <iostream>
#include <vector>
using namespace std;
class Graph {
public:
Graph(int n = 0) : n(n) {
for (int i = 0; i < n; ++i) {
vector<int> temp;
adj.push_back(temp);
}
}
void InsertVex(int v) {
vector<int> temp;
adj.push_back(temp);
++n;
}
void DeleteVex(int v) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < adj[i].size(); ++j) {
if (adj[i][j] == v) {
adj[i].erase(adj[i].begin() + j);
break;
}
}
}
adj.erase(adj.begin() + v);
--n;
}
void InsertArc(int v, int w) {
adj[v].push_back(w);
}
void DeleteArc(int v, int w) {
for (int i = 0; i < adj[v].size(); ++i) {
if (adj[v][i] == w) {
adj[v].erase(adj[v].begin() + i);
break;
}
}
}
void Print() {
for (int i = 0; i < n; ++i) {
cout << i << ": ";
for (int j = 0; j < adj[i].size(); ++j) {
cout << adj[i][j] << " ";
}
cout << endl;
}
}
private:
vector<vector<int>> adj;
int n;
};
int main() {
Graph g(3);
g.InsertArc(0, 1);
g.InsertArc(0, 2);
g.InsertArc(1, 2);
g.Print();
g.InsertVex(3);
g.Print();
g.DeleteArc(0, 1);
g.Print();
g.DeleteVex(2);
g.Print();
return 0;
}
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)