《邻接表存储结构建立无向图》由会员分享,可在线阅读,更多相关《邻接表存储结构建立无向图(3页珍藏版)》请在人人文库网上搜索。
1、算法功能:采用邻接表存储结构建立无向图 #include #include #define OK 1#define NULL 0#define MAX_VERTEX_NUM 20 / 最大顶点数typedef int Status; / 函数的类型,其值是函数结果状态代码typedef char VertexType;typedef int VRType;typedef int InforType;typedef struct ArcNodeint adjvex;/该边所指的顶点的位置struct ArcNode *nextarc; / 指向下一条边的指针 int weight; /边的权Ar。
2、cNode; / 表的结点typedef struct VNodeVertexType data; /顶点信息(如数据等)ArcNode *firstarc; /指向第一条依附该顶点的边的弧指针VNode, AdjListMAX_VERTEX_NUM;/ 头结点typedef struct ALGraphAdjList vertices;int vexnum, arcnum; / 图的当前顶点数和弧数ALGraph;/返回顶点 v 在顶点向量中的位置int LocateVex(ALGraph G , char v)int i;for(i = 0; v != G .verticesi.data 。
3、& i = G .vexnum) return -1;return i;/构造邻接链表Status CreateUDN(ALGraph &G)int j;ArcNode *s, *t;printf(”输入无向图顶点数:”); scanf(%d, &G .vexnum); printf(输入无向图边数:); scanf(%d, &G .arcnum); getchar();for(i nt i =:0; i adjvex = j;/该边所指向的顶点的位置为js-n extarc = G.verticesi.firstarc;G.verticesi.firstarc =s;t-adjvex = i;该边所指向的顶点的位置为jt-n extarc = G.verticesj.firstarc;G.verticesj.firstarc =t; return OK;Status PrintAdjList(ALGraph &G)ArcNode *p;printf(%4s%6s%12sn,编号,顶点,相邻边编号”); for(int i = 0; i n extarc)prin tf(%4d, p-adjvex);prin tf(n);return OK;int mai n()ALGraph G; CreateUDN(G); rin tAdjList(G); return 0;欢迎下载3。