1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| #include <stdio.h> #include <stdlib.h> #include <queue> using namespace std;
#define MAXVEX 100 #define INFINITY 65535
bool visited[MAXVEX]; typedef char VertexType; typedef int EdgeType; typedef int InforType;
typedef struct EdgeNode { int adjvex; EdgeType weight; struct EdgeNode *next; }EdgeNode;
typedef struct VertexNode { VertexType data; EdgeNode *firstedge; }VertexNode;
typedef struct GraphAdjlist { VertexNode adjlist[MAXVEX]; int numVertex, numEdge; }GraphList;
void BFSTraverse(GraphList g) { int i, j; EdgeNode * p; queue <int> q; for(i = 0; i < g.numVertex; i++) visited[i] = false; for(i = 0; i < g.numVertex; i++) { if(!visited[i]) { visited[i] = true; printf("%c ", g.adjlist[i].data); q.push(i); while(!q.empty()) { int m = q.front(); q.pop(); p = g.adjlist[m].firstedge; while(p) { if(!visited[p->adjvex]) { visited[p->adjvex] = true; printf("%c ", g.adjlist[p->adjvex].data); q.push(p->adjvex); } p = p->next; } } } } }
|