用c语言:设图结点的元素类型为char,建立一个不少于8个顶点的带权无向图G,实现以下图的各种基本操作的程序: ① 用邻接矩阵作为储结构存储图G并输出该邻接矩阵; ② 用邻接链表作为储结构存储图G并输出该邻接链表; ③ 按DFS算法输出图G中顶点的遍历序列; ④ 按BFS算法输出图G中顶点的遍历序列; ⑤主函数通过函数调用实现以上各项操作。
时间: 2023-12-14 22:36:15 浏览: 65
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTEX_NUM 8
// 邻接矩阵结构体
typedef struct {
char vertex[MAX_VERTEX_NUM]; // 存储结点的元素类型为char
int edge[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; // 存储边的权值
int vertexNum; // 结点数量
int edgeNum; // 边数量
} AdjMatrix;
// 邻接链表结构体
typedef struct EdgeNode {
int adjvex; // 邻接点的下标
int weight; // 权值
struct EdgeNode *next; // 指向下一个邻接点
} EdgeNode;
typedef struct {
char vertex;
EdgeNode *firstEdge; // 指向第一个邻接点
} VertexNode;
typedef struct {
VertexNode vertex[MAX_VERTEX_NUM]; // 存储结点的元素类型为char
int vertexNum; // 结点数量
int edgeNum; // 边数量
} AdjList;
// 创建邻接矩阵
void createAdjMatrix(AdjMatrix *G) {
char vertex[MAX_VERTEX_NUM] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'};
int edge[MAX_VERTEX_NUM][MAX_VERTEX_NUM] = {
{0, 1, 0, 1, 0, 0, 0, 0},
{1, 0, 1, 1, 0, 0, 0, 0},
{0, 1, 0, 1, 1, 1, 0, 0},
{1, 1, 1, 0, 0, 1, 1, 0},
{0, 0, 1, 0, 0, 1, 0, 1},
{0, 0, 1, 1, 1, 0, 0, 1},
{0, 0, 0, 1, 0, 0, 0, 1},
{0, 0, 0, 0, 1, 1, 1, 0}
};
for (int i = 0; i < MAX_VERTEX_NUM; i++) {
G->vertex[i] = vertex[i];
for (int j = 0; j < MAX_VERTEX_NUM; j++) {
G->edge[i][j] = edge[i][j];
}
}
G->vertexNum = MAX_VERTEX_NUM;
G->edgeNum = 14;
}
// 输出邻接矩阵
void printAdjMatrix(AdjMatrix G) {
printf("邻接矩阵:\n");
printf(" ");
for (int i = 0; i < G.vertexNum; i++) {
printf("%c ", G.vertex[i]);
}
printf("\n");
for (int i = 0; i < G.vertexNum; i++) {
printf("%c ", G.vertex[i]);
for (int j = 0; j < G.vertexNum; j++) {
printf("%d ", G.edge[i][j]);
}
printf("\n");
}
}
// 创建邻接链表
void createAdjList(AdjList *G) {
char vertex[MAX_VERTEX_NUM] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'};
int adjList[MAX_VERTEX_NUM][3] = {
{1, 1, 3},
{0, 1, 2},
{1, 1, 3},
{0, 1, 1},
{2, 1, 3},
{2, 1, 4},
{3, 1, 7},
{4, 1, 6}
};
for (int i = 0; i < MAX_VERTEX_NUM; i++) {
G->vertex[i].vertex = vertex[i];
G->vertex[i].firstEdge = NULL;
}
for (int i = 0; i < MAX_VERTEX_NUM; i++) {
int adjvex = adjList[i][0];
int weight = adjList[i][1];
EdgeNode *e = (EdgeNode *)malloc(sizeof(EdgeNode));
e->adjvex = adjvex;
e->weight = weight;
e->next = G->vertex[i].firstEdge;
G->vertex[i].firstEdge = e;
}
G->vertexNum = MAX_VERTEX_NUM;
G->edgeNum = 14;
}
// 输出邻接链表
void printAdjList(AdjList G) {
printf("邻接链表:\n");
for (int i = 0; i < G.vertexNum; i++) {
printf("%c -> ", G.vertex[i].vertex);
EdgeNode *e = G.vertex[i].firstEdge;
while (e != NULL) {
printf("%c(%d) ", G.vertex[e->adjvex].vertex, e->weight);
e = e->next;
}
printf("\n");
}
}
// DFS遍历邻接矩阵
void DFS_Matrix(AdjMatrix G, int i, int *visited) {
visited[i] = 1;
printf("%c ", G.vertex[i]);
for (int j = 0; j < MAX_VERTEX_NUM; j++) {
if (G.edge[i][j] == 1 && visited[j] == 0) {
DFS_Matrix(G, j, visited);
}
}
}
// DFS遍历邻接链表
void DFS_List(AdjList G, int i, int *visited) {
visited[i] = 1;
printf("%c ", G.vertex[i].vertex);
EdgeNode *e = G.vertex[i].firstEdge;
while (e != NULL) {
if (visited[e->adjvex] == 0) {
DFS_List(G, e->adjvex, visited);
}
e = e->next;
}
}
// BFS遍历邻接矩阵
void BFS_Matrix(AdjMatrix G, int i, int *visited) {
int queue[MAX_VERTEX_NUM];
int front = 0, rear = 0;
visited[i] = 1;
printf("%c ", G.vertex[i]);
queue[rear++] = i;
while (front != rear) {
int j = queue[front++];
for (int k = 0; k < MAX_VERTEX_NUM; k++) {
if (G.edge[j][k] == 1 && visited[k] == 0) {
visited[k] = 1;
printf("%c ", G.vertex[k]);
queue[rear++] = k;
}
}
}
}
// BFS遍历邻接链表
void BFS_List(AdjList G, int i, int *visited) {
int queue[MAX_VERTEX_NUM];
int front = 0, rear = 0;
visited[i] = 1;
printf("%c ", G.vertex[i].vertex);
queue[rear++] = i;
while (front != rear) {
int j = queue[front++];
EdgeNode *e = G.vertex[j].firstEdge;
while (e != NULL) {
if (visited[e->adjvex] == 0) {
visited[e->adjvex] = 1;
printf("%c ", G.vertex[e->adjvex].vertex);
queue[rear++] = e->adjvex;
}
e = e->next;
}
}
}
int main() {
AdjMatrix G_Matrix;
createAdjMatrix(&G_Matrix);
printAdjMatrix(G_Matrix);
printf("\n");
AdjList G_List;
createAdjList(&G_List);
printAdjList(G_List);
printf("\n");
int visited[MAX_VERTEX_NUM] = {0};
printf("DFS遍历邻接矩阵:");
DFS_Matrix(G_Matrix, 0, visited);
printf("\n");
for (int i = 0; i < MAX_VERTEX_NUM; i++) {
visited[i] = 0;
}
printf("DFS遍历邻接链表:");
DFS_List(G_List, 0, visited);
printf("\n");
for (int i = 0; i < MAX_VERTEX_NUM; i++) {
visited[i] = 0;
}
printf("BFS遍历邻接矩阵:");
BFS_Matrix(G_Matrix, 0, visited);
printf("\n");
for (int i = 0; i < MAX_VERTEX_NUM; i++) {
visited[i] = 0;
}
printf("BFS遍历邻接链表:");
BFS_List(G_List, 0, visited);
printf("\n");
return 0;
}
```
阅读全文