图之基于邻接链表的实现
来源:互联网 发布:通话声音分析软件 编辑:程序博客网 时间:2024/06/05 14:25
上一篇介绍了图的基本概念和基于邻接矩阵的表示方法,这一篇介绍一下基于邻接链表的实现方法
我们定义头结点作为图的顶点的存储结构,头结点包含数据域、顶点的度、邻接顶点单链表的结点指针;用单链表作为顶点的邻接顶点的存储形式,包含邻接顶点在头结点表中的下标位置权值、以及指向下一表结点的指针;
看这个图理解结构
#ifndef LINKGRAPH_H_INCLUDED#define LINKGRAPH_H_INCLUDED#define MAX_VEX 30using namespace std;typedef int InfoType;typedef char VexType;typedef string GraphKind;//表结点的定义typedef struct LinkNode{ int adjvex;//邻接结点在头结点中的下标位置; InfoType info;//权值等基本信息; struct LinkNode *nextarc;//指向下一个表结点;}LinkNode;//头结点的定义typedef struct VexNode{ VexType data; int degreen;//顶点的度 LinkNode *firstarc;}VexNode;//弧或边的定义typedef struct ArcType{ VexType vex1,vex2; InfoType info;//权值}ArcType;//图的定义typedef struct ALGraph{ GraphKind kind; int vexnum; VexNode AdjList[MAX_VEX];}ALGraph;//图的创建void Create_ALGraph(ALGraph *G){ cout<<"输入图的种类:"<<endl; string s; cin>>s; G->kind=s; G->vexnum=0;}//图的顶点定位int Locate_ALGraph_Vex(ALGraph *G,VexType *vex){ for(int k=0;k<G->vexnum;k++) { if(G->AdjList[k].data==*vex) return k; } return -1;}//图的插入顶点int AddVex_ALGraph(ALGraph *G,VexType *vex){ if(G->vexnum>=MAX_VEX) { cout<<"number overflow"<<endl; return -1; } if(Locate_ALGraph_Vex(G,vex)!=-1) { cout<<"vex has existed"<<endl; return -1; } G->AdjList[G->vexnum].data=*vex; G->AdjList[G->vexnum].degreen=0; G->AdjList[G->vexnum].firstarc=NULL; G->vexnum++; return 1;}//图的插入弧int AddArc_ALGraph(ALGraph *G,ArcType *arc){ //无向图修改一个单链表,有向图两个 int k,j; LinkNode *p,*q; k=Locate_ALGraph_Vex(G,&arc->vex1); j=Locate_ALGraph_Vex(G,&arc->vex2); if(k==-1 || j==-1) { cout<<"vex has not existed"<<endl; return -1; } p=(LinkNode *)malloc(sizeof(LinkNode)); p->adjvex=arc->vex1; p->info=arc->info; p->nextarc=NULL; q=(LinkNode *)malloc(sizeof(LinkNode)); q->adjvex=arc->vex2; q->info=arc->info; q->nextarc=NULL; // if(G->kind=="DG" || G->kind=="WDG")//有向图 { //正邻接链表,出度 q->nextarc=G->AdjList[k].firstarc; //单链表头插入法 G->AdjList[k].firstarc=q; } else { q->nextarc=G->AdjList[k].firstarc; //单链表头插入法 G->AdjList[k].firstarc=q; p->nextarc=G->AdjList[j].firstarc; G->AdjList[j].firstarc=p; } return 1;}#endif // LINKGRAPH_H_INCLUDED
测试代码:
cout<<"ALGraph : "<<endl; ALGraph *G; VexType vexs[4]={'A','B','C','D'}; ArcType arc; G=(ALGraph *)malloc(sizeof(ALGraph)); Create_ALGraph(G); for(int i=0;i<4;i++) { AddVex_ALGraph(G,&vexs[i]); } for(int i=0;i<4;i++) { arc.info=i+1; arc.vex1=vexs[i]; arc.vex2=vexs[(i+1)%4]; AddArc_ALGraph(G,&arc); } for(int i=0;i<4;i++) { cout<<G->AdjList[i].data<<" "; } cout<<endl; for(int i=0;i<4;i++) { LinkNode *p; p=(LinkNode *)malloc(sizeof(LinkNode)); p=G->AdjList[i].firstarc; cout<<G->AdjList[i].data<<"度:"<<G->AdjList[i].degreen<<" 邻接结点:"<<endl; while(p!=NULL) { cout<<G->AdjList[p->adjvex].data<<",权值: "<<p->info<<endl; p=p->nextarc; } cout<<endl; }
阅读全文
0 0
- 图之基于邻接链表的实现
- 图的广度优先遍历,基于邻接链表实现
- Java实现基于邻接表的图的建立
- Java实现基于邻接表的图的几个应用
- 图结构系列—基于邻接表的图实现
- 图的深度优先遍历,基于邻接链表的非递归实现
- 基于邻接链表的图的广度优先搜索Java实现
- 基于邻接链表和邻接矩阵实现图的各种常用函数C++
- 图的邻接链表实现
- 图的邻接表实现
- 图的邻接表实现
- 图的邻接表实现
- 图基于邻接表的遍历
- 图的遍历(基于邻接表)
- 图的基本操作(基于邻接表
- 图的存储之邻接表--C++ vector实现
- 图论之邻接表的数组实现
- 邻接链表实现图
- Angular 组件之间如何通信?
- Java代码质量检查工具及使用案例
- JavaScript 实例:点击漫天小星星 创建标签元素并加到父标签下
- 2进制/8进制/16进制 表示方法/Heredoc 结构/Nowdoc 结构
- SSL握手过程实例分析
- 图之基于邻接链表的实现
- 国际化
- UE4 解决动画模型“缺胳膊少腿”
- IntelliJ IDEA Maven项目为什么不能右键新建Java文件的解决办法!!
- javascript 对象属性的添加,删除,json对象和字符串转换方法等
- 2017年中国北京国际妇女儿童产业博览会会刊(参展商名录)
- 狗年宝宝起名大全:为宝宝起名的一些要点介绍
- Request 中nonceStr (随机数)的产生
- 关于点击显示与隐藏的事件