邻接表的深度优先遍历以及广度优先遍历

#include <stdio.h>
#include <stdlib.h>
#define MAX 20

int visited[20];

typedef struct ArcNode{
	int adjvex;
	struct ArcNode *nextarc;
}ArcNode,*LinkNode;
typedef struct{
	char data;
	ArcNode *firstarc;
}VNode,AdjList[MAX];
typedef struct{
	AdjList vexs;
	int vexnum,arcnum;
}ALGraph;
typedef struct{
	LinkNode *base;
	int top;
	int stacksize;
}SqStack;
typedef struct{
	LinkNode *Qbase;
	int front,rear;
}SqQueue;

int FirstAdj(ALGraph G, char v){
	int i;
	
	for(i = 0; i < G.vexnum; i ++){
		if(G.vexs[i].data ==  v)
			return i;	
	}
	
	return -1;
}

void CreateALGraphList(ALGraph &G){
	int i,j,k;
	char v1,v2;
	ArcNode *p,*s;
	
	printf("请输入节点数目和边的数目:\n");
	scanf("%d%d",&G.vexnum,&G.arcnum);
	
	getchar();
	printf("请输入各个顶点:\n");
	for(i = 0; i < G.vexnum; i ++){
		scanf("%c",&G.vexs[i].data);
		G.vexs[i].firstarc = NULL;
	}
	
	printf("请输入各个顶点连成的边:\n");
	for(i = 0; i < G.arcnum; i ++){
		getchar();
		scanf("%c%c",&v1,&v2);
		
		j = FirstAdj(G,v1);		k = FirstAdj(G,v2);
		
		s = (ArcNode *)malloc(sizeof(ArcNode));
		s->adjvex = k;		s->nextarc = NULL;
		
		if(!G.vexs[j].firstarc){
			G.vexs[j].firstarc = s;
		}
		else{
			p = G.vexs[j].firstarc;
			
			while(p->nextarc)
				p = p->nextarc;
			
			p->nextarc = s; 
		}
	}
}

void InitStack(SqStack &S){
	S.base = (LinkNode *)malloc(sizeof(LinkNode) * MAX);
	
	if(!S.base)
		return ;
	
	S.top = 0;
	S.stacksize = MAX;
}

int StackEmpty(SqStack S){
	if(!S.top)
		return 1;
	
	return 0;
}

void Push(SqStack &S, LinkNode p){
	if(S.top >= S.stacksize){
		S.base = (LinkNode *)realloc(S.base,(S.top + MAX) * sizeof(LinkNode));
		
		if(!S.base)
			return ;
		
		S.stacksize += MAX;
	}
	
	S.base[S.top ++] = p;
}

void Pop(SqStack &S, LinkNode &p){
	if(!S.top)
		return ;
	
	p = S.base[-- S.top];
}

void DFS(ALGraph G, int i){
	LinkNode p;
	
	printf("%3c",G.vexs[i].data);
	visited[i] = 1;
	
	for(p = G.vexs[i].firstarc; p; p = p->nextarc){
		if(!visited[p->adjvex])
			DFS(G,p->adjvex);
	}
}

void DFSTraverse(ALGraph G){
	int i;
	
	for(i = 0; i < G.vexnum; i ++){
		if(!visited[i])
			DFS(G,i);
	}
}

void DFS1(ALGraph G, int i){
	SqStack S;
	ArcNode *p;
	int k;
	
	InitStack(S);
	printf("%3c",G.vexs[i].data);
	visited[i] = 1;
	p = G.vexs[i].firstarc;
	
	while(p || !StackEmpty(S)){
		while(p){
			if(visited[p->adjvex])
				p = p->nextarc;
			else{
				printf("%3c",G.vexs[p->adjvex].data);
				visited[p->adjvex] = 1;
				Push(S,p);
				p = G.vexs[p->adjvex].firstarc;
			}
		}
		
		if(!StackEmpty(S)){
			Pop(S,p);
			p = p->nextarc;
		}
	}
	
}

void DFSTraverse1(ALGraph G){
	int i;
	
	for(i = 0; i < G.vexnum; i ++)
		visited[i] = 0;
	
	for(i = 0; i < G.vexnum; i ++){
		if(!visited[i])
			DFS1(G,i);
	}
}

void InitQueue(SqQueue &Q){
	Q.Qbase = (LinkNode *)malloc(sizeof(LinkNode) * MAX);
	
	if(!Q.Qbase)
		return ;
	
	Q.front = Q.rear = 0;
	
}

void EnQueue(SqQueue &Q, LinkNode p){
	if((Q.rear + 1) % MAX == Q.front)
		return ;
	
	Q.Qbase[Q.rear] = p;
	
	Q.rear = (Q.rear + 1) % MAX;
}

void DeQueue(SqQueue &Q, LinkNode &p){
	if(Q.front == Q.rear )
		return ;
	
	p = Q.Qbase[Q.front];
	Q.front = (Q.front + 1) % MAX;
	
}

int QueueEmpty(SqQueue Q){
	if(Q.front == Q.rear)
		return 1;
	
	return 0;
}

void BFS(ALGraph G, int i){
	LinkNode p;
	SqQueue Q;
	
	printf("%3c",G.vexs[i].data);
	visited[i] = 1;
	InitQueue(Q);
	p = G.vexs[i].firstarc;
	
	while(p || !QueueEmpty(Q)){
		while(p){
			if(visited[p->adjvex])
				p = p->nextarc;
			else{
				printf("%3c",G.vexs[p->adjvex].data);
				visited[p->adjvex] = 1;
				EnQueue(Q,p);
				p = p->nextarc;
			}
		}
		if(!QueueEmpty(Q)){
			DeQueue(Q,p);
			p = G.vexs[p->adjvex].firstarc;
		}
	}
}

void BFSTraverse(ALGraph G){
	int i;
	
	for(i = 0; i < G.vexnum; i ++)
		visited[i] = 0;
	
	for(i = 0; i < G.vexnum; i ++)
		if(!visited[i])
			BFS(G,i);
}

int main(){
	ALGraph G;
	
	CreateALGraphList(G);
	
	DFSTraverse(G);
	putchar('\n');
	
	DFSTraverse1(G);
	putchar('\n');
	
	BFSTraverse(G);
	
	return 0;
}

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值