当前位置: 首页 > news >正文

图——邻接表基本操作算法实现

一、存储结构

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
//邻接表
#define MAXVertexNum 100	//最大顶点数
typedef char VerTexType;
typedef int ArcType;
typedef int Otherinfo;typedef struct ArcNode{    //边表结点 int adjvex;  //该边所指向的顶点的位置 struct ArcNode *nextarc;//指向下一条边的指针 Otherinfo weight;  //网的边权值 
}ArcNode;typedef struct VNode{    //顶点结点VerTexType data;  //顶点信息ArcNode *firstarc;   //指向第一条依附该顶点的边的指针 
}VNode,AdjList[MAXVertexNum];//AdjList表示邻接表类型typedef struct{    AdjList vertices;    //邻接表int vexnum,arcnum;    //图的当前顶点数和弧数bool isUndirected;
}ALGraph;

二、初始化操作

//初始化图
void InitGraph(ALGraph &G, bool isUndirected=1) {G.vexnum = 0;      // 初始化顶点数为 0G.arcnum = 0;      // 初始化边数为 0G.isUndirected = isUndirected;  // 设置图的类型(有向图或无向图)// 初始化每个顶点的边表为空for (int i = 0; i < MAXVertexNum; i++) {G.vertices[i].firstarc = NULL;  // 每个顶点的第一条边指针都设置为 NULL}
}

三、查找图中指定顶点的位置

/*在图的顶点数组 vertices 中遍历所有顶点,查找与顶点 v 相等的顶点。如果找到了,就返回该顶点的索引;如果没有找到该顶点,则返回 -1*/

int LocateVertex(ALGraph G, VerTexType v) {for (int i = 0; i < G.vexnum; i++) {if (G.vertices[i].data == v) return i;}return -1;
}

四、插入顶点操作

void InsertVertex(ALGraph &G, VerTexType v) {if (G.vexnum >= MAXVertexNum) return;  // 如果顶点数已满,返回// 将顶点 v 插入到顶点表中G.vertices[G.vexnum].data = v;G.vertices[G.vexnum].firstarc = NULL;  // 插入的新顶点没有边G.vexnum++;  // 顶点数加 1
}

五、插入边操作

void AddEdge(ALGraph &G, VerTexType v1, VerTexType v2, Otherinfo weight=1) {int i = LocateVertex(G, v1);  // 查找顶点 v1 的位置int j = LocateVertex(G, v2);  // 查找顶点 v2 的位置if (i == -1 || j == -1) return;  // 如果没有找到顶点,返回// 创建新的边结点 ArcNode *p = (ArcNode *)malloc(sizeof(ArcNode));p->adjvex = j;  // 记录边指向的顶点p->weight = weight;  // 设置边的权重p->nextarc = G.vertices[i].firstarc;  // 新边指向原来第一个边G.vertices[i].firstarc = p;  // 更新顶点 v1 的第一条边指针// 如果是无向图,则插入反向的边if (G.isUndirected) {ArcNode *q = (ArcNode *)malloc(sizeof(ArcNode));q->adjvex = i;q->weight = weight;q->nextarc = G.vertices[j].firstarc;G.vertices[j].firstarc = q;}G.arcnum++;  // 边数加 1
}

六、删除边操作

/* 
与链表不同在于这里传入的参数是图,类型是ALGrapg结构体 
而链表传入的是头指针,类型是指针结构 LinkList=Lnode*
InsertList(LinkList &L) InsertList(L) ===  InsertList(LinkList *L) InsertList(&L) ===  InsertList(LNode **L) InsertList(&L)
由于并不能直接在 RemoveEdge传入某个顶点的头指针
因此 在函数体内部 只能采用  ArcNode *(*p) = &G.vertices[i].firstarc;
*/

