王道考研 ++++ 图的广度优先搜索

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define MaxSize 100
typedef struct EageTable  //边表
{
  int eageData;           //值
  struct EageTable *next; //指向下一条弧
}EageTable;
typedef struct HeadTable  //头表
{
  int headData;           //值
  EageTable *first;       //指向头节点后第一个节点
}HeadTable,HeadList[MaxSize];
typedef struct
{
  HeadList Graph;
  int headNum,arcNum;     //头节点书 弧数
}Graph;

typedef struct LQueue     //队列
{
  int Queue[MaxSize];
  int front,rear;
}LQueue;
bool vis[MaxSize] = {false};

void InitQueue(LQueue *Q);
bool JudgeEmpty(LQueue *Q);
void EnQueue(LQueue *Q,int num);
int DeQueue(LQueue *Q);
int GetFront(LQueue *Q);
void InitGraph(Graph *G);
void CreateGraph(Graph *G,int flag,int a,int b);
void BFS(LQueue *Q,Graph *G,int front);
void TraverBFS(LQueue *Q,Graph *G,int start);
/*初始化队列*/
void InitQueue(LQueue *Q)
{
  Q->front = Q->rear = 0;
}
/*判断队列是否为空*/
bool JudgeEmpty(LQueue *Q)
{
  if(Q->front == Q->rear)return true;
  else return false;
}
/*进队*/
void EnQueue(LQueue *Q,int num)
{
  Q->Queue[Q->rear++] = num;
}
/*出队*/
int DeQueue(LQueue *Q)
{
  int temp = Q->Queue[Q->front];
  Q->front++;
  return temp;
}
int GetFront(LQueue *Q)
{
  return Q->Queue[Q->front];
}
/*初始化邻接表的头*/
void InitGraph(Graph *G)
{
  int i;
  for(i = 1;i <= G->headNum;i++)
  {
    G->Graph[i].headData = i;
    G->Graph[i].first = NULL;
  }
}
/*创建邻接表*/
void CreateGraph(Graph *G,int flag,int a,int b)
{
  EageTable* eage;
  eage = (EageTable*)malloc(sizeof(EageTable));
  eage->eageData = b;
  //头插入
  eage->next = G->Graph[a].first;
  G->Graph[a].first = eage;
  //图为无向图时
  if(flag == 2)
  {
    EageTable* eagee;
    eagee = (EageTable*)malloc(sizeof(EageTable));
    eagee->eageData = a;

    eagee->next = G->Graph[b].first;
    G->Graph[b].first = eagee;
  }
}
/*广搜*/
void BFS(LQueue *Q,Graph *G,int front)
{
    EageTable *eage = G->Graph[front].first;//eage区front节点的侧表
    while(eage)//遍历
    {
      if(vis[eage->eageData] == false)//没被访问的节点入栈 并 标志节点已读
      {
          EnQueue(Q,eage->eageData);
          vis[eage->eageData] = true;
      }
      eage = eage->next;
    }
}
void TraverBFS(LQueue *Q,Graph *G,int start)
{
  InitQueue(Q);
  EnQueue(Q,start);
  vis[start] = true;//标志节点已读
  while(!JudgeEmpty(Q))
  {
    int front = GetFront(Q);//获取队中第一个节点
    printf("%-2d",front);//输出节点
    BFS(Q,G,front);
    DeQueue(Q);
  }
}
int main(int argc, char const *argv[]) {
  Graph G;
  LQueue Q;
  int i,flag,start;

  printf("有向图输入1,无向图输入2:");
  scanf("%d",&flag);
  printf("请输入节点数:");
  scanf("%d",&G.headNum);
  InitGraph(&G);
  printf("请输入弧的条数(从1开始):");
  scanf("%d",&G.arcNum);
  printf("请输%d入条边:\n",G.arcNum);
  for(i = 0;i < G.arcNum;i++)
  {
    int a,b;
    scanf("%d %d",&a,&b);
    CreateGraph(&G,flag,a,b);
  }
  printf("请输入开始的节点:");
  scanf("%d",&start);
  TraverBFS(&Q,&G,start);
  return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值