#include <stdio.h> #include <stdlib.h> #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define OVERFLOW -2 #define MAX_NUM 20 typedef int Status; typedef int QElemType; typedef char VexType; /* * 鄰接表存儲結構 */ typedef struct EdgeNode { int adjvex; //頂點的位置 struct EdgeNode *next; //指向下一條邊的指針 }EdgeNode, *EdgeLink; typedef struct VexNode { VexType data; //頂點數據 EdgeNode *firstEdge; //指向第一條依附該頂點的邊的指針 }VexNode, AdjList[MAX_NUM]; typedef struct { AdjList adjList; int vexNum, edgeNum; //頂點數和邊數 }ALGraph; /* * 隊列存儲結構(用於圖的遍歷) */ typedef struct QNode { QElemType data; //結點數據 struct QNode *next; //指向下一個結點 }QNode, *QueuePtr; typedef struct { QueuePtr front; //隊頭指針 QueuePtr rear; //隊尾指針 }LinkQueue; /* * 初始化隊列 */ Status InitQueue(LinkQueue *Q) { Q->front = Q->rear = (QueuePtr) malloc(sizeof(QNode)); if (!Q->front) { exit(OVERFLOW); } Q->front->next = NULL; return OK; } /* * 判斷隊列是否為空 */ Status IsEmpty(LinkQueue Q) { if (Q.front->next == NULL) { return TRUE; } else { return FALSE; } } /* * 入隊 */ Status EnQueue(LinkQueue *Q, QElemType e) { QueuePtr p = (QueuePtr) malloc(sizeof(QNode)); if (!p) { exit(OVERFLOW); } p->data = e; p->next = NULL; Q->rear->next = p; Q->rear = p; return OK; } /* * 出隊 */ Status DeQueue(LinkQueue *Q, QElemType *e) { QueuePtr p; if (Q->front == Q->rear) { return ERROR; } p = Q->front->next; *e = p->data; Q->front->next = p->next; if (Q->rear == p) { Q->rear = Q->front; } free(p); return OK; } /* * 創建圖 */ Status CreateGraph(ALGraph *G) { int i, j, k; EdgeLink e; printf("請輸入頂點數目和邊數:\n"); scanf("%d", &G->vexNum); scanf("%d", &G->edgeNum); getchar(); printf("請輸入各頂點的數據:\n"); for (i = 0; i < G->vexNum; i++) { scanf("%c",&G->adjList[i].data); if (G->adjList[i].data == '\n') { i--; continue; } G->adjList[i].firstEdge = NULL; } printf("請依次輸入邊(Vi,Vj)的頂點序號:\n"); for (k = 0; k < G->edgeNum; k++) { scanf("%d", &i); scanf("%d", &j); e = (EdgeLink) malloc(sizeof(EdgeNode)); e->adjvex = j; e->next = G->adjList[i].firstEdge; G->adjList[i].firstEdge = e; e = (EdgeLink) malloc(sizeof(EdgeNode)); e->adjvex = i; e->next = G->adjList[j].firstEdge; G->adjList[j].firstEdge = e; } return OK; } int visited[MAX_NUM]; //用於記錄遍歷狀態 /* * 遞歸從第i個結點深度優先遍歷圖 */ void DFS(ALGraph G, int i) { EdgeLink p; visited[i] = TRUE; printf("%c ", G.adjList[i].data); p = G.adjList[i].firstEdge; while (p) { if (!visited[p->adjvex]) { DFS(G, p->adjvex); } p = p->next; } } /* * 深度優先遍歷 */ Status DFSTraverse(ALGraph G) { int i; for (i = 0; i < MAX_NUM; i++) { visited[i] = FALSE; } for (i = 0; i < G.vexNum; i++) { if (!visited[i]) { DFS(G, i); } } return OK; } /* * 廣度優先遍歷 */ Status BFSTraverse(ALGraph G) { int i; EdgeLink p; LinkQueue Q; InitQueue(&Q); for (i = 0; i < MAX_NUM; i++) { visited[i] = FALSE; } for (i = 0; i < G.vexNum; i++) { if (!visited[i]) { visited[i] = TRUE; printf("%c ", G.adjList[i].data); EnQueue(&Q, i); while (!IsEmpty(Q)) { DeQueue(&Q, &i); p = G.adjList[i].firstEdge; while (p) { if (!visited[p->adjvex]) { visited[p->adjvex] = TRUE; printf("%c ", G.adjList[p->adjvex].data); EnQueue(&Q, p->adjvex); } p = p->next; } } } } return OK; } int main() { ALGraph G; CreateGraph(&G); printf("深度優先遍歷:"); DFSTraverse(G); printf("\n廣度優先遍歷:"); BFSTraverse(G); printf("\n"); }
