066.图的广度优先遍利

简介: 066.图的广度优先遍利
/*//*/
/*    图形的广度优先搜寻法                 */
/* ///*/
#include <stdlib.h>
#include <stdio.h>
#define MAXQUEUE 10               /* 队列的最大容量       */
struct node                       /* 图的顶点结构定义     */
{
   int vertex;
   struct node *nextnode;
};
typedef struct node *graph;       /*  图的结构指针        */
struct node head[9];              /* 图的顶点数组         */
int visited[9];                   /* 遍历标记数组         */
int queue[MAXQUEUE];              /* 定义序列数组         */
int front = -1;                   /* 序列前端            */
int rear = -1;                    /* 序列后端            */
/***********************二维数组向邻接表的转化****************************/
void creategraph(int node[20][2],int num)
{
   graph newnode;                 /*  顶点指针           */
   graph ptr;
   int from;                      /* 边起点             */
   int to;                        /* 边终点             */
   int i;
   for ( i = 0; i < num; i++ )    /* 第i条边的信息处理    */
   {
      from = node[i][0];           /* 边的起点           */
      to = node[i][1];           /* 边的终点             */
    /*              建立新顶点                         */
      newnode = ( graph ) malloc(sizeof(struct node));
      newnode->vertex = to;       /*    顶点内容         */
      newnode->nextnode = NULL;   /* 设定指针初值         */
      ptr = &(head[from]);        /* 顶点位置             */
      while ( ptr->nextnode != NULL ) /* 遍历至链表尾     */
         ptr = ptr->nextnode;         /* 下一个顶点       */
      ptr->nextnode = newnode;        /* 插入第i个节点的链表尾部 */
   }
}
/************************ 数值入队列************************************/
int enqueue(int value)
{
   if ( rear >= MAXQUEUE )        /* 检查伫列是否全满     */
      return -1;                  /* 无法存入             */
   rear++;                        /* 后端指标往前移       */
   queue[rear] = value;           /* 存入伫列             */
}
/************************* 数值出队列*********************************/
int dequeue()
{
   if ( front  == rear )          /* 队列是否为空         */
      return -1;                  /* 为空,无法取出       */
   front++;                       /* 前端指标往前移       */
   return queue[front];           /* 从队列中取出信息     */
}
/***********************  图形的广度优先遍历************************/
void bfs(int current)
{
   graph ptr;
   /* 处理第一个顶点 */
   enqueue(current);              /* 将顶点存入队列       */
   visited[current] = 1;          /* 已遍历过记录标志置疑1*/
   printf(" Vertex[%d]\n",current);   /* 打印输出遍历顶点值 */
   while ( front != rear )        /* 队列是否为空         */
   {
      current = dequeue();        /* 将顶点从队列列取出   */
      ptr = head[current].nextnode;   /* 顶点位置         */
      while ( ptr != NULL )           /* 遍历至链表尾     */
      {
         if ( visited[ptr->vertex] == 0 ) /*顶点没有遍历过*/
         {
            enqueue(ptr->vertex);     /* 奖定点放入队列   */
            visited[ptr->vertex] = 1; /* 置遍历标记为1    */
      printf(" Vertex[%d]\n",ptr->vertex);/* 印出遍历顶点值 */
         }
         ptr = ptr->nextnode;     /* 下一个顶点           */
      }
   }
}
/***********************  主程序  ************************************/
/*********************************************************************/
void main()
{
   graph ptr;
   int node[20][2] = { {1, 2}, {2, 1},  /* 边信息数组       */
                       {6, 3}, {3, 6},
                       {2, 4}, {4, 2},
                       {1, 5}, {5, 1},
                       {3, 7}, {7, 3},
                       {1, 7}, {7, 1},
                       {4, 8}, {8, 4},
                       {5, 8}, {8, 5},
                       {2, 8}, {8, 2},
                       {7, 8}, {8, 7} };
   int i;
   clrscr();
   puts("This is an example of Width Preferred Traverse of Gragh.\n");
   for ( i = 1; i <= 8; i++ )        /*顶点结构数组初始化*/
   {
      head[i].vertex = i;
      head[i].nextnode = NULL;
      visited[i] = 0;
   }
   creategraph(node,20);       /* 图信息转换,邻接表的建立 */
   printf("The content of the graph's allist is:\n");
   for ( i = 1; i <= 8; i++ )
   {
      printf(" vertex%d =>",head[i].vertex); /* 顶点值       */
      ptr = head[i].nextnode;             /* 顶点位置     */
      while ( ptr != NULL )       /* 遍历至链表尾         */
      {
         printf(" %d ",ptr->vertex);  /* 打印输出顶点内容     */
         ptr = ptr->nextnode;         /* 下一个顶点       */
      }
      printf("\n");               /* 换行                 */
   }
   printf("The contents of BFS are:\n");
   bfs(1);                        /* 打印输出遍历过程         */
   printf("\n");                  /* 换行                 */
   puts(" Press any key to quit...");
   getch();
}
相关文章
|
2天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8072 2
|
11天前
|
域名解析 人工智能 Linux
|
21天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
18776 64
|
19天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
10156 13
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
5天前
|
人工智能 安全 网络安全
|
10天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1529 5
|
9天前
|
云安全 数据采集 人工智能
|
16天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2682 8
|
9天前
|
Web App开发 缓存 安全
巨魔商店 TrollStore 安装教程(2026):免电脑在线安装与全机型安装器矩阵_iOS巨魔安装
巨魔商店(TrollStore)是iOS 14–17.0(特定版本)的永久签名工具,无需越狱,利用CoreTrust漏洞实现IPA签名长期有效,彻底告别7天重签。支持免电脑在线安装(TrollHelperOTA),适配多机型/系统组合,开源免费,安全可控。(239字)
2275 0