#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define V 9
typedef struct AdjNode {
int vertex;
struct AdjNode* next;
} AdjNode;
typedef struct AdjList {
AdjNode* head;
} AdjList;
typedef struct Graph {
int numVertices;
AdjList adjLists[V];
} Graph;
AdjNode* newAdjNode(int vertex) {
AdjNode* node = (AdjNode*)malloc(sizeof(AdjNode));
node->vertex = vertex;
node->next = NULL;
return node;
}
Graph* createGraph() {
Graph* graph = (Graph*)malloc(sizeof(Graph));
graph->numVertices = V;
for (int i = 0; i < V; i++) {
graph->adjLists[i].head = NULL;
}
return graph;
}
void addEdge(Graph* graph, int src, int dest) {
AdjNode* node = newAdjNode(dest);
node->next = graph->adjLists[src].head;
graph->adjLists[src].head = node;
node = newAdjNode(src);
node->next = graph->adjLists[dest].head;
graph->adjLists[dest].head = node;
}
void DFSUtilAdjList(Graph* graph, int vertex, int* visited) {
visited[vertex] = 1;
printf("%c ", vertex + 'A');
AdjNode* temp = graph->adjLists[vertex].head;
while (temp) {
if (!visited[temp->vertex])
DFSUtilAdjList(graph, temp->vertex, visited);
temp = temp->next;
}
}
void DFSAdjList(Graph* graph) {
int visited[V] = {0};
for (int i = 0; i < V; i++) {
if (!visited[i])
DFSUtilAdjList(graph, i, visited);
}
printf("\n");
}
void buildAdjMatrix(Graph* graph, int adjMatrix[V][V]) {
for (int i = 0; i < V; i++) {
AdjNode* temp = graph->adjLists[i].head;
while (temp) {
adjMatrix[i][temp->vertex] = 1;
temp = temp->next;
}
}
}
void DFSUtilAdjMatrix(int adjMatrix[V][V], int vertex, int* visited) {
visited[vertex] = 1;
printf("%c ", vertex + 'A');
for (int i = 0; i < V; i++) {
if (adjMatrix[vertex][i] &&!visited[i])
DFSUtilAdjMatrix(adjMatrix, i, visited);
}
}
void DFSAdjMatrix(int adjMatrix[V][V]) {
int visited[V] = {0};
for (int i = 0; i < V; i++) {
if (!visited[i])
DFSUtilAdjMatrix(adjMatrix, i, visited);
}
printf("\n");
}
void printMenu() {
printf("1. 显示邻接表存储\n");
printf("2. 显示邻接矩阵存储\n");
printf("3. 邻接表深度优先遍历\n");
printf("4. 邻接矩阵深度优先遍历\n");
printf("5. 退出\n");
}
int main() {
Graph* graph = createGraph();
addEdge(graph, 0, 1);
addEdge(graph, 0, 2);
addEdge(graph, 1, 2);
addEdge(graph, 1, 3);
addEdge(graph, 1, 4);
addEdge(graph, 2, 5);
addEdge(graph, 3, 4);
addEdge(graph, 3, 6);
addEdge(graph, 4, 6);
addEdge(graph, 5, 6);
addEdge(graph, 7, 8);
addEdge(graph, 8, 7);
int choice;
int adjMatrix[V][V] = {0};
buildAdjMatrix(graph, adjMatrix);
while (1) {
printMenu();
printf("请输入你的选择: ");
scanf("%d", &choice);
while (getchar() != '\n');
switch (choice) {
case 1:
for (int i = 0; i < V; i++) {
printf("%c: ", i + 'A');
AdjNode* temp = graph->adjLists[i].head;
while (temp) {
printf("%c ", temp->vertex + 'A');
temp = temp->next;
}
printf("\n");
}
break;
case 2:
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
printf("%d ", adjMatrix[i][j]);
}
printf("\n");
}
break;
case 3:
DFSAdjList(graph);
break;
case 4:
DFSAdjMatrix(adjMatrix);
break;
case 5:
for (int i = 0; i < V; i++) {
AdjNode* temp = graph->adjLists[i].head;
AdjNode* next;
while (temp) {
next = temp->next;
free(temp);
temp = next;
}
}
free(graph);
exit(0);
default:
printf("无效的选择,请重新输入。\n");
}
}
return 0;
}