c语言邻接表的构建_C/C++知识点之C语言建立有向图的邻接表及其遍历操作

本文主要向大家介绍了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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
邻接表是图的一种常见存储方式,一般用链表实现。下面是C语言邻接表建立图并遍历的示例代码: ```c #include <stdio.h> #include <stdlib.h> #define MAX_VERTEX_NUM 20 // 顶数的最大值 // 邻接表结构体定义 typedef struct ArcNode { int adjvex; // 指向的顶编号 struct ArcNode *next; // 指向下一个边的指针 } ArcNode; typedef struct VertexNode { int data; // 顶的数据 ArcNode *firstarc; // 指向第一条依附该顶的边的指针 } VertexNode, AdjList[MAX_VERTEX_NUM]; typedef struct Graph { AdjList vertices; // 邻接表数组 int vexnum, arcnum; // 图的顶数和边数 } Graph; // 初始化图 void InitGraph(Graph *G, int n) { G->vexnum = n; G->arcnum = 0; for (int i = 0; i < n; ++i) { G->vertices[i].data = i; // 顶的数据为编号 G->vertices[i].firstarc = NULL; // 初始化边表为空 } } // 插入边 void InsertArc(Graph *G, int v, int w) { ArcNode *p = (ArcNode *) malloc(sizeof(ArcNode)); p->adjvex = w; p->next = G->vertices[v].firstarc; G->vertices[v].firstarc = p; ++G->arcnum; } // 深度优先遍历 void DFS(Graph *G, int v, int *visited) { printf("%d ", v); visited[v] = 1; ArcNode *p = G->vertices[v].firstarc; while (p) { if (!visited[p->adjvex]) { DFS(G, p->adjvex, visited); } p = p->next; } } // 广度优先遍历 void BFS(Graph *G, int v, int *visited) { int queue[MAX_VERTEX_NUM], front = 0, rear = 0; printf("%d ", v); visited[v] = 1; queue[rear++] = v; while (front < rear) { int w = queue[front++]; ArcNode *p = G->vertices[w].firstarc; while (p) { if (!visited[p->adjvex]) { printf("%d ", p->adjvex); visited[p->adjvex] = 1; queue[rear++] = p->adjvex; } p = p->next; } } } int main() { Graph G; int n = 5; // 图的顶数 InitGraph(&G, n); InsertArc(&G, 0, 1); InsertArc(&G, 0, 2); InsertArc(&G, 1, 2); InsertArc(&G, 1, 3); InsertArc(&G, 2, 1); InsertArc(&G, 2, 3); InsertArc(&G, 2, 4); InsertArc(&G, 3, 4); printf("DFS: "); int visited[MAX_VERTEX_NUM] = {0}; DFS(&G, 0, visited); printf("\n"); printf("BFS: "); for (int i = 0; i < n; ++i) { visited[i] = 0; } BFS(&G, 0, visited); printf("\n"); return 0; } ``` 上述代码中,`Graph` 结构体表示邻接表存储的图,其中 `AdjList` 数组是邻接表数组,每个元素都是 `VertexNode` 结构体,表示一个。`ArcNode` 结构体表示一条边,每个顶都有一个链表,其中存储了以该顶为起的所有边。`InitGraph` 函数用于初始化一个空图,`InsertArc` 函数用于插入一条边。`DFS` 和 `BFS` 分别表示深度优先遍历和广度优先遍历。在遍历时,需要使用一个 `visited` 数组来记录每个顶是否已经被访问过。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值