(深度/广度优先算法)——遍历邻接表(C语言)

一、算法代码

//采用邻接表表示图的遍历
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define MAXSIZE 100
typedef int VerTexType;
int visited[MAXSIZE];			//访问数组
typedef struct ArcNode{
   			//边结点
	int adjvex;					//结点位置
	ArcNode *nextarc;			//指向下一结点的指针
}ArcNode;
typedef struct VNode{
   			//头结点
	VerTexType data;			//顶点信息
	ArcNode *firstarc;			//指向第一条依附该顶点的边的指针
}Vnode;
typedef struct{
   
	Vnode verxs[MAXSIZE];		//顶点表
	int numNodes,numEdqes;		//顶点数和边数
}AGraph;
AGraph* CreatAGraph();
void DFSTraverse(AGraph G);		//深度优先遍历
void BFSTraverse(AGraph G);		//广度优先遍历
int main(){
   
	AGraph * G;
	printf("\n---采用邻接表表示图的遍历---\n\n");
	G=CreatAGraph();
	printf("\n-----------邻接表深度优先搜索遍历序列---------------\n\n");
	DFSTraverse(*G);
	printf("\n-----------邻接表广度优先搜索遍历序列---------------\n\n");
	BFSTraverse(*G);
	printf("\n\n");
	system("pause");
	return 0;
}


//--------------------------链队列定义-------------------------------------------------
//链队列的结构
typedef int QElemType;

typedef struct QNode{
    //结点结构
	QElemType  data;
	struct QNode *next;
}QNode,
  • 3
    点赞
  • 64
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是深度优先遍历广度优先遍历有向邻接表C语言算法代码深度优先遍历: ```c #include <stdio.h> #include <stdlib.h> #define MAX_VERTEX_NUM 20 typedef struct ArcNode { int adjvex; struct ArcNode *nextarc; } ArcNode; typedef struct VNode { int data; ArcNode *firstarc; } VNode, AdjList[MAX_VERTEX_NUM]; typedef struct { AdjList vertices; int vexnum, arcnum; } ALGraph; int visited[MAX_VERTEX_NUM]; void CreateGraph(ALGraph *G) { printf("请输入顶点数和边数:"); scanf("%d%d", &G->vexnum, &G->arcnum); printf("请输入顶点信息:"); for (int i = 0; i < G->vexnum; i++) { scanf("%d", &G->vertices[i].data); G->vertices[i].firstarc = NULL; } printf("请输入边的信息:\n"); for (int k = 0; k < G->arcnum; k++) { int i, j; printf("请输入边(vi, vj)的顶点序号:"); scanf("%d%d", &i, &j); ArcNode *p = (ArcNode *)malloc(sizeof(ArcNode)); p->adjvex = j; p->nextarc = G->vertices[i].firstarc; G->vertices[i].firstarc = p; } } void DFS(ALGraph *G, int v) { visited[v] = 1; printf("%d ", G->vertices[v].data); ArcNode *p = G->vertices[v].firstarc; while (p) { if (!visited[p->adjvex]) { DFS(G, p->adjvex); } p = p->nextarc; } } void DFSTraverse(ALGraph *G) { for (int i = 0; i < G->vexnum; i++) { visited[i] = 0; } for (int i = 0; i < G->vexnum; i++) { if (!visited[i]) { DFS(G, i); } } } int main() { ALGraph G; CreateGraph(&G); printf("深度优先遍历结果为:"); DFSTraverse(&G); return 0; } ``` 广度优先遍历: ```c #include <stdio.h> #include <stdlib.h> #define MAX_VERTEX_NUM 20 #define MAX_QUEUE_SIZE 20 typedef struct ArcNode { int adjvex; struct ArcNode *nextarc; } ArcNode; typedef struct VNode { int data; ArcNode *firstarc; } VNode, AdjList[MAX_VERTEX_NUM]; typedef struct { AdjList vertices; int vexnum, arcnum; } ALGraph; int visited[MAX_VERTEX_NUM]; typedef struct { int data[MAX_QUEUE_SIZE]; int front, rear; } Queue; void InitQueue(Queue *Q) { Q->front = Q->rear = 0; } int QueueEmpty(Queue *Q) { return Q->front == Q->rear; } void EnQueue(Queue *Q, int x) { Q->data[Q->rear++] = x; } int DeQueue(Queue *Q) { return Q->data[Q->front++]; } void CreateGraph(ALGraph *G) { printf("请输入顶点数和边数:"); scanf("%d%d", &G->vexnum, &G->arcnum); printf("请输入顶点信息:"); for (int i = 0; i < G->vexnum; i++) { scanf("%d", &G->vertices[i].data); G->vertices[i].firstarc = NULL; } printf("请输入边的信息:\n"); for (int k = 0; k < G->arcnum; k++) { int i, j; printf("请输入边(vi, vj)的顶点序号:"); scanf("%d%d", &i, &j); ArcNode *p = (ArcNode *)malloc(sizeof(ArcNode)); p->adjvex = j; p->nextarc = G->vertices[i].firstarc; G->vertices[i].firstarc = p; } } void BFS(ALGraph *G, int v) { Queue Q; InitQueue(&Q); visited[v] = 1; printf("%d ", G->vertices[v].data); EnQueue(&Q, v); while (!QueueEmpty(&Q)) { int u = DeQueue(&Q); ArcNode *p = G->vertices[u].firstarc; while (p) { int w = p->adjvex; if (!visited[w]) { visited[w] = 1; printf("%d ", G->vertices[w].data); EnQueue(&Q, w); } p = p->nextarc; } } } void BFSTraverse(ALGraph *G) { for (int i = 0; i < G->vexnum; i++) { visited[i] = 0; } for (int i = 0; i < G->vexnum; i++) { if (!visited[i]) { BFS(G, i); } } } int main() { ALGraph G; CreateGraph(&G); printf("广度优先遍历结果为:"); BFSTraverse(&G); return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值