数据结构图的数组表示法以及深度,广度遍历

#include"stdio.h"
#include"stdlib.h"
#define MAX_VERTEX_NUM 10
#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define maxsize 100
typedef int QElemType;
typedef int VRType;
typedef int InfoType;
typedef int VertexType;
typedef int Status;
int visited[MAX_VERTEX_NUM];//全局数组变量


第一部分      存储结构:

1.边存储结构

typedef struct ArcCell{
	VRType adj;
	//InfoType * info;//此程序并未涉及权值
}ArcCell,AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];//AdiMatrix 为一‘该结构体类型的数组类型’,可用于定义‘该结构体类型数据的数组变量’


1.图数组表示法存储结构

typedef struct {
	VertexType vexs[MAX_VERTEX_NUM];
	AdjMatrix arcs;
	int vexnum,arcnum;
	char kind;
}MGraph;


 

第二部分     基础操作

1图的建立操作(仅仅写了一个UDN的建立)

int LocateVex(MGraph *G,int vex){
	int i;
	for (i=0;i<G->vexnum;i++){
		if(vex == G->vexs[i])
			break;
	}
	if(i<G->vexnum)return i;
	else return ERROR;
		
}
Status CreateGraph (MGraph *G){
	//int CreateDG (MGraph *);//函数申明
	//int CreateDN (MGraph *);//函数申明
	//int CreateUDG (MGraph *);//函数申明
	int CreateUDN (MGraph *);//函数申明
	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;
	case '4': CreateUDN(G);break;//此处G为指针
	default :return ERROR;
	}
	return OK;
}
int CreateUDN (MGraph *G1){
	int i,j,k;
	int v1,v2;//邻接点变量用于表示变或弧
	printf ("Please enter values for G.vexnum and G.arcnum.\n");
	scanf ("%d%d",&G1->vexnum,&G1->arcnum);
	printf ("Please enter value for vertexs.\n");
	for (i=0;i<G1->vexnum;i++)
		scanf("%d",&G1->vexs[i]);
	for (i=0;i<MAX_VERTEX_NUM;i++){
		for (j=0;j<MAX_VERTEX_NUM;j++)
			G1->arcs[i][j].adj = 0;
	}//初始化边矩阵,网则初始化为无穷大INFINITY,图则初始化为0
	printf("Please enter ArcCell,example:<1,2>,enter '1,2'\n");
	for (k=1;k<=G1->arcnum;k++){
		scanf ("%d,%d",&v1,&v2);
		i=LocateVex (G1,v1);//LocataVex定位,G1为指针类型
		j=LocateVex (G1,v2);
		G1->arcs[i][j].adj = 1;
		G1->arcs[j][i] = G1->arcs[i][j];
	}
	return OK;
}


2.DFSTraverse操作

Status DFSTraverse(MGraph G){
	Status DFS (MGraph, int );//函数申明
	int v;
	for (v=0; v<G.vexnum; v++)
		visited[v]=0;
	for (v=0; v<G.vexnum; v++){
		if (visited[v]==0)
			DFS (G, v);
	}
	return OK;
}
Status DFS(MGraph G, int v){
	int FirstAdjVex (MGraph, int );
	int NextAdjvex (MGraph, int, int );
	int w;
	visited[v]=1;
	printf ("%3d",G.vexs[v]);
	for(w=FirstAdjVex (G, v);w>0;w=NextAdjvex (G, w, v)){
		if (visited[w]==0)
			DFS (G, w);
	}
	return OK;
}
int NextAdjvex (MGraph G, int v, int w) {
	int i,t=0;
	for (i=w+1; i<G.vexnum; i++) {
		if (G.arcs[v][i].adj==1 && visited[i]==0) {
			t=i;
			break;
		}
		else t=0;
	}
	return t;
}
int FirstAdjVex (MGraph G, int v){
	int i,t=0;
	for (i=0; i<G.vexnum; i++){
		if(G.arcs[v][i].adj==1&&visited[i]==0){
			t=i;
			break;
		}
		else t=0;
	}
	return t;
}


 

3.BFSTraverse操作,需要借助循环队列

3.1循环队列存储结构以及基本操作

typedef struct{
	QElemType *base;
	int front;
	int rear;
}SqQueue;
Status InitQueue_Sq(SqQueue *Q){
	Q->base=(QElemType *)malloc(maxsize*sizeof(QElemType));
	if(!Q->base)exit(OVERFLOW);
	Q->front=Q->rear=0;
	return OK;
}
Status EnQueue_Sq(SqQueue *Q,QElemType e){
	if((Q->rear+1)%maxsize==Q->front)exit(0);
	Q->base[Q->rear]=e;
	Q->rear=(Q->rear+1)%maxsize;
	return OK;
}
Status DeQueue_Sq(SqQueue *Q,QElemType *e){
	if(Q->rear==Q->front)exit(0);
	*e=Q->base[Q->front];
	Q->front=(Q->front+1)%maxsize;
	return OK;
}
Status GetFront_Sq(SqQueue Q,QElemType *e){
	if(Q.rear==Q.front)return ERROR;
	else *e=Q.base[Q.front];
	return OK;
}


 

3.2BFS操作

Status BFSTraverse (MGraph G){
	int v,a,i;
	SqQueue Q;
	for (v=0; v<G.vexnum; v++)
		visited[v]=0;//标志数组初始化
	InitQueue_Sq (&Q);
	for (v=0; v<G.vexnum; v++) {
		if (visited[v] == 0) {
			visited[v] = 1;
			printf ("%3d",G.vexs[v]);
			EnQueue_Sq (&Q,v);
		}
		while(Q.rear!=Q.front){
			GetFront_Sq (Q,&a);
			for (i=0; i<G.vexnum; i++){
				if (G.arcs[v][i].adj==0)
					continue;
				if (visited[v] == 1)
					continue;
				else {
					visited[i] = 1;
					printf ("%3d",G.vexs[i]);
					EnQueue_Sq (&Q,i);
				}
			}
			DeQueue_Sq (&Q,&a);
		}
	}
}


 

最后一部分:main函数

int main (){
	int i,j;
	MGraph G;
	CreateGraph (&G);
	for(i=0;i<G.vexnum;i++)
		printf ("%3d",G.vexs[i]);
	printf ("\n");
	for (i=0; i<G.vexnum; i++){
		for (j=0; j<G.vexnum; j++){
			printf ("%2d",G.arcs[i][j]);
			if (j==G.vexnum-1)
				printf("\n");
		}
	}
	DFSTraverse(G);
	printf("\n");
	BFSTraverse (G);
	system("pause");
	return OK;
}


 程序演示截图

微笑吐舌头偷笑大笑生气得意羡慕奋斗

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