数据结构与算法李春葆系列之

//实现图的BFS,DFS 
#include<iostream>
#include<stdlib.h>
#include<algorithm>
#define MAXV 6 
#define INF 32767
using namespace std;
typedef  char ElemType;
typedef struct
{
	ElemType data[MAXV];
	int front,rear;
}SqQueue;

typedef struct
{
	int no;
}VertexType;
typedef struct
{
	int edges[MAXV][MAXV];
	int n,e;
	VertexType vexs[MAXV];
}MatGraph;
typedef struct ANode
{
	int adjvex;
	struct ANode *nextarc;
	int weight;
}ArcNode;
typedef struct Vnode
{
	ArcNode *firstarc;
}VNode;

typedef struct
{
	VNode adjlist[MAXV];
	int n,e;
}AdjGraph;
void InitQueue(SqQueue * &q)//初始化环形队列 
{
	q=(SqQueue *)malloc(sizeof(SqQueue));
	q->front=q->rear=0;
}
bool QueueEmpty(SqQueue * q)//环形队列判空 
{
	return(q->front==q->rear);
}
bool enQueue(SqQueue *&q,ElemType e)//入队 
{
	if((q->rear+1)%MAXV==q->front)
	{
		return false;
	}
	q->rear=(q->rear+1)%MAXV;
	q->data[q->rear]=e;
}


bool deQueue(SqQueue *&q, int &e)//出队 
{
	if(q->front==q->rear)
	{
		return false;
	}
	q->front=(q->front+1)%MAXV;
	printf("%c\n",q->data[q->front]);
	e=q->data[q->front];
	return true;
}
void CreatAdj(AdjGraph *&G,int A[MAXV][MAXV],int n,int e)
{
	int i,j;
	ArcNode *p;
	G=(AdjGraph *)malloc(sizeof(AdjGraph));
	for(i=0;i<n;i++)
	{
		G->adjlist[i].firstarc=NULL;
	}
	for(i=0;i<n;i++)
	{
		for(j=n-1;j>=0;j--)
		{
			if(A[i][j]!=0&&A[i][j]!=INF)
			{
				p=(ArcNode *)malloc(sizeof(ArcNode));
				p->adjvex=j;
				p->weight=A[i][j];
				p->nextarc=G->adjlist[i].firstarc;
				G->adjlist[i].firstarc=p;
			}
		}
	}
	G->n=n;
	G->e=e;
}
void DispAdj(AdjGraph *G)
{
    int i;
    ArcNode *p;
    for(i = 0; i < G->n; i++)
    {
        p = G->adjlist[i].firstarc;
        if(p)  printf("%3d:", i);
        while(p)
        {
            printf("%3d->", p->adjvex);
            p = p->nextarc;
        }
        printf("\n");
    }
}
void DestroyAdj(AdjGraph *&G)
{
	int i;
	ArcNode *pre,*p;
	for(i=0;i<G->n;i++)
	{
		pre=G->adjlist[i].firstarc;
		if(pre!=NULL)
		{
			p=pre->nextarc;
			while(p!=NULL)
			{
				free(pre);
				pre=p;p=p->nextarc;
			}
			free(pre);
		}
	}
	free(G);
}
int visited[MAXV]={0};
void DFS(AdjGraph *G,int v)
{
	ArcNode *p;
	visited[v]=1;
	printf("%d",v);
	p=G->adjlist[v].firstarc;
	while(p!=NULL)
	{
		if(visited[p->adjvex]==0)
		{
			DFS(G,p->adjvex);
		}
		p=p->nextarc;
	}
}


void DFS1(AdjGraph *G, int v)
{
    ArcNode *p;
    ArcNode *St[MAXV];
    int top = -1, i, w;
    for(i = 0; i < G->n; i++)
        visited[i] = 0;
    printf("%3d", v);
    visited[v] = 1;
    top++;
    St[top] = G->adjlist[v].firstarc;
    while(top > -1)
    {
        p = St[top];
        top--;
        while(p)
        {
            w = p->adjvex;
            if(visited[w] == 0)
            {
                printf("%3d", w);
                visited[w] = 1;
                top++;
                St[top] = G->adjlist[w].firstarc;
                break;
            }
            p = p->nextarc;
        }
    }
    printf("\n");
}
void BFS(AdjGraph *G,int  v)
{
	int w,i;
	ArcNode *p;
	SqQueue *qu;
	InitQueue(qu);
	int visited[MAXV];
	for(i=0;i<G->n;i++)
	{
		visited[i]=0;
	}
	printf("%2d",v);
	visited[v]=1;
	enQueue(qu,v);
	while(!QueueEmpty(qu))
	{
		deQueue(qu,w);
		p=G->adjlist[w].firstarc;
		while(p!=NULL)
		{
			if(visited[p->adjvex]==0)
			{
				printf("%2d",p->adjvex);
				visited[p->adjvex]=1;
				enQueue(qu,p->adjvex);
			}
			p=p->nextarc;
		}
	}
}
int main()
{   
 	int A[MAXV][MAXV] =
    {
        {  0, 5,  INF, 7, INF, INF},
        {INF, 0,  4, INF, INF, INF},
        {  8, INF, 0, INF, INF, 9 },
        {INF, INF, 5, 0, INF, 6   },
        {INF, INF, INF, 5, 0,  INF},
        {  3, INF, INF, INF, 1, 0 }
    };
	 AdjGraph *p;
	 int n=6;
	 int e=10;
	 CreatAdj(p,A,n,e);
	 cout<<"DFS 递归"<<endl; 
	 DFS(p,0);
	 printf("\n");
	 cout<<"DFS 非递归"<<endl;
	 DFS1(p, 0) ;
	 cout<<"BFS"<<endl; 
	 BFS(p,0);
	 DestroyAdj(p); 
	 
} 
posted @ 2019-12-06 16:27  AmosAlbert  阅读(124)  评论(0编辑  收藏  举报