c - 在函数内部如何检查边缘是否存在,以便在 C 中不会重新插入边缘
问题描述
int addEdge(struct Graph* graph, int src, int dest) {
struct AdjListNode *newNode = newListNode(dest);
newNode->next = graph->array[src].head;
graph->array[src].head = newNode;
newNode = newListNode(src);
newNode->next = graph->array[dest].head;
graph->array[dest].head = newNode;
}
解决方案
我就是这样做的!
int addEdge(struct Graph* graph, int src, int dest) {
if (src == dest) {
printf("Can't connect the same node");
return 0;
}
node *nodeList2 = graph->array[src].head;
while (nodeList2 != NULL) {
if (nodeList2->dest == dest || nodeList2->dest == src) {
printf(" the edge exists %d και %d", src, dest);
return 0;
}
nodeList2 = nodeList2->next;
}
node *newNode = newListNode(dest);
newNode->next = graph->array[src].head;
graph->array[src].head = newNode;
newNode = newListNode(src);
newNode->next = graph->array[dest].head;
graph->array[dest].head = newNode;
}
推荐阅读
- camunda - 在 Camunda 中启用调试
- spring-boot - 在 spring 中测试同步方法/块
- java - 如何在recyclerview中获取已保存帖子的当前状态
- ios - 当应用程序处于非活动状态(例如在后台播放音乐)时如何调用函数?
- reactjs - 反应原生应用程序中的反应 js 网站
- autodesk-forge - 查看器 GUI 外的 div 中的 ModelStructurePanel 实例
- c++ - 消息并不总是出现在输入错误中
- python - 安装 pycairo 时遇到问题,有什么关于下一步尝试的建议吗?
- python - 计算连续帧中两个二维坐标之间的实际速度
- python - 我正在尝试将具有多条消息的 Slack 消息的 JSON 有效负载加载到 Slack 通道