写一下提供前驱图的修改功能,包括边和结点的增加和删除c语言代码
时间: 2024-02-24 18:57:28 浏览: 12
提供前驱图的修改功能,包括边和结点的增加和删除的 C 语言代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
// 结点结构体定义
typedef struct Node {
int id; // 结点编号
char name[20]; // 结点名称
int type; // 结点类型,1表示源结点,2表示目标结点,3表示中间结点
} Node;
// 边结构体定义
typedef struct Edge {
int id; // 边编号
int weight; // 边权重
int from; // 出发结点编号
int to; // 到达结点编号
} Edge;
// 前驱图结构体定义
typedef struct Graph {
Node nodes[100]; // 结点数组,最多支持100个结点
Edge edges[1000]; // 边数组,最多支持1000条边
int nodeCount; // 结点数量
int edgeCount; // 边数量
} Graph;
// 添加结点函数
void addNode(Graph *graph, int id, char *name, int type) {
// 判断结点数量是否超过最大值
if (graph->nodeCount >= 100) {
printf("Error: Node count exceeds the maximum value!\n");
return;
}
// 判断结点编号是否已存在
for (int i = 0; i < graph->nodeCount; i++) {
if (graph->nodes[i].id == id) {
printf("Error: Node with the same ID already exists!\n");
return;
}
}
// 添加结点
graph->nodes[graph->nodeCount].id = id;
strcpy(graph->nodes[graph->nodeCount].name, name);
graph->nodes[graph->nodeCount].type = type;
graph->nodeCount++;
printf("Node added successfully!\n");
}
// 删除结点函数
void deleteNode(Graph *graph, int id) {
// 查找结点位置
int index = -1;
for (int i = 0; i < graph->nodeCount; i++) {
if (graph->nodes[i].id == id) {
index = i;
break;
}
}
// 判断结点是否存在
if (index == -1) {
printf("Error: Node not found!\n");
return;
}
// 删除结点
for (int i = index; i < graph->nodeCount - 1; i++) {
graph->nodes[i] = graph->nodes[i + 1];
}
graph->nodeCount--;
printf("Node deleted successfully!\n");
}
// 添加边函数
void addEdge(Graph *graph, int id, int weight, int from, int to) {
// 判断边数量是否超过最大值
if (graph->edgeCount >= 1000) {
printf("Error: Edge count exceeds the maximum value!\n");
return;
}
// 判断边编号是否已存在
for (int i = 0; i < graph->edgeCount; i++) {
if (graph->edges[i].id == id) {
printf("Error: Edge with the same ID already exists!\n");
return;
}
}
// 判断结点是否存在
int fromIndex = -1, toIndex = -1;
for (int i = 0; i < graph->nodeCount; i++) {
if (graph->nodes[i].id == from) {
fromIndex = i;
}
if (graph->nodes[i].id == to) {
toIndex = i;
}
}
if (fromIndex == -1 || toIndex == -1) {
printf("Error: Node not found!\n");
return;
}
// 添加边
graph->edges[graph->edgeCount].id = id;
graph->edges[graph->edgeCount].weight = weight;
graph->edges[graph->edgeCount].from = from;
graph->edges[graph->edgeCount].to = to;
graph->edgeCount++;
printf("Edge added successfully!\n");
}
// 删除边函数
void deleteEdge(Graph *graph, int id) {
// 查找边位置
int index = -1;
for (int i = 0; i < graph->edgeCount; i++) {
if (graph->edges[i].id == id) {
index = i;
break;
}
}
// 判断边是否存在
if (index == -1) {
printf("Error: Edge not found!\n");
return;
}
// 删除边
for (int i = index; i < graph->edgeCount - 1; i++) {
graph->edges[i] = graph->edges[i + 1];
}
graph->edgeCount--;
printf("Edge deleted successfully!\n");
}
int main() {
Graph graph;
graph.nodeCount = 0;
graph.edgeCount = 0;
// 添加结点
addNode(&graph, 1, "Source Node", 1);
addNode(&graph, 2, "Target Node", 2);
addNode(&graph, 3, "Middle Node", 3);
// 添加边
addEdge(&graph, 1, 10, 1, 3);
addEdge(&graph, 2, 20, 3, 2);
// 删除结点
deleteNode(&graph, 3);
// 删除边
deleteEdge(&graph, 1);
return 0;
}
```
上述代码实现了前驱图的结点和边的增加和删除功能,并且在添加和删除操作时对输入进行了一些判断,例如判断结点和边的数量是否超过最大值,以及判断结点和边的编号是否已存在等,确保了前驱图的一致性和完整性。