#include<iostream> #include<queue> #include<vector> using namespace std; // 定义图的邻接表结构 struct edge { int dest; edge* next; }; struct vertex { edge* head; }; // 图类 class Graph { private: int V; // 顶点数 vertex* adj; // 邻接表 public: Graph(int V) { this->V = V; adj = new vertex[V]; for (int i = 0; i < V; ++i) { adj[i].head = nullptr; } } // 添加边 void addEdge(int src, int dest) { edge* e = new edge; e->dest = dest; e->next = adj[src].head; adj[src].head = e; } // 深度优先遍历 void DFS(int v, bool* visited) { visited[v] = true; cout << v << " "; edge* e = adj[v].head; while (e != nullptr) { if (!visited[e->dest]) { DFS(e->dest, visited); } e = e->next; } } // 广度优先遍历 void BFS(int v, bool* visited) { queue<int> q; visited[v] = true; q.push(v); while (!q.empty()) { int u = q.front(); q.pop(); cout << u << " "; edge* e = adj[u].head; while (e != nullptr) { if (!visited[e->dest]) { visited[e->dest] = true; q.push(e->dest); } e = e->next; } } } }; int main() { int V, E; cout << "请输入顶点数和边数:" << endl; cin >> V >> E; Graph g(V); cout << "请输入每条边的起点和终点:" << endl; for (int i = 0; i < E; ++i) { int src, dest; cin >> src >> dest; g.addEdge(src, dest); } // 深度优先遍历 cout << "深度优先遍历结果为:" << endl; bool* visited = new bool[V]; for (int i = 0; i < V; ++i) { visited[i] = false; } for (int i = 0; i < V; ++i) { if (!visited[i]) { g.DFS(i, visited); } } cout << endl; // 广度优先遍历 cout << "广度优先遍历结果为:" << endl; for (int i = 0; i < V; ++i) { visited[i] = false; } for (int i = 0; i < V; ++i) { if (!visited[i]) { g.BFS(i, visited); } } cout << endl; return 0; }这段代码从输入数字修改为输入为字母
时间: 2024-02-10 12:10:46 浏览: 77
将输入数字修改为输入字母需要对代码进行一定的修改,因为字母无法直接转化为整数进行处理。以下是修改后的代码:
```
#include<iostream>
#include<queue>
#include<vector>
#include<map>
using namespace std;
// 定义图的邻接表结构
struct edge {
char dest;
edge* next;
};
struct vertex {
edge* head;
};
// 图类
class Graph {
private:
int V; // 顶点数
vertex* adj; // 邻接表
map<char, int> charToInt; // 将字母映射为数字
public:
Graph(int V) {
this->V = V;
adj = new vertex[V];
for (int i = 0; i < V; ++i) {
adj[i].head = nullptr;
}
}
// 添加边
void addEdge(char src, char dest) {
// 将字母映射为数字
int srcInt, destInt;
if (charToInt.count(src) == 0) {
srcInt = charToInt.size();
charToInt[src] = srcInt;
} else {
srcInt = charToInt[src];
}
if (charToInt.count(dest) == 0) {
destInt = charToInt.size();
charToInt[dest] = destInt;
} else {
destInt = charToInt[dest];
}
edge* e = new edge;
e->dest = destInt;
e->next = adj[srcInt].head;
adj[srcInt].head = e;
}
// 深度优先遍历
void DFS(int v, bool* visited) {
visited[v] = true;
// 将数字映射为字母输出
for (auto it = charToInt.begin(); it != charToInt.end(); ++it) {
if (it->second == v) {
cout << it->first << " ";
break;
}
}
edge* e = adj[v].head;
while (e != nullptr) {
if (!visited[e->dest]) {
DFS(e->dest, visited);
}
e = e->next;
}
}
// 广度优先遍历
void BFS(int v, bool* visited) {
queue<int> q;
visited[v] = true;
q.push(v);
while (!q.empty()) {
int u = q.front();
q.pop();
// 将数字映射为字母输出
for (auto it = charToInt.begin(); it != charToInt.end(); ++it) {
if (it->second == u) {
cout << it->first << " ";
break;
}
}
edge* e = adj[u].head;
while (e != nullptr) {
if (!visited[e->dest]) {
visited[e->dest] = true;
q.push(e->dest);
}
e = e->next;
}
}
}
};
int main() {
int V, E;
cout << "请输入顶点数和边数:" << endl;
cin >> V >> E;
Graph g(V);
cout << "请输入每条边的起点和终点:" << endl;
for (int i = 0; i < E; ++i) {
char src, dest;
cin >> src >> dest;
g.addEdge(src, dest);
}
// 深度优先遍历
cout << "深度优先遍历结果为:" << endl;
bool* visited = new bool[V];
for (int i = 0; i < V; ++i) {
visited[i] = false;
}
for (int i = 0; i < V; ++i) {
if (!visited[i]) {
g.DFS(i, visited);
}
}
cout << endl;
// 广度优先遍历
cout << "广度优先遍历结果为:" << endl;
for (int i = 0; i < V; ++i) {
visited[i] = false;
}
for (int i = 0; i < V; ++i) {
if (!visited[i]) {
g.BFS(i, visited);
}
}
cout << endl;
return 0;
}
```
阅读全文