图的广度优先搜索 皇星客栈

View Code
  1 //----------------------图的广度优先搜索-----------------------
  2 #include<stdio.h>
  3 #include<stdlib.h>
  4 
  5 #define MAXQUEUE   10                        //队列的最大容量
  6 
  7 
  8 struct  node                                  //图顶点结构声明
  9 { 
 10     int vertex;                               //顶点数据
 11     struct  node *nextnode;                         //指向下一顶点的指针
 12 
 13 };
 14 typedef  struct  node *graph;                    //图的结构新类型
 15 struct  node head[9];                       //图的顶点结构数组
 16 int visited[9];                               //遍历记录数组
 17 
 18 
 19 int queue[MAXQUEUE];                     //队列数组声明
 20  
 21 int  front =-1;                           //队列的对头
 22 int rear  =-1;                              //队列的队尾
 23 
 24 
 25 
 26 //---------------------创建图-----------------------
 27 
 28 
 29 void  creategraph(int *node,int num)
 30 {
 31     graph newnode;                      //新顶点指针
 32         graph ptr;                      
 33         int  from;                       //边的起点
 34         int  to;                           //边的终点
 35 
 36         int  i;
 37 
 38               for(i=0;i<num;i++)       //读取边的循环
 39               {
 40                   from=node[i*2];       //边的起点
 41                   to=node[i*2+1];        //边的终点
 42 
 43 
 44                   //-----------创建新顶点内存---------------------
 45 
 46                   newnode=( graph ) malloc(sizeof(struct  node));
 47                   newnode->vertex=to;                   //创建顶点内容
 48                   newnode->nextnode=NULL;              //设置指针初值
 49                   ptr=&(head[from]);                   //顶点位置
 50                   while(ptr->nextnode!=NULL)        //遍历至链表尾
 51                   
 52                       ptr=ptr->nextnode;              //下一个顶点
 53                       ptr->nextnode=newnode;     //插入结尾
 54                   
 55 
 56               }
 57 
 58 }
 59 
 60 //-----------------------队列的数据存入-------------------------
 61 int enqueue(int value)
 62 {
 63     if(rear>=MAXQUEUE)                          //检查队列是否全满
 64         return  -1;                              //无法存入
 65         rear++;                                   //队尾指针往前移
 66         queue[rear]=value;                         //存入队列
 67 
 68 }
 69 
 70 
 71 //-----------------------队列数据的取出-----------------------
 72 
 73 int  dequeue()
 74 {
 75     if(front==rear)                          //检查队列是否为空
 76         return  -1;                       //无法取出
 77     front++;                                 //对头指针往前移
 78     return  queue[front];                        //队列取出
 79 }
 80 
 81 //-------------------------图的广度优先搜索法--------------------------------
 82 void   bfs(  int   current)
 83 {
 84     graph  ptr;
 85 
 86     //处理第一个顶点
 87         enqueue(current);               //将顶点存入队列
 88         visited[current]=1;             //记录已遍历过
 89                                      
 90         printf("顶点[%d]  ",current);   //输出遍历顶点值
 91 
 92         while(front !=rear )                  //队列是否为空
 93         {
 94             current=dequeue();              //将顶点从队列中取出
 95             ptr=head[current].nextnode;        //顶点位置
 96 
 97             while(ptr!=NULL)                //遍历至链表尾
 98             {
 99                 if(visited[ptr->vertex]==0)   //如果没有遍历过
100                 {
101                     enqueue(ptr->vertex);   //递归遍历调用 
102                     visited[ptr->vertex]=1;  //记录已遍历过
103 
104                   printf("顶点[%d] ",ptr->vertex);
105                 }
106             ptr=ptr->nextnode;  //下一个顶点
107             }
108         
109         }
110 
111 }
112 //-----------------将遍历内容输出-----------------------
113 
114 int  main()
115 {
116 
117     graph ptr;
118     int node[20][2]= {                               //边数组
119         {1,2},{2,1},  
120         {1,3},{3,1},  
121         {2,4},{4,2},   
122         {2,5},{5,2}, 
123         {3,6},{6,3},
124         {3,7},{7,3},  
125         {4,8},{8,4},   
126         {5,8},{8,5},   
127         {6,8},{8,6}, 
128         {7,8},{8,7}  };
129 
130        int  i;  
131        for(i=1;i<=8;i++) 
132        { 
133            head[i].vertex=i;                                                     //设置顶点值 
134            head[i].nextnode=NULL;                                                //清除图指针     
135            visited[i]=0;                                                         //设置遍历初值   
136        } 
137        creategraph(*node,20);                                                      //创建图    
138        printf("图的邻接表内容:\n");  
139        for(i=1;i<=8;i++)
140        {
141            printf("顶点%d => ",head[i].vertex);                                 //顶点值     
142            ptr=head[i].nextnode;                                               //顶点位置     
143            while(ptr!=NULL)                                                     //遍历至链表尾    
144            {
145                printf(" %d ",ptr->vertex);                                      //输出顶点内容    
146                ptr=ptr->nextnode;                                              //下一个顶点     
147            } 
148            printf("\n");    
149        }    
150        printf("图的广度优先遍历内容: \n");    
151        bfs(1);
152        printf("\n");
153        system("pause");
154 }

第一次,比较粗糙

原文地址:https://www.cnblogs.com/huangxingkezhan/p/2731953.html