数据结构邻接表的建立以及两种遍历


静下心,去思考,思路便会清晰,程序基本框架便会浮现在脑海中微笑

#include "stdio.h"
#include "stdlib.h"
#define MAX_VEX_NUM 20
#define OK 1
#define ERROR 0
typedef int Status;
int visited[20];


第一部分   存储结构

typedef struct ArcNode {
	int adjvex;//用于存储数组下标
	struct ArcNode * nextarc;
}ArcNode;//表结点存储结构体
typedef struct VexNode {
	int data;
	ArcNode *firstarc;
}VexNode,AdiList[MAX_VEX_NUM];//顶点存储结构体,AdiList可用于定义一个存储该结构体类型数据的数组
typedef struct {
	AdiList vexs;//顶点数组
	int vexnum,arcnum;
	char kind;//图标志种类
}ALGraph;//邻接表结构体


 

第二部分  邻接表的创立

int LocateVex (ALGraph *G,int vex) {
	int i,t;
	for (i=0; i<G->vexnum; i++) {
		if(G->vexs[i].data == vex) {
			t=i;
			break;
		}
		else t = G->vexnum;
	}
	if (t == G->vexnum) exit(0);
	return t;//t = G.vexnumt作为值返回时,表示定位失败
}//定位操作,已知顶点元素,求出对应数组下标,并返回
Status InsertChild (ALGraph *G,int i,int j){
	ArcNode *C;
	C = (ArcNode *) malloc (sizeof(ArcNode));
	C->adjvex=j;
	C->nextarc=G->vexs[i].firstarc;
	G->vexs[i].firstarc=C;
	return OK;
}//插入边,形成孩子链
Status CreateALGraph (ALGraph *G){
	//int CreateDG (ALGraph *);//函数申明
	//int CreateDN (ALGraph *);//函数申明
	int CreateUDG (ALGraph *);//函数申明
	//int CreateUDN (ALGraph *);//函数申明
	printf ("Please choose kind of graph(1DG/2DN/3UDG/4UDN).\n");
	scanf ("%c",&G->kind);
	switch (G->kind){
	//case '1': CreateDG(G); break;
	//case '2': CreateDN(G); break;
	case '3': CreateUDG(G); break;//此处G为指针
	//case '4': CreateUDN(G); break;
	default : return ERROR;
	}
	return OK;
}
int CreateUDG (ALGraph * G){
	int i,j,v1,v2;
	printf ("Please enter values for G.vexnum and G.arcnum\nExample : 5,6\n");
	scanf ("%d,%d",&G->vexnum,&G->arcnum);
	printf ("Please enter values for vexs[]\n");
	for (i=0; i<G->vexnum; i++){
		scanf ("%d",&G->vexs[i].data);
		G->vexs[i].firstarc=NULL;//初始化为空指针
	}
	printf ("Please enter ChildList\nExample : '1-2' enter '1,2', enter  '0,0'  to over\n");
	scanf ("%d,%d",&v1, &v2);
	while (v1 != 0 && v2 != 0) {
		i = LocateVex(G,v1);
		j = LocateVex(G,v2);
		InsertChild (G,i,j);
		InsertChild (G,j,i);
		scanf ("%d,%d",&v1, &v2);
	}
	return OK;
}


 第三部分  BFS遍历

Status BFS(ALGraph G,int i){
	ArcNode *p;
	visited[i]=1;
	printf("%3d",G.vexs[i].data);
	p=G.vexs[i].firstarc;
	while(p){
		i=p->adjvex;
		if (visited[i]==0){
			visited[i]=1;
			printf("%3d",G.vexs[i].data);
		}
		p=p->nextarc;
	}
	return OK;
}
Status 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]==0)
			BFS(G,i);
	}
	printf ("\n");
	return OK;
}


 

第四部分   DFS递归遍历

Status DFS (ALGraph G,int i){
	ArcNode *p;
	visited[i]=1;
	printf ("%3d",G.vexs[i].data);
	p=G.vexs[i].firstarc;
	while (p && visited[p->adjvex]==0){
		i=p->adjvex;
		DFS (G,i);
		p=p->nextarc;
	}
	return OK;
}	
Status DFSTraverse (ALGraph G){
	int i;
	for (i=0; i<G.vexnum; i++)
		visited[i]=0;
	for (i=0; i<G.vexnum; i++){
		if (visited[i]==0)
			DFS(G,i);//递归
	}
	printf ("\n");
	return OK;
}


 

最后一部分   mian函数

int main (){
	int i;
	ArcNode *p;
	ALGraph  G;
	CreateALGraph (&G);
	printf("********Check the Child Link List********\n");
	for (i=0; i<G.vexnum; i++) {
		printf ("%d",G.vexs[i].data);
		p=G.vexs[i].firstarc;
		while (p) {
			printf("—>%d",G.vexs[p->adjvex].data);
			p=p->nextarc;
		}
		printf ("\n");
	}
	printf("*****************************************\n");
	printf("***********   BFSTraverse   *************\n");
	BFSTraverse (G);
	printf("*****************************************\n");
	printf("***********   DFSTraverse   *************\n");
	DFSTraverse (G);
	printf("*****************************************\n");
	return 0;
}

程序演示截图!!!大笑
 

展开阅读全文

没有更多推荐了,返回首页