C:求图的深度和广度遍历严蔚敏版[新手向]⑨

C:求图的深度和广度遍历严蔚敏版[新手向]⑨

近段时间本人在复习数据结构,重新敲一次代码,有什么不足的地方望大佬多多指教,严蔚敏版教程配套算法代码

#include<stdio.h>
#include<stdlib.h>

#define MaxVex 255
#define TRUE   1
#define FALSE  0

typedef char VertexType;
typedef int Bool;
Bool visited[MaxVex];

typedef struct EdgeNode {
	int adjvex;
	struct EdgeNode *next;
} EdgeNode;

typedef struct VertexNode {
	VertexType data;
	EdgeNode *firstedge;
} VertexNode,AdjList[MaxVex];

typedef struct Graph {
	AdjList adjList;
	int numVertexes,numEdges;
} Graph,*GraphAdjList;



typedef struct LoopQueue {
	int data[MaxVex];
	int front,rear;
} LoopQueue,*Queue;

void initQueue(Queue &Q) {
	Q->front=Q->rear=0;
}

Bool QueueEmpty(Queue &Q) {
	if(Q->front == Q->rear) {
		return TRUE;
	} else {
		return FALSE;
	}
}

Bool QueueFull(Queue &Q) {
	if((Q->rear+1)%MaxVex == Q->front) {
		return TRUE;
	} else {
		return FALSE;
	}
}



void EnQueue(Queue &Q,int e) {
	if(!QueueFull(Q)) {
		Q->data[Q->rear] = e;
		Q->rear = (Q->rear+1)%MaxVex;
	}
}



void DeQueue(Queue &Q,int *e) {
	if(!QueueEmpty(Q)) {
		*e = Q->data[Q->front];
		Q->front = (Q->front+1)%MaxVex;
	}
}



void CreateALGraph(GraphAdjList &G) {
	int i, j, k;
	if(G==NULL) {
		G = (GraphAdjList)malloc(sizeof(Graph));
	}

	printf("输入图的结点数以及边数: ");
	scanf("%d%d",&G->numVertexes,&G->numEdges);
	fflush(stdin);

	printf("===========================\n");
	printf("输入各个顶点的数据:\n");
	for (i=0; i<G->numVertexes; ++i) {
		printf("顶点%d: ",i+1);
		scanf("%c", &(G->adjList[i].data));
		G->adjList[i].firstedge = NULL;
		fflush(stdin);
	}

	printf("===========================\n");
	for (k=0; k<G->numEdges; ++k) {
		printf("输入(vi,vj)上的顶点序号: ");
		scanf("%d%d",&i,&j);

		EdgeNode *ptrEdgeNode = (EdgeNode*)malloc(sizeof(EdgeNode));
		ptrEdgeNode->adjvex = j;
		ptrEdgeNode->next = G->adjList[i].firstedge;
		G->adjList[i].firstedge = ptrEdgeNode;

		ptrEdgeNode = (EdgeNode*)malloc(sizeof(EdgeNode));
		ptrEdgeNode->adjvex = i;
		ptrEdgeNode->next = G->adjList[j].firstedge;
		G->adjList[j].firstedge = ptrEdgeNode;
	}
}

void DFS(GraphAdjList &G, int i) {
	visited[i] = TRUE;
	printf("%c ", G->adjList[i].data);

	EdgeNode *p = G->adjList[i].firstedge;
	while(p) {
		if(!visited[p->adjvex]) {
			DFS(G,p->adjvex);
		}
		p= p->next;
	}
}



void DFSTraverse(GraphAdjList &G) {
	int i;
	for (i=0; i<G->numVertexes; ++i) {
		visited[i] = FALSE;
	}
	for (i=0; i<G->numVertexes; ++i) {
		if(!visited[i]) {
			DFS(G,i);
		}
	}
}


void BFSTraverse(GraphAdjList &G) {
	int i;
	Queue Q = (Queue)malloc(sizeof(LoopQueue));

	for (i=0; i<G->numVertexes; ++i) {
		visited[i] = FALSE;
	}
	initQueue(Q);

	for (i=0; i<G->numVertexes; ++i) {
		if(!visited[i]) {
			visited[i] = TRUE;
			printf("%c ", G->adjList[i].data);
			EnQueue(Q, i);

			while (!QueueEmpty(Q)) {
				DeQueue(Q, &i);
				EdgeNode *p = G->adjList[i].firstedge;
				while (p) {
					if (!visited[p->adjvex]) {
						visited[p->adjvex] = TRUE;
						printf("%c ", G->adjList[p->adjvex].data);
						EnQueue(Q, p->adjvex);
					}
					p = p->next;
				}
			}
		}
	}
}


int main() {
	GraphAdjList G = NULL;

	CreateALGraph(G);

	printf("\n图的深度优先遍历为: ");
	DFSTraverse(G);

	printf("\n图的广度优先遍历为: ");
	BFSTraverse(G);

	printf("\n");

	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值