MGraph. h
#pragma once #include “Queue.h” #define MaxVertexNum 100 typedef char VertexType; typedef int EdgeType; typedef struct { VertexType vexs[MaxVertexNum]; EdgeType edges[MaxVertexNum][MaxVertexNum]; int n; //当前图顶点数 int e; //当前边数 }MGraph; bool visited[MaxVertexNum]; MGraph* initMGraph(); bool DFSTraverseM(MGraph* m); bool DFSM(MGraph* m, int i); bool BFSTraverseM(MGraph* m); bool BFSM(MGraph* m, int i);
MGraph.c
#include “MGraph.h” #include <stdio.h> #include <stdlib.h> #include <string.h> /************************************************************************/ /* 初始化图 */ /************************************************************************/ MGraph* initMGraph() { MGraph* m = NULL; int i, j, k; char v1, v2; m = (MGraph*)malloc(sizeof(MGraph)); memset(m->vexs, 0, MaxVertexNum); printf(“please input the number of vertex and edges(‘v,e’): “); scanf(“%d,%d”, &i, &j); if(i<0 && j<0) { printf(“error number”); return NULL; } m->n = i; m->e = j; printf(“please input the vertexs by order:/n”); for(i=0; i<m->n; i++) { fflush(stdin); scanf(“%c”, &m->vexs[i]); } for(i=0; i<m->n; i++) for(j=0; j<m->n; j++) m->edges[i][j] = 0; for(k=0; k<m->e; k++) { printf(“please input the edges by order(‘v1,v2’): “); fflush(stdin); scanf(“%c,%c”, &v1, &v2); for(i=0; v1!=m->vexs[i]; i++); for(j=0; v2!=m->vexs[j]; j++); m->edges[i][j] = 1; } return m; } /************************************************************************/ /* 深度优先遍历(深度优先搜索) */ /************************************************************************/ bool DFSTraverseM(MGraph* m) { int i; if(m == NULL) return FALSE; for(i=0; i<m->n; i++) visited[i] = FALSE; for(i=0; i<m->n; i++) DFSM(m, i); return TRUE; } bool DFSM(MGraph* m, int i) { int j; if(m == NULL) return FALSE; if(visited[i] == FALSE) { printf(“DFSM: node %c/n”, m->vexs[i]); visited[i] = TRUE; } for(j=0; j<m->n; j++) if(visited[j] == FALSE && m->edges[i][j] == 1) DFSM(m, j); return TRUE; } /************************************************************************/ /* 广度优先遍历(广度优先搜索) */ /************************************************************************/ bool BFSM(MGraph* m, int i) { int j; Queue* q = NULL; if(m == NULL) return FALSE; visited[i] = TRUE; printf(“BFSM: node %c/n”, m->vexs[i]); q = initQueue(); enQueue(q, i); while(q->size != 0) { i = deQueue(q); for(j=0; j<m->n; j++) if(visited[i] == FALSE && m->edges[i][j] == 1) { enQueue(q, j); printf(“BFSM: node %c/n”, m->vexs[j]); visited[j] = TRUE; } } return TRUE; } bool BFSTraverseM(MGraph* m) { Queue* q = NULL; int i; if(m == NULL) return FALSE; q = initQueue(); for(i=0; i<m->n; i++) visited[i] = FALSE; for(i=0; i<m->n; i++) if(visited[i] == FALSE) BFSM(m, i); return TRUE; }
Queue.h
#pragma once typedef enum{TRUE, FALSE}bool; #define CAPACITY 10 typedef int ElemType; typedef struct { int front; int rear; int size; ElemType data[CAPACITY]; }Queue; Queue* initQueue(); ElemType deQueue(Queue* q); bool enQueue(Queue* q, ElemType data);
Queue.c
#include “Queue.h” #include <stdlib.h> #include <string.h> /************************************************************************/ /* 初始化队列 */ /************************************************************************/ Queue* initQueue() { Queue *q = NULL; q = (Queue*)malloc(sizeof(Queue)); if(q == NULL) return NULL; memset(q->data, 0, CAPACITY); q->front = q->rear = 0; q->size = 0; return q; } /************************************************************************/ /* 队尾入队 */ /************************************************************************/ bool enQueue(Queue* q, ElemType data) { if(q == NULL) return FALSE; if(q->size == CAPACITY) return FALSE; q->data[q->rear] = data; q->rear = (q->rear+1) % CAPACITY; q->size++; return TRUE; } /************************************************************************/ /* 队首出队 */ /************************************************************************/ ElemType deQueue(Queue* q) { ElemType res; if(q == NULL) exit(0); if(q->size == 0) return FALSE; res = q->data[q->front]; q->front = (q->front+1) % CAPACITY; q->size–; return res; }
main.c
#include “MGraph.h” #include “Queue.h” int main() { MGraph* m = initMGraph(); DFSTraverseM(m); printf(“/n”); BFSTraverseM(m); return 0; }