# 图的广度优先搜索（采用邻接表存储方式）

#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 20

int visited[MAXSIZE];

typedef struct node{
struct node * next;
}Node;

typedef struct{
int vertex;
Node * first;
}VNode;

typedef struct qnode{
int data;
struct qnode * next;
}Qnode;

typedef struct{
Qnode * front;
Qnode * rear;

Qnode * qn;
qn = (Qnode *)malloc(sizeof(Qnode));

qn->next = NULL;
lq->front = qn;
lq->rear = qn;
return lq;
}

Qnode * p = (Qnode *)malloc(sizeof(Qnode));
p->data = x;
p->next = NULL;
lq->rear->next = p;
lq->rear = p;
}

Qnode * p;
int x;
if(lq->front == lq->rear){
printf("队列为空\n");
return -1;
}else{
p = lq->front->next;
x = p->data;
lq->front->next = p->next;
free(p);
if(lq->front->next == NULL){
lq->rear = lq->front;
}
return x;
}
}

void create(VNode g[],int n,int e){         //创建无向图的邻接表
Node * p;
int i,j;

for(int t=0;t<n;t++){
g[t].vertex = t;
g[t].first = NULL;
}

for(int k=0;k<e;k++){
printf("请输入连通边i->j\n");
scanf("%d,%d",&i,&j);
p = (Node *)malloc(sizeof(Node));
p->next = g[i].first;
g[i].first = p;
p = (Node *)malloc(sizeof(Node));
p->next = g[j].first;
g[j].first = p;
}
}

void BFS(VNode g[]){                        //广度优先搜索
Node * p;
int j;
printf("%4d",g[0].vertex);
visited[0] = 1;
while(lq->front != lq->rear){
p = g[j].first;
while(p != NULL){
}else{
p = p->next;
}
}
}
}

int main(){
VNode g[MAXSIZE];
int n,e;
printf("请输入节点的个数！\n");
scanf("%d",&n);
printf("请输入边的个数\n");
scanf("%d",&e);
create(g,n,e);
printf("广度优先搜索结果：\n");
BFS(g);
return 0;
}



2017-04-27 21:26:22

#### 【算法入门】广度/宽度优先搜索(BFS)

2012-04-30 02:58:22

#### 【数据结构】广度优先搜索算法

2017-05-08 22:46:52

#### 广度优先算法，深度优先算法和DijKstra算法

2017-02-25 12:33:30

#### 广度优先搜索BFS-C实现、思路、解析和总结

2017-09-16 16:29:31

#### 广度优先搜索的实现

2017-05-28 16:25:33

#### 广度/宽度优先搜索(BFS)详解

2016-09-27 11:17:55

#### 广度优先搜索与深度优先遍历

2018-03-07 22:22:38

#### BFS 广度优先搜索 解析

2015-01-29 18:09:05

#### BFS广度优先搜索——入门

2016-10-26 21:03:57