用c语言做一个用菜单函数实现的算法管理系统,该系统包含了二叉树和图的所有基础算法,包括树的创建,查找,插入,删除以及树的遍历和图的创建,图的深度和广度优先遍历,以及用普利姆算法和库鲁斯卡尔算法实现最小生成树,和最短路劲和关键路径以及拓扑排序的实现的相关算法,这些功能都用菜单函数来实现,菜单函数分为树的子菜单和图的子菜单
时间: 2023-11-27 15:50:57 浏览: 85
好的,下面是一个简单的实现,仅供参考:
```c
#include <stdio.h>
#include <stdlib.h>
// 树节点结构体
typedef struct TreeNode {
int data;
struct TreeNode *left;
struct TreeNode *right;
} TreeNode;
// 图节点结构体
typedef struct GraphNode {
int data;
struct GraphNode *next;
} GraphNode;
// 图结构体
typedef struct Graph {
int n; // 节点数
GraphNode **adjList; // 邻接表
} Graph;
// 创建树节点
TreeNode* createTreeNode(int data) {
TreeNode *node = (TreeNode*)malloc(sizeof(TreeNode));
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}
// 创建二叉搜索树
void createBinarySearchTree(TreeNode **root) {
int data;
printf("请输入节点值(-1 结束):");
scanf("%d", &data);
while (data != -1) {
TreeNode *node = createTreeNode(data);
if (*root == NULL) {
*root = node;
} else {
TreeNode *parent = *root;
while (1) {
if (data < parent->data) {
if (parent->left == NULL) {
parent->left = node;
break;
} else {
parent = parent->left;
}
} else {
if (parent->right == NULL) {
parent->right = node;
break;
} else {
parent = parent->right;
}
}
}
}
printf("请输入节点值(-1 结束):");
scanf("%d", &data);
}
}
// 先序遍历
void preOrderTraversal(TreeNode *root) {
if (root == NULL) {
return;
}
printf("%d ", root->data);
preOrderTraversal(root->left);
preOrderTraversal(root->right);
}
// 中序遍历
void inOrderTraversal(TreeNode *root) {
if (root == NULL) {
return;
}
inOrderTraversal(root->left);
printf("%d ", root->data);
inOrderTraversal(root->right);
}
// 后序遍历
void postOrderTraversal(TreeNode *root) {
if (root == NULL) {
return;
}
postOrderTraversal(root->left);
postOrderTraversal(root->right);
printf("%d ", root->data);
}
// 创建图节点
GraphNode* createGraphNode(int data) {
GraphNode *node = (GraphNode*)malloc(sizeof(GraphNode));
node->data = data;
node->next = NULL;
return node;
}
// 创建邻接表
void createAdjList(Graph *graph) {
int i, j, n, data;
GraphNode *node;
printf("请输入节点数:");
scanf("%d", &n);
graph->n = n;
graph->adjList = (GraphNode**)malloc(n * sizeof(GraphNode*));
for (i = 0; i < n; i++) {
graph->adjList[i] = NULL;
}
for (i = 0; i < n; i++) {
printf("请输入节点%d的邻居(-1 结束):", i + 1);
scanf("%d", &data);
while (data != -1) {
node = createGraphNode(data);
if (graph->adjList[i] == NULL) {
graph->adjList[i] = node;
} else {
GraphNode *p = graph->adjList[i];
while (p->next != NULL) {
p = p->next;
}
p->next = node;
}
printf("请输入节点%d的邻居(-1 结束):", i + 1);
scanf("%d", &data);
}
}
}
// 深度优先遍历
void depthFirstTraversal(Graph *graph, int v, int *visited) {
GraphNode *node;
visited[v] = 1;
printf("%d ", v + 1);
for (node = graph->adjList[v]; node != NULL; node = node->next) {
if (!visited[node->data - 1]) {
depthFirstTraversal(graph, node->data - 1, visited);
}
}
}
// 广度优先遍历
void breadthFirstTraversal(Graph *graph, int v, int *visited) {
int i, queue[100], front = 0, rear = 0;
GraphNode *node;
visited[v] = 1;
printf("%d ", v + 1);
queue[rear++] = v;
while (front != rear) {
v = queue[front++];
for (node = graph->adjList[v]; node != NULL; node = node->next) {
i = node->data - 1;
if (!visited[i]) {
visited[i] = 1;
printf("%d ", i + 1);
queue[rear++] = i;
}
}
}
}
// 普利姆算法实现最小生成树
void primAlgorithm(Graph *graph) {
int i, j, k, u, v, min, visited[100], lowCost[100], closest[100];
GraphNode *node;
for (i = 0; i < graph->n; i++) {
lowCost[i] = 0x7fffffff; // 初始化为最大值
visited[i] = 0;
}
visited[0] = 1;
for (node = graph->adjList[0]; node != NULL; node = node->next) {
v = node->data - 1;
lowCost[v] = 1;
closest[v] = 1;
}
for (i = 1; i < graph->n; i++) {
min = 0x7fffffff;
for (j = 0; j < graph->n; j++) {
if (!visited[j] && lowCost[j] < min) {
min = lowCost[j];
u = j;
}
}
visited[u] = 1;
printf("%d-%d ", closest[u] + 1, u + 1);
for (node = graph->adjList[u]; node != NULL; node = node->next) {
v = node->data - 1;
if (!visited[v] && node->data < lowCost[v]) {
lowCost[v] = node->data;
closest[v] = u;
}
}
}
}
// 库鲁斯卡尔算法实现最小生成树
void kruskalAlgorithm(Graph *graph) {
int i, j, k, u, v, min, parent[100];
GraphNode *node;
for (i = 0; i < graph->n; i++) {
parent[i] = i;
}
for (i = 0; i < graph->n - 1; i++) {
min = 0x7fffffff;
for (j = 0; j < graph->n; j++) {
for (node = graph->adjList[j]; node != NULL; node = node->next) {
u = j;
v = node->data - 1;
if (parent[u] != parent[v] && node->data < min) {
min = node->data;
k = i;
}
}
}
printf("%d-%d ", u + 1, v + 1);
for (j = 0; j < graph->n; j++) {
if (parent[j] == parent[v]) {
parent[j] = parent[u];
}
}
}
}
// 最短路径和关键路径
void shortestPathAndCriticalPath() {
// TODO: 实现最短路径和关键路径算法
}
// 拓扑排序
void topologicalSorting() {
// TODO: 实现拓扑排序算法
}
// 树的子菜单
void treeSubMenu(TreeNode **root) {
int choice;
do {
printf("\n");
printf("1. 创建二叉搜索树\n");
printf("2. 先序遍历\n");
printf("3. 中序遍历\n");
printf("4. 后序遍历\n");
printf("0. 返回上级菜单\n");
printf("请选择操作:");
scanf("%d", &choice);
switch (choice) {
case 1:
createBinarySearchTree(root);
break;
case 2:
preOrderTraversal(*root);
printf("\n");
break;
case 3:
inOrderTraversal(*root);
printf("\n");
break;
case 4:
postOrderTraversal(*root);
printf("\n");
break;
}
} while (choice != 0);
}
// 图的子菜单
void graphSubMenu(Graph *graph) {
int choice, i, v, visited[100];
do {
printf("\n");
printf("1. 创建邻接表\n");
printf("2. 深度优先遍历\n");
printf("3. 广度优先遍历\n");
printf("4. 普利姆算法实现最小生成树\n");
printf("5. 库鲁斯卡尔算法实现最小生成树\n");
printf("6. 最短路径和关键路径\n");
printf("7. 拓扑排序\n");
printf("0. 返回上级菜单\n");
printf("请选择操作:");
scanf("%d", &choice);
switch (choice) {
case 1:
createAdjList(graph);
break;
case 2:
for (i = 0; i < graph->n; i++) {
visited[i] = 0;
}
printf("请输入起始节点:");
scanf("%d", &v);
depthFirstTraversal(graph, v - 1, visited);
printf("\n");
break;
case 3:
for (i = 0; i < graph->n; i++) {
visited[i] = 0;
}
printf("请输入起始节点:");
scanf("%d", &v);
breadthFirstTraversal(graph, v - 1, visited);
printf("\n");
break;
case 4:
primAlgorithm(graph);
printf("\n");
break;
case 5:
kruskalAlgorithm(graph);
printf("\n");
break;
case 6:
shortestPathAndCriticalPath();
break;
case 7:
topologicalSorting();
break;
}
} while (choice != 0);
}
// 主菜单
void mainMenu() {
int choice;
TreeNode *root = NULL;
Graph graph;
graph.n = 0;
graph.adjList = NULL;
do {
printf("\n");
printf("1. 树\n");
printf("2. 图\n");
printf("0. 退出\n");
printf("请选择操作:");
scanf("%d", &choice);
switch (choice) {
case 1:
treeSubMenu(&root);
break;
case 2:
graphSubMenu(&graph);
break;
}
} while (choice != 0);
}
int main() {
mainMenu();
return 0;
}
```
阅读全文