//删除边 
void RemoveEdge(ALGraph &G, VerTexType v1, VerTexType v2) {  int i = LocateVertex(G, v1);int j = LocateVertex(G, v2);if (i == -1 || j == -1) return;// 查找并删除 v1 -> v2 的边ArcNode **p = &G.vertices[i].firstarc;  //*p接受firstarc这个指针的地址 while (*p) {if ((*p)->adjvex == j) {ArcNode *temp = *p;*p = temp->nextarc;free(temp);G.arcnum--;  // 边数减 1 break;}p = &(*p)->nextarc;}// 如果是无向图,还需要删除反向边if (G.isUndirected) {p = &G.vertices[j].firstarc;while (*p) {if ((*p)->adjvex == i) {ArcNode *temp = *p;*p = temp->nextarc;free(temp);break;}p = &(*p)->nextarc;}}}

七、删除顶点操作

// 删除顶点 
void DeleteVertex(ALGraph &G, VerTexType v) {int idx = LocateVertex(G, v);if (idx == -1) return;// 删除有向图中所有指向该顶点的边,无向图中双方向的边 for (int i = 0; i < G.vexnum; i++) {if (i == idx) continue;RemoveEdge(G, G.vertices[i].data, v);}// 删除有向图中该顶点的边if (G.isUndirected==0) {ArcNode *p = G.vertices[idx].firstarc;  //此处不动顶点结点,可以不动用&G while (p) {ArcNode *temp = p;p = p->nextarc;free(temp);G.arcnum--;}}// 移动顶点数组for (int i = idx; i < G.vexnum - 1; i++) {G.vertices[i] = G.vertices[i + 1];}G.vexnum--;// 更新所有边的邻接点索引for (int i = 0; i < G.vexnum; i++) {ArcNode *p = G.vertices[i].firstarc;while (p) {if (p->adjvex > idx) {p->adjvex--;}p = p->nextarc;}}
}

八、列出图中节点 x 的邻接的边 

// 列出图中节点 x 的邻接的边 
void Neighbors(ALGraph G, VerTexType v) {int i = LocateVertex(G, v);if (i == -1) return;printf("邻接的边 %c: ", v);ArcNode *p = G.vertices[i].firstarc;while (p) {if(G.isUndirected==0) printf("<%c,%c> ", v,G.vertices[p->adjvex].data);if(G.isUndirected==1) printf("(%c,%c) ", v,G.vertices[p->adjvex].data);p = p->nextarc;}if(G.isUndirected==0){for (int j = 0; j < G.vexnum; j++) {if (j == i) continue;ArcNode *p = G.vertices[j].firstarc;  while (p) {if (p->adjvex == i) {printf("<%c,%c>", G.vertices[j].data,v);}p = p->nextarc;}}	}printf("\n");
}

九、输出查看邻接矩阵

//输出操作 
void DisplayGraph(ALGraph G) {for (int i = 0; i < G.vexnum; i++) {printf("%c: ", G.vertices[i].data);ArcNode *p = G.vertices[i].firstarc;while (p) {printf("-> %c(%d) ", G.vertices[p->adjvex].data, p->weight);p = p->nextarc;}printf("\n");}
}

十、主函数

int main() {ALGraph G;InitGraph(G,0); InsertVertex(G, 'A');InsertVertex(G, 'B');InsertVertex(G, 'C');InsertVertex(G, 'D');InsertVertex(G, 'E');AddEdge(G, 'A', 'B');AddEdge(G, 'A', 'D');AddEdge(G, 'A', 'C');AddEdge(G, 'C', 'A');AddEdge(G, 'B', 'E');AddEdge(G, 'C', 'D');DisplayGraph(G);printf("\n边数: %d\n", G.arcnum);Neighbors(G,'C');DeleteVertex(G, 'C');printf("\n删除C后:\n");DisplayGraph(G);printf("\n边数: %d\n", G.arcnum);return 0;
}
http://www.lryc.cn/news/603007.html

相关文章:

  • USRP X410 X440 5G及未来通信技术的非地面网络(NTN)
  • 代码解读:微调Qwen2.5-Omni 实战
  • 《Go Web编程实战派--从入门到精通》的随笔笔记
  • LLM Landscape:2025年大语言模型概览
  • 数据处理工具是做什么的?常见数据处理方法介绍
  • ethers.js基础(学习路线清单)
  • 正向代理和反向代理的理解
  • 从“PPT动画”到“丝滑如德芙”——uni-app x 动画性能的“终极奥义”
  • AI 驱动、设施扩展、验证器强化、上线 EVM 测试网,Injective 近期动态全更新!
  • clock_getres系统调用及示例
  • PyTorch中flatten()函数详解以及与view()和 reshape()的对比和实战代码示例
  • 【代码解读】通义万相最新视频生成模型 Wan 2.2 实现解析
  • AR技术赋能工业设备维护:效率与智能的飞跃
  • 一个典型的微控制器MCU包含哪些模块?
  • 安宝特方案丨AI算法能力开放平台:适用于人工装配质检、点检、实操培训
  • Java学习-----如何创建线程
  • 基于黑马教程——微服务架构解析(二):雪崩防护+分布式事务
  • Qt:盒子模型的理解
  • 2025.7.28总结
  • 嵌入式分享合集186
  • JavaScript 回调函数讲解_callback
  • 关于xshell的一些基本内容讲解
  • tsc命令深入全面讲解
  • jQuery 最新语法大全详解(2025版)
  • python对象的__dict__属性详解
  • 防水医用无人机市场报告:现状、趋势与洞察
  • Java 笔记 serialVersionUID
  • 分布式IO详解:2025年分布式无线远程IO采集控制方案选型指南
  • 生物信息学数据技能-学习系列001
  • 秒级构建消息驱动架构:描述事件流程,生成 Spring Cloud Stream+RabbitMQ 代码