ALGraph.h文件代码
#include <stdio.h>
#include <stdlib.h>
#define MAX_VER_NUM 20
typedef char VertexType;
//边结点
typedef struct {
VertexType vexname;
struct ArcNode *next;
} ArcNode;
//顶点结点
typedef struct {
VertexType vexname;
ArcNode *first;
int visited;
} VNode, AdjList[MAX_VER_NUM];
//图
typedef struct {
AdjList Vertices;
int vexnum, arcnum;
} ALGraph;
//定义一个队列
typedef struct {
VNode *VQ[MAX_VER_NUM];
int rear, front;
} Queue;
void initALGraph(ALGraph *G);
VNode *firstNeighbour(ALGraph *G, VNode *w);
VNode *nextNeighbour(ALGraph *G, VNode *w, VNode *v);
int isEmpty(Queue *Q);
//入队操作
void enQueue(Queue *Q, VNode *v);
//出队操作
VNode *deQueue(Queue *Q);
void BFS(ALGraph *G, int i);
void BFSTraverse(ALGraph *G, int i);
void DFS(ALGraph *G, int i);
void DFSTraverse(ALGraph *G, int i);
The Method Of ALGraph.c函数实现文件
#include "ALGraph.h"
//DFS
void DFS(ALGraph *G, int i) {
VNode *w = &G->Vertices[i];
if (w->vexname == '0')
return;
for (VNode *w1 = firstNeighbour(G, w); w1->vexname != '0'; w1 = nextNeighbour(G, w, w1)) {
if (!w1->visited) {
w1->visited = 1;
int j;
for (j = 0; G->Vertices[j].vexname != w1->vexname; j++);
DFS(G, j);
printf("%c ", w1->vexname);
}
}
}
void DFSTraverse(ALGraph *G, int i) {
if (i < 1 || i >= G->vexnum)
return -1;
DFS(G, i - 1);
for (int j = 0; j < G->vexnum; j++)
if (!G->Vertices[j].visited)
DFS(G, j);
}
//BFS
void BFS(ALGraph *G, int i) {
Queue *Q = (Queue *)malloc(sizeof(Queue));
Q->front = 0;
Q->rear = 0;
VNode *w1 = &G->Vertices[i];
enQueue(Q, w1);
while (!isEmpty(Q)) {
w1 = deQueue(Q);
if (w1->vexname == '0')
return -1;
printf("%c ", w1->vexname);
w1->visited = 1;
for (VNode *w = firstNeighbour(G, w1); w->vexname != '0'; w = nextNeighbour(G, w1, w))
if (!w->visited) {
w->visited = 1;
enQueue(Q, w);
}
}
}
void BFSTraverse(ALGraph *G, int i) {
if (i < 1 || i >= G->vexnum)
return -1;
BFS(G, i - 1);
for (int j = 0; j < G->vexnum; j++) {
if (!G->Vertices[j].visited)
BFS(G, j);
}
}
//构造无向图的邻接表
void initALGraph(ALGraph *G) {
int x = 1;
int y = 1;
while (x <= 5) {
char vname;
scanf("%c", &vname);
getchar();
G->Vertices[G->vexnum].vexname = vname;
G->Vertices[G->vexnum].visited = 0;
G->Vertices[G->vexnum++].first = NULL;
x++;
}
// scanf("%d", &y);
while (y <= 5) {
int v1, v2;
//printf("输入结点1:(1~n)");
scanf("%d", &v1);
getchar();
//printf("输入结点2:(1~n)");
scanf("%d", &v2);
getchar();
//(v1,v2)
if (G->Vertices[v1 - 1].first == NULL) {
ArcNode *p = (ArcNode *)malloc(sizeof(ArcNode));
p->next = NULL;
p->vexname = G->Vertices[v2 - 1].vexname;
G->Vertices[v1 - 1].first = p;
} else {
ArcNode *p = G->Vertices[v1 - 1].first;
while (p->next != NULL)
p = p->next;
ArcNode *q = (ArcNode *)malloc(sizeof(ArcNode));
q->vexname = G->Vertices[v2 - 1].vexname;
q->next = NULL;
p->next = q;
}
//(v2,v1)
if (G->Vertices[v2 - 1].first == NULL) {
ArcNode *p = (ArcNode *)malloc(sizeof(ArcNode));
p->next = NULL;
p->vexname = G->Vertices[v1 - 1].vexname;
G->Vertices[v2 - 1].first = p;
} else {
ArcNode *p = G->Vertices[v2 - 1].first;
while (p->next != NULL)
p = p->next;
ArcNode *q = (ArcNode *)malloc(sizeof(ArcNode));
q->vexname = G->Vertices[v1 - 1].vexname;
q->next = NULL;
p->next = q;
}
G->arcnum++;
y++;
}
}
//查找w的下一个顶点
VNode *firstNeighbour(ALGraph *G, VNode *w) {
ArcNode *p = w->first;
VNode *x = (VNode *)malloc(sizeof(VNode));
x->vexname = '0';
if (p != NULL) {
for (int i = 0; i < G->vexnum; i++) {
if (G->Vertices[i].vexname == p->vexname)
return &G->Vertices[i];
}
} else
return x;
}
//查找沿w路径方向的v顶点的下衣个顶点
VNode *nextNeighbour(ALGraph *G, VNode *w, VNode *v) {
ArcNode *p = w->first;
VNode *x = (VNode *)malloc(sizeof(VNode));
x->vexname = '0';
if (v->vexname == '0')
return x;
while (p->vexname != v->vexname)
p = p->next;
p = p->next;
if (p != NULL) {
for (int i = 0; i < G->vexnum; i++) {
if (G->Vertices[i].vexname == p->vexname)
return &G->Vertices[i];
}
} else
return x;
}
//入队操作
void enQueue(Queue *Q, VNode *v) {
if (Q->rear == MAX_VER_NUM - 1)
return -1;
Q->VQ[Q->rear++] = v;
}
//出队操作
VNode *deQueue(Queue *Q) {
VNode *x = (VNode *)malloc(sizeof(VNode));
x->vexname = '0';
return Q->front > Q->rear ? x : Q->VQ[Q->front++];
}
int isEmpty(Queue *Q) {
if (Q->rear == 0 || Q->rear == Q->front)
return 1;
else
return 0;
}
test.c测试文件
#include "ALGraph.h"
int main() {
ALGraph *G = (ALGraph *)malloc(sizeof(ALGraph));
G->arcnum = 0;
G->vexnum = 0;
initALGraph(G);
printf("************邻接表****************\n");
for (int i = 0; i < G->vexnum; i++) {
printf("%c->", G->Vertices[i].vexname);
ArcNode *p = G->Vertices[i].first;
while (p->next != NULL) {
printf("%c->", p->vexname);
p = p->next;
}
printf("%c\n", p->vexname);
}
printf("**********************************\n");
printf("************BFS******************\n");
int n = 0;
scanf("%d", &n);
getchar();
BFSTraverse(G, n);
printf("\n*********************************\n");
printf("************DFS******************\n");
int s;
scanf("%d", &s);
for (int n = 0; n < G->vexnum; n++)
G->Vertices[n].visited = 0;
DFSTraverse(G, s);
printf("\n*********************************\n");
}