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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
| #include<stdio.h> #include <malloc.h> #define Max 10
typedef int VertexType; typedef int EdgeType;
typedef struct EdgeNode { int adjvex; EdgeType weight; struct EdgeNode* next; }EdgeNode;
typedef struct VertexNode { VertexType data; EdgeNode* first_edge; }VertexNode, AdjList[Max];
typedef struct { AdjList adjList; int numVertexes, numEdges; }GraphAdjList, * GraphAdj;
typedef struct LinkNode { VertexNode data; struct LinkNode* next; }LinkNode;
typedef struct { LinkNode* front, * rear; }LinkQueue;
void InitGraph(GraphAdj& G) { int m, n; EdgeNode* e = NULL; G->numVertexes = 8; G->numEdges = 10; for (int i = 0; i < G->numVertexes; i++) { G->adjList[i].data = i + 1; G->adjList[i].first_edge = NULL; } for (int k = 0; k < G->numEdges; k++) { printf("输入边(Vi,Vj)上的顶点序号:"); scanf_s("%d%d", &m, &n);
e = (EdgeNode*)malloc(sizeof(EdgeNode)); e->adjvex = n; e->next = G->adjList[m].first_edge; G->adjList[m].first_edge = e;
e = (EdgeNode*)malloc(sizeof(EdgeNode)); e->adjvex = m; e->next = G->adjList[n].first_edge; G->adjList[n].first_edge = e; } }
int FirstNeighbor(GraphAdj G, int x) { if (x >= Max) { return -1; } if (G->adjList[x].first_edge != NULL) return G->adjList[x].first_edge->adjvex; else return -1; }
int NextNeighbor(GraphAdj G, int x, int y) { EdgeNode* temp = G->adjList[x].first_edge; while (temp != NULL) { if (temp->adjvex == y) { return -1; } else { temp = temp->next; if (temp != NULL) { return temp->adjvex; } else { return -1; } } } }
void InitQueue(LinkQueue& Q) { Q.front = Q.rear = (LinkNode*)malloc(sizeof(LinkNode)); Q.front->next = NULL; }
bool isEmpty(LinkQueue Q) { return Q.front == Q.rear; }
void EnQueue(LinkQueue& Q, GraphAdj G, int v) { LinkNode* new_point = (LinkNode*)malloc(sizeof(LinkNode)); new_point->data = G->adjList[v]; new_point->next = NULL; Q.rear->next = new_point; Q.rear = new_point; }
void DeQueue(LinkQueue& Q, int& v) { if (Q.front == Q.rear) { printf("队列为空"); } else { LinkNode* new_point = Q.front->next; v = new_point->data.data; Q.front->next = new_point->next; if (Q.rear == new_point) { Q.rear = Q.front; } free(new_point); } }
bool visited[Max];
void visit(GraphAdj G, int num) { printf("访问到的数据为:%d\n", G->adjList[num].data); visited[num] = true; }
void BFS(GraphAdj G, int v) { LinkQueue Q; InitQueue(Q); visit(G, v); EnQueue(Q, G, v); while (!isEmpty(Q)) { DeQueue(Q, v); for (int w = FirstNeighbor(G, v); w >= 0; w = NextNeighbor(G, v, w)) { if (!visited[w]) { visit(G, w); EnQueue(Q, G, w); } } } }
void BFS_Traverse(GraphAdj G) { for (int i = 0; i < Max; i++) { visited[i] = false; } for (int i = 0; i < G->numVertexes; i++) { if (!visited[i]) { BFS(G, i); } } }
int main() { GraphAdj G = (GraphAdj)malloc(sizeof(GraphAdj)); InitGraph(G); BFS_Traverse(G); return 0; }
|