请用C语言编程实现图的邻接矩阵和邻接表两种存储结构,要有菜单
时间: 2024-05-10 12:17:50 浏览: 153
实现图的邻接矩阵和邻接表存储
好的,以下是代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTEX_NUM 100 // 最大顶点数
#define MAX_EDGE_NUM 10000 // 最大边数
// 邻接矩阵存储结构
typedef struct {
int vertex[MAX_VERTEX_NUM]; // 存储顶点信息
int edge[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; // 存储边信息
int vertex_num; // 顶点数
int edge_num; // 边数
} AdjacencyMatrix;
// 邻接表存储结构
typedef struct EdgeNode {
int adjvex; // 邻接顶点
struct EdgeNode *next; // 指向下一个邻接点的指针
} EdgeNode;
typedef struct {
int vertex; // 顶点信息
EdgeNode *first_edge; // 指向第一个邻接点的指针
} VertexNode;
typedef struct {
VertexNode vertex[MAX_VERTEX_NUM]; // 存储顶点和边信息
int vertex_num; // 顶点数
int edge_num; // 边数
} AdjacencyList;
// 初始化邻接矩阵
void init_adjacency_matrix(AdjacencyMatrix *matrix) {
int i, j;
matrix->vertex_num = 0;
matrix->edge_num = 0;
for (i = 0; i < MAX_VERTEX_NUM; i++) {
matrix->vertex[i] = 0;
for (j = 0; j < MAX_VERTEX_NUM; j++) {
matrix->edge[i][j] = 0;
}
}
}
// 添加顶点到邻接矩阵
void add_vertex_to_matrix(AdjacencyMatrix *matrix, int vertex) {
if (matrix->vertex_num >= MAX_VERTEX_NUM) {
printf("Error: too many vertices!\n");
return;
}
matrix->vertex[matrix->vertex_num++] = vertex;
}
// 添加边到邻接矩阵
void add_edge_to_matrix(AdjacencyMatrix *matrix, int start, int end, int weight) {
int i, j;
for (i = 0; i < matrix->vertex_num; i++) {
if (matrix->vertex[i] == start) {
break;
}
}
for (j = 0; j < matrix->vertex_num; j++) {
if (matrix->vertex[j] == end) {
break;
}
}
if (i >= matrix->vertex_num || j >= matrix->vertex_num) {
printf("Error: start vertex or end vertex not found!\n");
return;
}
if (matrix->edge[i][j] != 0) {
printf("Error: edge already exists!\n");
return;
}
matrix->edge[i][j] = weight;
matrix->edge_num++;
}
// 初始化邻接表
void init_adjacency_list(AdjacencyList *list) {
int i;
list->vertex_num = 0;
list->edge_num = 0;
for (i = 0; i < MAX_VERTEX_NUM; i++) {
list->vertex[i].vertex = 0;
list->vertex[i].first_edge = NULL;
}
}
// 添加顶点到邻接表
void add_vertex_to_list(AdjacencyList *list, int vertex) {
if (list->vertex_num >= MAX_VERTEX_NUM) {
printf("Error: too many vertices!\n");
return;
}
list->vertex[list->vertex_num].vertex = vertex;
list->vertex_num++;
}
// 添加边到邻接表
void add_edge_to_list(AdjacencyList *list, int start, int end, int weight) {
int i, j;
for (i = 0; i < list->vertex_num; i++) {
if (list->vertex[i].vertex == start) {
break;
}
}
for (j = 0; j < list->vertex_num; j++) {
if (list->vertex[j].vertex == end) {
break;
}
}
if (i >= list->vertex_num || j >= list->vertex_num) {
printf("Error: start vertex or end vertex not found!\n");
return;
}
EdgeNode *p = list->vertex[i].first_edge;
while (p != NULL) {
if (p->adjvex == j) {
printf("Error: edge already exists!\n");
return;
}
p = p->next;
}
EdgeNode *new_edge = (EdgeNode *)malloc(sizeof(EdgeNode));
new_edge->adjvex = j;
new_edge->next = list->vertex[i].first_edge;
list->vertex[i].first_edge = new_edge;
list->edge_num++;
}
// 打印邻接矩阵
void print_adjacency_matrix(AdjacencyMatrix *matrix) {
int i, j;
printf("Vertexes:");
for (i = 0; i < matrix->vertex_num; i++) {
printf(" %d", matrix->vertex[i]);
}
printf("\n");
printf("Edges:\n");
for (i = 0; i < matrix->vertex_num; i++) {
printf("%d", matrix->vertex[i]);
for (j = 0; j < matrix->vertex_num; j++) {
printf(" %d", matrix->edge[i][j]);
}
printf("\n");
}
}
// 打印邻接表
void print_adjacency_list(AdjacencyList *list) {
int i;
printf("Vertexes:");
for (i = 0; i < list->vertex_num; i++) {
printf(" %d", list->vertex[i].vertex);
}
printf("\n");
printf("Edges:\n");
for (i = 0; i < list->vertex_num; i++) {
printf("%d", list->vertex[i].vertex);
EdgeNode *p = list->vertex[i].first_edge;
while (p != NULL) {
printf(" %d", list->vertex[p->adjvex].vertex);
p = p->next;
}
printf("\n");
}
}
int main() {
int choice;
AdjacencyMatrix matrix;
AdjacencyList list;
init_adjacency_matrix(&matrix);
init_adjacency_list(&list);
while (1) {
printf("\nMenu:\n");
printf("1. Add vertex\n");
printf("2. Add edge\n");
printf("3. Print adjacency matrix\n");
printf("4. Print adjacency list\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
printf("\n");
switch (choice) {
case 1:
printf("Enter vertex: ");
int vertex;
scanf("%d", &vertex);
add_vertex_to_matrix(&matrix, vertex);
add_vertex_to_list(&list, vertex);
break;
case 2:
printf("Enter start vertex: ");
int start;
scanf("%d", &start);
printf("Enter end vertex: ");
int end;
scanf("%d", &end);
printf("Enter weight: ");
int weight;
scanf("%d", &weight);
add_edge_to_matrix(&matrix, start, end, weight);
add_edge_to_list(&list, start, end, weight);
break;
case 3:
print_adjacency_matrix(&matrix);
break;
case 4:
print_adjacency_list(&list);
break;
case 5:
exit(0);
default:
printf("Invalid choice!\n");
break;
}
}
return 0;
}
```
这个程序实现了一个简单的菜单,可以通过输入不同的选项来实现添加顶点、添加边、打印邻接矩阵和打印邻接表等操作。菜单由一个无限循环实现,直到用户选择退出为止。在添加顶点和边的过程中,程序会检查输入的顶点和边是否有效,以及是否已经存在。
阅读全文