图——邻接表基本操作算法实现
一、存储结构
#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;
}