# 基于邻接表的图的广度遍历

16 篇文章 0 订阅

Time Limit: 1000ms Memory limit: 65536K 有疑问？点这里^_^

1
6 7 0
0 3
0 4
1 4
1 5
2 3
2 4
3 5

0 3 4 2 5 1

# include <stdio.h>
# include <stdlib.h>
# include <memory.h>
typedef struct node
{
int vertex;
struct node*next;
} Node;

int visited[100];//用来记录顶点是否访问过,访问过置为1,没访问过为0
int queue[100];
int front,rear;
void BFS(int t);
int main()
{
Node*p;
int n,k,m,t,i;
int v1,v2;
scanf("%d",&n);
for(i=0;i<100;i++)
{
}
while(n--)
{
memset(visited,0,sizeof(visited));
scanf("%d%d%d",&k,&m,&t);
for(i=0;i<k;i++)
{
}
while(m--)
{
scanf("%d%d",&v1,&v2);
}
/* 检测建的邻接表
for(i=0;i<k;i++)
{
printf("顶点%d::",i);
while(p)
{
printf("%d ",p->vertex);
p = p->next;
}
printf("%\n");
}
*/
BFS(t);
}
return 0;
}

void BFS(int t)
{
int peak ;
Node*p;
front = rear = 0;
queue[rear++] = t;
visited[t] = 1;
printf("%d",t);
while(front < rear)
{
peak = queue[front++];
while(p)
{
if(!visited[p->vertex])
{
printf(" %d",p->vertex);
visited[p->vertex] = 1;
queue[rear++] = p->vertex;
}
p = p->next;
}
}
}

/*建立有序链表*/
{
Node*p,*q,*r;
p = (Node*)malloc(sizeof(Node));
p->vertex = v;
p->next = NULL;
while(r && r->vertex < v)
{
q = r;
r = r->next;
}
q->next = p;
p->next = r;
}


• 0
点赞
• 0
评论
• 4
收藏
• 打赏
• 扫一扫，分享海报

04-13 2万+
10-07 4826
07-09 1524
07-28 423
05-02 3654
11-14 1万+
09-10 1251
02-07 900
09-25 4442
06-08 1796
06-29 1201

power_to_go

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。