源码+注释
#include <stdio.h>
#include <malloc.h>
#define TRUE 1
#define FALSE 0
#define MAX_ALVNUMS 100
typedef int QElemType;
typedef struct QNode {
QElemType data;
struct QNode *next;
} QNode, *QueuePtr;
typedef struct {
QueuePtr front;
QueuePtr rear;
} LinkQueue;
typedef struct EdgeNode {
int adjvex;
struct EdgeNode *next;
} EdgeNode;
typedef struct VertexNode {
char verxs;
EdgeNode *firstEdge;
} AdjList[MAX_ALVNUMS];
typedef struct {
AdjList adjList;
int numVertexes, numEdges;
} ALGraph;
int visited[MAX_ALVNUMS];
void CreateALGraph(ALGraph *G);
void PrintAMatrix(ALGraph G);
void DFSTraverse(ALGraph G, int v);
void DFS_AL(ALGraph G, int v);
void BFSTraverse(ALGraph G, int v);
void BFS_AL(ALGraph G, int v);
int InitQueue(LinkQueue *queue);
int EnQueue(LinkQueue *queue, QElemType elem);
int DeQueue(LinkQueue *queue, QElemType *elem);
int IsEmpty(LinkQueue *queue);
int main() {
ALGraph *G;
G = (ALGraph *) malloc(sizeof(ALGraph));
CreateALGraph(G);
PrintAMatrix(*G);
DFSTraverse(*G, 0);
printf("\n");
BFSTraverse(*G, 1);
getchar();
}
void CreateALGraph(ALGraph *G) {
printf("输入无向图的顶点数和边数,用空格分开:");
scanf("%d %d", &(G->numVertexes), &(G->numEdges));
getchar();
for (int i = 0; i < G->numVertexes; i++) {
G->adjList[i].verxs = i;
G->adjList[i].firstEdge = NULL;
}
int arrSub1[] = {0, 0, 1, 1, 2, 2, 3, 4, 5};
int arrSub2[] = {1, 2, 3, 4, 5, 6, 7, 7, 6};
for (int i = 0; i < G->numEdges; i++) {
EdgeNode *edgeNode = (EdgeNode *) malloc(sizeof(EdgeNode));
edgeNode->adjvex = arrSub2[i];
edgeNode->next = G->adjList[arrSub1[i]].firstEdge;
G->adjList[arrSub1[i]].firstEdge = edgeNode;
edgeNode = (EdgeNode *) malloc(sizeof(EdgeNode));
edgeNode->adjvex = arrSub1[i];
edgeNode->next = G->adjList[arrSub2[i]].firstEdge;
G->adjList[arrSub2[i]].firstEdge = edgeNode;
}
}
void PrintAMatrix(ALGraph G) {
printf("============= 无向图G的邻接表 =============\n");
for (int i = 0; i < G.numVertexes; i++) {
if (i == 0) {
printf(" .--------.");
for (EdgeNode *edgeNode = G.adjList[i].firstEdge;
edgeNode != NULL; edgeNode = edgeNode->next) {
printf(" .-------.");
}
printf("\n");
}
printf(" %d ", i);
printf("| V%d | --|-->", G.adjList[i].verxs);
for (EdgeNode *edgeNode = G.adjList[i].firstEdge;
edgeNode != NULL; edgeNode = edgeNode->next) {
if (edgeNode->next != NULL) {
printf("| %d | --|", edgeNode->adjvex);
printf("-->");
} else {
printf("| %d | ^ |", edgeNode->adjvex);
}
}
printf("\n");
if (i != G.numVertexes - 1) {
printf(" |----+---|");
for (EdgeNode *edgeNode = G.adjList[i].firstEdge;
edgeNode != NULL; edgeNode = edgeNode->next) {
printf(" :=======:");
}
} else {
printf(" '--------'");
for (EdgeNode *edgeNode = G.adjList[i].firstEdge;
edgeNode != NULL; edgeNode = edgeNode->next) {
printf(" '-------'");
}
}
printf("\n");
}
}
void DFSTraverse(ALGraph G, int v) {
for (int i = 0; i < G.numVertexes; i++) {
visited[i] = FALSE;
}
for (int i = 0; i < G.numVertexes; i++) {
if (!visited[i]) {
DFS_AL(G, v);
}
}
}
void DFS_AL(ALGraph G, int v) {
printf("V%d ", v);
visited[v] = TRUE;
EdgeNode *edgeNode = G.adjList[v].firstEdge;
while (edgeNode != NULL) {
int i = edgeNode->adjvex;
if (!visited[i]) {
DFS_AL(G, i);
}
edgeNode = edgeNode->next;
}
}
void BFSTraverse(ALGraph G, int v) {
for (int i = 0; i < G.numVertexes; i++) {
visited[i] = FALSE;
}
for (int i = 0; i < G.numVertexes; i++) {
if (!visited[i]) {
BFS_AL(G, v);
}
}
}
void BFS_AL(ALGraph G, int v) {
printf("V%d ", v);
visited[v] = TRUE;
LinkQueue queue;
InitQueue(&queue);
EnQueue(&queue, v);
int u;
while (!IsEmpty(&queue)) {
DeQueue(&queue, &u);
EdgeNode *edgeNode = G.adjList[u].firstEdge;
while (edgeNode != NULL) {
if ((!visited[edgeNode->adjvex])) {
printf("V%d ", edgeNode->adjvex);
visited[edgeNode->adjvex] = TRUE;
EnQueue(&queue, edgeNode->adjvex);
}
edgeNode = edgeNode->next;
}
}
}
int InitQueue(LinkQueue *queue) {
queue->front = queue->rear = (QueuePtr) malloc(sizeof(QNode));
if (!(queue->front)) {
return FALSE;
}
queue->front->next = NULL;
return TRUE;
}
int EnQueue(LinkQueue *queue, QElemType elem) {
QueuePtr temp;
temp = (QueuePtr) malloc(sizeof(QNode));
if (!temp) {
return FALSE;
}
temp->data = elem;
temp->next = NULL;
queue->rear->next = temp;
queue->rear = temp;
return TRUE;
}
int DeQueue(LinkQueue *queue, QElemType *elem) {
if (IsEmpty(queue)) {
return FALSE;
}
QueuePtr pQ;
pQ = queue->front->next;
*elem = pQ->data;
queue->front->next = pQ->next;
if (queue->rear == pQ) {
queue->rear = queue->front;
}
free(pQ);
return TRUE;
}
int IsEmpty(LinkQueue *queue) {
return queue->front == queue->rear;
}
运行结果