用c语言和c++写一个"图的基本操作
时间: 2024-03-22 21:38:23 浏览: 52
c、c++代码.zip
5星 · 资源好评率100%
好的,下面是图的基本操作的C和C++实现。
C语言实现:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTEX_NUM 20 // 图的最大顶点数
// 定义图的结构体
typedef struct {
int vertex[MAX_VERTEX_NUM]; // 存放顶点的数组
int arc[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; // 存放边的二维数组
int vertex_num; // 实际的顶点数
int edge_num; // 实际的边数
} Graph;
// 初始化图
void InitGraph(Graph* G) {
int i, j;
G->vertex_num = 0;
G->edge_num = 0;
for (i = 0; i < MAX_VERTEX_NUM; i++) {
G->vertex[i] = 0;
for (j = 0; j < MAX_VERTEX_NUM; j++) {
G->arc[i][j] = 0;
}
}
}
// 添加顶点
void AddVertex(Graph* G, int v) {
if (G->vertex_num < MAX_VERTEX_NUM) {
G->vertex[G->vertex_num++] = v;
}
}
// 添加边
void AddEdge(Graph* G, int v1, int v2) {
int i, j;
for (i = 0; i < G->vertex_num; i++) {
if (G->vertex[i] == v1) break;
}
for (j = 0; j < G->vertex_num; j++) {
if (G->vertex[j] == v2) break;
}
if (i >= G->vertex_num || j >= G->vertex_num) return;
G->arc[i][j] = 1;
G->arc[j][i] = 1;
G->edge_num++;
}
// 删除边
void DeleteEdge(Graph* G, int v1, int v2) {
int i, j;
for (i = 0; i < G->vertex_num; i++) {
if (G->vertex[i] == v1) break;
}
for (j = 0; j < G->vertex_num; j++) {
if (G->vertex[j] == v2) break;
}
if (i >= G->vertex_num || j >= G->vertex_num) return;
G->arc[i][j] = 0;
G->arc[j][i] = 0;
G->edge_num--;
}
// 遍历图(深度优先遍历)
void DFS(Graph* G, int v, int* visited) {
int i;
visited[v] = 1;
printf("%d ", G->vertex[v]);
for (i = 0; i < G->vertex_num; i++) {
if (G->arc[v][i] == 1 && visited[i] == 0) {
DFS(G, i, visited);
}
}
}
// 遍历图(广度优先遍历)
void BFS(Graph* G, int v, int* visited) {
int queue[MAX_VERTEX_NUM];
int front = 0, rear = 0;
int i, j;
visited[v] = 1;
printf("%d ", G->vertex[v]);
queue[rear++] = v;
while (front != rear) {
i = queue[front++];
for (j = 0; j < G->vertex_num; j++) {
if (G->arc[i][j] == 1 && visited[j] == 0) {
visited[j] = 1;
printf("%d ", G->vertex[j]);
queue[rear++] = j;
}
}
}
}
int main() {
Graph G;
int visited[MAX_VERTEX_NUM] = { 0 };
InitGraph(&G);
AddVertex(&G, 1);
AddVertex(&G, 2);
AddVertex(&G, 3);
AddVertex(&G, 4);
AddEdge(&G, 1, 2);
AddEdge(&G, 1, 3);
AddEdge(&G, 2, 4);
AddEdge(&G, 3, 4);
DFS(&G, 0, visited);
printf("\n");
for (int i = 0; i < MAX_VERTEX_NUM; i++) {
visited[i] = 0;
}
BFS(&G, 0, visited);
printf("\n");
return 0;
}
```
C++实现:
```c++
#include <iostream>
#include <queue>
#define MAX_VERTEX_NUM 20 // 图的最大顶点数
using namespace std;
// 定义图的结构体
typedef struct {
int vertex[MAX_VERTEX_NUM]; // 存放顶点的数组
int arc[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; // 存放边的二维数组
int vertex_num; // 实际的顶点数
int edge_num; // 实际的边数
} Graph;
// 初始化图
void InitGraph(Graph* G) {
int i, j;
G->vertex_num = 0;
G->edge_num = 0;
for (i = 0; i < MAX_VERTEX_NUM; i++) {
G->vertex[i] = 0;
for (j = 0; j < MAX_VERTEX_NUM; j++) {
G->arc[i][j] = 0;
}
}
}
// 添加顶点
void AddVertex(Graph* G, int v) {
if (G->vertex_num < MAX_VERTEX_NUM) {
G->vertex[G->vertex_num++] = v;
}
}
// 添加边
void AddEdge(Graph* G, int v1, int v2) {
int i, j;
for (i = 0; i < G->vertex_num; i++) {
if (G->vertex[i] == v1) break;
}
for (j = 0; j < G->vertex_num; j++) {
if (G->vertex[j] == v2) break;
}
if (i >= G->vertex_num || j >= G->vertex_num) return;
G->arc[i][j] = 1;
G->arc[j][i] = 1;
G->edge_num++;
}
// 删除边
void DeleteEdge(Graph* G, int v1, int v2) {
int i, j;
for (i = 0; i < G->vertex_num; i++) {
if (G->vertex[i] == v1) break;
}
for (j = 0; j < G->vertex_num; j++) {
if (G->vertex[j] == v2) break;
}
if (i >= G->vertex_num || j >= G->vertex_num) return;
G->arc[i][j] = 0;
G->arc[j][i] = 0;
G->edge_num--;
}
// 遍历图(深度优先遍历)
void DFS(Graph* G, int v, int* visited) {
int i;
visited[v] = 1;
cout << G->vertex[v] << " ";
for (i = 0; i < G->vertex_num; i++) {
if (G->arc[v][i] == 1 && visited[i] == 0) {
DFS(G, i, visited);
}
}
}
// 遍历图(广度优先遍历)
void BFS(Graph* G, int v, int* visited) {
queue<int> q;
int i, j;
visited[v] = 1;
cout << G->vertex[v] << " ";
q.push(v);
while (!q.empty()) {
i = q.front();
q.pop();
for (j = 0; j < G->vertex_num; j++) {
if (G->arc[i][j] == 1 && visited[j] == 0) {
visited[j] = 1;
cout << G->vertex[j] << " ";
q.push(j);
}
}
}
}
int main() {
Graph G;
int visited[MAX_VERTEX_NUM] = { 0 };
InitGraph(&G);
AddVertex(&G, 1);
AddVertex(&G, 2);
AddVertex(&G, 3);
AddVertex(&G, 4);
AddEdge(&G, 1, 2);
AddEdge(&G, 1, 3);
AddEdge(&G, 2, 4);
AddEdge(&G, 3, 4);
DFS(&G, 0, visited);
cout << endl;
for (int i = 0; i < MAX_VERTEX_NUM; i++) {
visited[i] = 0;
}
BFS(&G, 0, visited);
cout << endl;
return 0;
}
```
阅读全文