图的广度优先遍历(邻接表)

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<queue>
using namespace std;

#define MAX 100

typedef struct EdgeNode// 边表结点
{
    int adjves;//存储顶点的下标
    struct EdgeNode* next;//连接下一个邻点
}EdgeNode;

typedef struct VertexNode//顶点表结点
{
    int ves;//顶点的值
    EdgeNode* firstedge;//相连的顶点的值
}VertexNode,AdjList[MAX];
//邻接表
typedef struct
{
    AdjList adjlist;
    int ves;//顶点
    int edge;//
    int book[MAX];//判断是否有被访问过
}MGraph;

void createMGraph(MGraph *G)
{
    int i;
    int start;
    int end;

    EdgeNode *e;

    printf("please input the ves and edge:\n");
    scanf("%d%d",&(G->ves),&(G->edge));
//初始化
    printf("please input the ves:\n");//此处设置顶点与存储下标相同且从零开始 

    for(i = 0; i < G->ves; i++)//输入顶点
    {
        scanf("%d",&(G->adjlist[i].ves));
        G->adjlist[i].firstedge = NULL;
    }
//创建邻接矩阵

    printf("please input the edges:\n");
    for(i = 0; i < G->edge; i++)
    {
        scanf("%d%d",&start,&end);

        e =(EdgeNode*)malloc(sizeof(EdgeNode));//分配空间
        e->adjves = end;
        e->next = G->adjlist[start].firstedge;
        G->adjlist[start].firstedge = e;//类似于链表的前插


        e =(EdgeNode*)malloc(sizeof(EdgeNode));//分配空间
        e->adjves = start;
        e->next = G->adjlist[end].firstedge;
        G->adjlist[end].firstedge = e;//类似于链表的前插
    }
}

void bfs(MGraph *G,int ves)
{
    queue<VertexNode> Q;
    Q.push(G->adjlist[ves]);
    G->book[ves] = 1;
    while(!Q.empty()){
        VertexNode tmp = Q.front();
        printf("%d ", tmp.ves);
        Q.pop();
        EdgeNode *p = tmp.firstedge;
        while(p != NULL){
            if(G->book[p->adjves] == 0){
                Q.push(G->adjlist[p->adjves]);
                G->book[p->adjves] = 1;
            }
            p = p->next;
        }
    }
}

void bfsTraverse(MGraph *G){
    int i;
    memset(G->book,0,sizeof(G->book));//清空标志位
    for(i = 0; i < G->ves; i++)
        if(!G->book[i])
            bfs(G, i);
} 

int main()
{
    MGraph G;
    createMGraph(&G);

    bfsTraverse(&G);
    return 0;
}
/*
输入样例:
7 9
0 1 2 3 4 5 6
0 2
0 3
0 4
1 3
1 5
2 3
2 5
4 5
5 6
*/ 

输入样例:给定图如下 
这里写图片描述

 

在关于bfs的代码编写的时候发现了自己 ->操作符和 .操作符乱用,下面作出说明:

比如你有这个结构体:

struct xx
{
  int a;
  int b;
}yy, *kk;

那么使用如下:

yy.a = 3,  yy.b = 5;
kk = new xx;
kk->a = 4, kk->b = 6;

也就是说你用结构体定义了一个实体,那么这个实体要引用他里面的成员,就用.操作符

如果你用结构体定义的是一个结构指针,那么要引用他里面的成员就用->

typedef struct VertexNode//顶点表结点
{
    int ves;//顶点的值
    EdgeNode* firstedge;//相连的顶点的值
}VertexNode,AdjList[MAX];
.
.
.
void bfs(MGraph *G,int ves)
{
    queue<VertexNode> Q;
    Q.push(G->adjlist[ves]);
    G->book[ves] = 1;
    while(!Q.empty()){
        VertexNode tmp = Q.front();
        printf("%d ", tmp.ves);
        Q.pop();
        EdgeNode *p = tmp.firstedge;
        while(p != NULL){
            if(G->book[p->adjves] == 0){
                Q.push(G->adjlist[p->adjves]);
                G->book[p->adjves] = 1;
            }
            p = p->next;
        }
    }
}

结合上述代码,VertexNode 结构体中定义了AdjList[MAX]实体数组

所以队列声明为 queue<VertexNode> Q; 而不是 queue<VertexNode*> Q;

所以类似声明 VertexNode tmp = Q.front();

继而 tmp 的相关操作使用操作符 .

posted @ 2018-12-12 02:17  Johnny、  阅读(4827)  评论(0编辑  收藏  举报