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

#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;
}



• 本文已收录于以下专栏：

举报原因： 您举报文章：图的广度优先搜索（采用邻接表存储方式） 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)