数据结构 c语言实现图的邻接表创建及深度与广度优先遍历

这里将顶点通过数组的方式写入,直接输入边的数量,顶点信息和权值,如果要更改数组元素的数量则也需要修改最多顶点数,以及标记数组的元素的数量,并将其初始化为1,这里太懒了不想弄。

数据结构 c语言实现图的邻接表创建及深度与广度优先遍历_第1张图片

 

#include 
#include 
#define MVNUM 5 //最多的顶点数
typedef char VerTexType; //顶点元素的类型
VerTexType vexs[MVNUM]={'a','b','c','d','e'}; //顶点数组
int visited[5]={0,0,0,0,0};  //深度and广度优先遍历的标记数组
typedef struct ArcNode //边节点
{
	int adjvex,info;  //adjvex该边所指向的顶点的位置  info和边相关的信息
	struct ArcNode *nextarc;  //指向下一条边的指针
}ArcNode;
typedef struct VNode //顶点数组
{
	VerTexType data;  //顶点信息
	struct ArcNode *firstarc;  //指向第一条依附该顶点的边的指针
}VNode,AdjList[MVNUM]; //AdjList表示邻接表的类型
typedef struct  //ALGraph定义
{
	AdjList vertices;  //顶点数组
	int vexnum,arcnum; //图当前的顶点数和弧数
}ALGraph;
typedef struct  //定义广度优先遍历的辅助队列
{
	int data[MVNUM]; 
	int front,rear;
}SqQueue;

//函数声明
void InitALGraph(ALGraph *G,VerTexType vex[],int len);  //初始化图
ArcNode *CreateArcNode(int adjvex,int info);  //生成ArcNode也就是边的结点
int FindVexIndex(ALGraph *G,VerTexType v,int len);
void DFS(ALGraph *G,int v);  //深度优先遍历
void BFS(ALGraph *G,int v);  //广度优先遍历
void InitSqQueue(SqQueue *Q); //初始化队列
void EnQueue(SqQueue *Q,int v);//入队
int QueueEmpty(SqQueue *Q); //判断队列是否为空
void DeQueue(SqQueue *Q,int *u); //出队
int main()
{
	ALGraph G;
	int i=0;
	InitALGraph(&G,vexs,MVNUM);
	printf("顶点元素:\n");
	for(i=0;ivexnum=len;
	printf("请输入边的数量:\t");
	scanf("%d",&G->arcnum);
	for(i=0;ivertices[i].data=vex[i];
		G->vertices[i].firstarc=NULL;
	}
	for(k=0;karcnum;k++)
	{
		ArcNode *p1,*p2;
		printf("第条%d边邻接的顶点以及权值,输入是请用空格格开  ###格式为:a b 5\n",k+1);
		scanf(" %c %c %d",&v1,&v2,&w);
		i=FindVexIndex(G,v1,len);
		j=FindVexIndex(G,v2,len);
		p1=CreateArcNode(j,w);  //有向网的邻接表,只需将p2的代码注释即可 把p1注释 便是逆邻接表
		p2=CreateArcNode(i,w);  
		p1->nextarc=G->vertices[i].firstarc;
		G->vertices[i].firstarc=p1;
		p2->nextarc=G->vertices[j].firstarc;
		G->vertices[j].firstarc=p2;
	}
}
int FindVexIndex(ALGraph *G,VerTexType v,int len)  //找到v的下标
{
	int i=0;
	for(i=0;ivertices[i].data==v)return i;
	}
	return -1;
}
ArcNode *CreateArcNode(int adjvex,int info)  //生成ArcNode也就是边的结点
{
	ArcNode *newNode=(ArcNode*)malloc(sizeof(ArcNode));
	newNode->adjvex=adjvex;
	newNode->info=info;
	newNode->nextarc=NULL;
	return newNode;
}
void DFS(ALGraph *G,int v)  //深度优先遍历
{
	ArcNode *p;
	printf("%c	",G->vertices[v].data);
	visited[v]=1;
	p=G->vertices[v].firstarc;
	while(p!=NULL)
	{
		if(visited[p->adjvex]==0)
		{
			DFS(G,p->adjvex);
		}
		p=p->nextarc;
	}
}
void BFS(ALGraph *G,int v) //广度优先遍历
{
	int u;
	SqQueue Q;
	ArcNode *p;
	visited[v]=1;
	printf("%c	",G->vertices[v].data);
	InitSqQueue(&Q);
	EnQueue(&Q,v);
	while(Q.front!=Q.rear)
	{
		DeQueue(&Q,&u);
		p=G->vertices[u].firstarc;
		while(p!=NULL)
		{
			if(visited[p->adjvex]==0)
			{
				printf("%c	",G->vertices[p->adjvex]);
				visited[p->adjvex]=1;
				EnQueue(&Q,p->adjvex);
			}
			p=p->nextarc;
		}
	}

}
void InitSqQueue(SqQueue *Q)  //初始化队列
{
	Q->front=Q->rear=0;
}
void EnQueue(SqQueue *Q,int v)  //入队
{
	Q->data[Q->rear]=v;
	Q->rear=(Q->rear+1)%MVNUM;
	
}
int QueueEmpty(SqQueue *Q)  //判断队列是否为空
{
	if(Q->front==Q->rear){
		return 1;
	}
	return 0;
}
void DeQueue(SqQueue *Q,int *u) //出队
{
	*u=Q->data[Q->front];
	Q->front=(Q->front+1)%MVNUM;
}

你可能感兴趣的:(数据结构,数据结构,c语言,宽度优先)