邻接表 数据结构 -更新中。。。。。
来源:互联网 发布:异步网络框架 编辑:程序博客网 时间:2024/06/05 15:55
邻接表定义:点击即可
可实现有向图和无向图
#include<iostream>#include <cstdlib>using namespace std;#define MAXVEX 100typedef int VertexType;typedef int EdgeType;typedef struct EdgeNode{ int adjvex; struct EdgeNode *next;} EdgeNode;typedef struct VextexNode{ VertexType data;//顶点域,存储顶点信息 int number;//顶点度数 int visit;//标记是否访问过此点 EdgeNode *firstedge;// 边表头指针} VextexNode, AdjList[MAXVEX];typedef struct{ AdjList adjList; int numNodes, numEdges; // 图中当前顶点数和边数} GraphAdjList;void CreateALGraph_nodir(GraphAdjList *Gp){ int i, j, k; EdgeNode *pe; cout<< "这是一个无向图。"<<endl; cout << "输入顶点数和边数:" << endl; cin >> Gp->numNodes >> Gp->numEdges; for (i = 1; i <= Gp->numNodes; i++)//初始化 { Gp->adjList[i].data=i; Gp->adjList[i].firstedge = NULL; Gp->adjList[i].number=0; } for (k = 0; k < Gp->numEdges; k++)//建表 { cout<<"第"<<k+1<< "条边的顶点序号:" << endl; cin >> i >> j; Gp->adjList[i].number++; Gp->adjList[j].number++; pe = (EdgeNode *)malloc(sizeof(EdgeNode)); pe->adjvex = j; pe->next = Gp->adjList[i].firstedge; Gp->adjList[i].firstedge = pe; pe = (EdgeNode *)malloc(sizeof(EdgeNode)); pe->adjvex = i; pe->next = Gp->adjList[j].firstedge; Gp->adjList[j].firstedge = pe; } GraphAdjList *G=Gp; for (k = 1; k <= G->numNodes; k++){ cout<<k<<"的度数为"<<G->adjList[k].number*2<<endl; cout<<"与"<<k<<"相连的顶点:"; int d=-1; while(G->adjList[k].firstedge){ if(G->adjList[k].firstedge->adjvex!=d) cout<<G->adjList[k].firstedge->adjvex<<" "; d=G->adjList[k].firstedge->adjvex; G->adjList[k].firstedge=G->adjList[k].firstedge->next; } cout<<endl; }}void CreateALGraph_dir(GraphAdjList *Gp){ int i, j, k; EdgeNode *pe; cout<<"这是一个有向图。"<<endl; cout << "输入顶点数和边数:" << endl; cin >> Gp->numNodes >> Gp->numEdges; for (i = 1; i <= Gp->numNodes; i++) { Gp->adjList[i].data=i; Gp->adjList[i].firstedge = NULL;//将边表置为空表 Gp->adjList[i].number =0;//度数标记为0 } for (k = 0; k < Gp->numEdges; k++)// 建立边表 { cout<<"第"<<k+1<< "条边的起点和终点序号:" << endl; cin >> i >> j; Gp->adjList[i].number++; Gp->adjList[j].number++; pe = (EdgeNode *)malloc(sizeof(EdgeNode)); pe->adjvex = j;// 将pe的指针指向当前顶点上指向的结点 pe->next = Gp->adjList[i].firstedge; Gp->adjList[i].firstedge = pe;// 将当前顶点的指针指向pe } GraphAdjList *G=Gp; for (k = 1; k <= G->numNodes; k++){ cout<<k<<"的度数为"<<G->adjList[k].number<<endl; cout<<"与"<<k<<"相连的顶点:"; int d=-1; while(G->adjList[k].firstedge){ if(G->adjList[k].firstedge->adjvex!=d) cout<<G->adjList[k].firstedge->adjvex<<" "; d=G->adjList[k].firstedge->adjvex; G->adjList[k].firstedge=G->adjList[k].firstedge->next; } cout<<endl; }}int main(void){ while(1){ cout<<"Please enter a directed graph or undirected graph."<<endl; cout<<"1 indicates directed graph"<<endl<<"2 indicates nodirected graph"<<endl<<"3 indicates end"<<endl;; int type; cin>>type; GraphAdjList GL; if(type==1) CreateALGraph_dir(&GL); else if(type==2) CreateALGraph_nodir(&GL); else{ cout<<"the program is end!"<<endl; break; } }return 0;}<u></u>
因为用引用传参的时候一直又问题,所以用的指针,许多功能没有分离开来。所以代码有点丑,更新中。。。。。
0 0
- 邻接表 数据结构 -更新中。。。。。
- 数据结构---邻接表(模板)
- 数据结构-邻接表 单向
- 数据结构---图---邻接表
- 数据结构中 深度优先遍历 堆 邻接表
- 【数据结构】邻接表创建_CreateALGraph
- 《数据结构》实验六-----邻接表
- 数据结构---图(邻接表)
- 数据结构 邻接链表 DFS
- 数据结构---图的邻接表
- 数据结构学习笔记 --- 图(邻接表)
- 数据结构练习题 017 图 邻接多重表
- 【数据结构】图邻接表存储实现
- 数据结构:图的实现--邻接表
- 数据结构:图的实现--邻接表
- 数据结构之图的邻接多重表
- 数据结构:图的实现--邻接表
- 《数据结构》C++代码 邻接表与邻接矩阵
- Android学习之——入门启蒙
- “流支付”掀起互联网支付创新浪潮,SAK支付白皮书揭秘
- 前端编程提高之旅(十六)----jquery中的动画
- hdu2059——龟兔赛跑
- Poj2488【深搜】
- 邻接表 数据结构 -更新中。。。。。
- Github 博客平台搭建
- IP数据报的分片与重组分析
- 邻接表的基本操作(无向网)
- Android图片相关学习
- Adapter的arrayladapter和simpleadapter使用
- 设计模式之装饰者模式(Decorator)
- 深入理解openstack网络架构(2)----Basic Use Cases
- [Objective-C] 2014.11.28