本文主要向大家介绍了C/C++知识点之C语言建立有向图的邻接表及其遍历操作,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。
1 /**/
2 #include"stdio.h"
3 #include"stdlib.h"
4 //图的邻接矩阵储存结构
5 typedef char elemtype;
6 #define maxsize 10
7 #define queuesize 100
8 //边结点的类型定义
9 typedef struct edgenode
10 {
11 int adjvex;//存放邻接的点在顶点表的下标,邻接点
12 struct edgenode *next;//指向Vi下一个邻接点的边结点
13 int weight;/*权值*/
14 }edgenode;
15 //顶点结点类型定义
16 typedef struct vexnode
17 {
18 elemtype data; //存储顶点的名称或其相关信息
19 edgenode *firstedge;//边表头指针
20 }vexnode;
21 //图的邻接表数据类型
22 typedef struct{
23 vexnode vexlist[maxsize];//顶点表
24 int n,e;
25 }graph;
26 //在图g中查找顶点v,存在顶点数组中的下标,不存在返回-1
27 int locatevex(graph g,elemtype v)
28 {
29 int i;
30 for(i=0;i
31 if(g.vexlist[i].data==v)return i;
32 return -1;
33 }
34 //打印图信息
35 void print(graph g)
36 {
37 int i;
38 edgenode *p;
39 printf("图的邻接表表示:");
40 for(i=0;i
41 printf("\n%4c",g.vexlist[i].data);
42 p=g.vexlist[i].firstedge;
43 while(p!=NULL){
44 printf("-->%d",p->adjvex);p=p->next;
45 }
46 }
47 printf