完整代码
#include<stdarg.h>
#include<malloc.h>
#include<stdio.h>
#define MAX_VERTEX_NUM 50
#define VType int
#define InfoType char
typedef enum {false,true} bool;
bool visited[MAX_VERTEX_NUM];
typedef struct
{
int weight;
InfoType info;
}Matrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedef struct
{
VType vertex[MAX_VERTEX_NUM];
Matrix arcs;
int arcNum,vertexNum;
}MGraph;
typedef struct Node
{
VType data;
struct Node * l_child;
struct Node * nextSibling;
}*Tree,Node;
typedef struct Queue
{
//队列中存放的为树结点
Tree node;
struct Queue * next;
}Queue;
int locateVertex(MGraph *g, VType vertex)
{
for (int i = 0; i < g->vertexNum; ++i) {
if (g->vertex[i] == vertex)
{
return i;
}
}
return -1;
}
void createDN(MGraph * g)
{
g->arcNum = 13;
g->vertexNum = 13;
// printf("请输入顶点数据 \n");
// for (int i = 0; i < g->vertexNum; ++i) {
// //strtol
// scanf("%d",&(g->vertex[i]));
// }
for (int i = 0; i < g->vertexNum; ++i) {
//strtol
g->vertex[i] = i+1;
}
for (int k = 0; k < g->vertexNum; ++k) {
for (int i = 0; i < g->vertexNum; ++i) {
g->arcs[k][i].weight = 00;
g->arcs[k][i].info = NULL;
}
}
// printf("请输入弧头和弧尾 \n");
// for (int j = 0; j < g->vertexNum; ++j) {
// VType v1,v2;
// scanf("%d,%d",&v1,&v2);
// int n = locateVertex(g,v1);
// int m = locateVertex(g,v2);
// g->arcs[n][m].weight = 1;
// g->arcs[m][n].weight = 1;
// }
int n = 0, m = 1;
g->arcs[n][m].weight = 1;
g->arcs[m][n].weight = 1;
m = 2;
g->arcs[n][m].weight = 1;
g->arcs[m][n].weight = 1;
m = 5;
g->arcs[n][m].weight = 1;
g->arcs[m][n].weight = 1;
n = 0, m = 11;
g->arcs[n][m].weight = 1;
g->arcs[m][n].weight = 1;
n = 1, m = 12;
g->arcs[n][m].weight = 1;
g->arcs[m][n].weight = 1;
n = 4, m = 2;
g->arcs[n][m].weight = 1;
g->arcs[m][n].weight = 1;
m = 7, n = 6;
g->arcs[n][m].weight = 1;
g->arcs[m][n].weight = 1;
m = 9;
g->arcs[n][m].weight = 1;
g->arcs[m][n].weight = 1;
m = 8;
g->arcs[n][m].weight = 1;
g->arcs[m][n].weight = 1;
n = 7, m = 9;
g->arcs[n][m].weight = 1;
g->arcs[m][n].weight = 1;
n = 11,m=10;
g->arcs[n][m].weight = 1;
g->arcs[m][n].weight = 1;
n=12;
g->arcs[n][m].weight = 1;
g->arcs[m][n].weight = 1;
m=11;
g->arcs[n][m].weight = 1;
g->arcs[m][n].weight = 1;
}
void initQueue(Queue ** q)
{
(*q) = (Queue*)malloc(sizeof(Queue));
(*q)->next = NULL;
}
void deQueue(Queue ** q,Tree * t)
{
(*t) = (*q)->next->node;
(*q)->next = (*q)->next->next;
}
bool isEmpty(Queue * q)
{
if (q->next)
return false;
else
return true;
}
int firstAdjVex(MGraph * g,int v)
{
for (int i = 0; i < g->vertexNum; ++i) {
if (g->arcs[v][i].weight == 1)
{
return i;
}
}
return -1;
}
int nextAdjVex(MGraph * g,int v,int w)
{
for (int i = w+1; i < g->vertexNum; ++i) {
if (g->arcs[v][i].weight == 1)
{
return i;
}
}
return -1;
}
void enQueue(Queue ** q,Tree t)
{
Queue * e = malloc(sizeof(Queue));
e->node = t;
e->next = NULL;
Queue * temp = (*q);
while (temp->next)
{
temp = temp->next;
}
temp->next = e;
}
//Tree t
void bfsTree(MGraph * g,Tree * t)
{
Queue * q = NULL;
initQueue(&q);
//根结点入队
enQueue(&q, (*t)); // t
//存储出队列的结点
Tree p = NULL;
//while (q)
while (!isEmpty(q))
{
bool first = true;
deQueue(&q,&p);
//判断出队结点中的数据在数组中的具体位置
int v = locateVertex(g,p->data);
visited[v] = true;
for (int i = firstAdjVex(g,v); i > -1; i = nextAdjVex(g,v,i)) {
if (!visited[i])
{
Tree temp = malloc(sizeof(Node));
temp->data = g->vertex[i];
temp->l_child = NULL;
temp->nextSibling = NULL;
enQueue(&q,temp);
if (first)
{
p->l_child = temp;
first = false;
}
else
{
p->nextSibling = temp;
}
visited[i] = true;
p = temp;
}
}
}
}
//Tree t
void bfsForest(MGraph * g, Tree * t)
{
(*t) = NULL;
Tree q = NULL;
for (int i = 0; i < g->vertexNum; ++i) {
visited[i] = false;
}
for (int j = 0; j < g->vertexNum; ++j) {
if (!visited[j])
{
Tree p = malloc(sizeof(Node));
p->data = g->vertex[j];
p->nextSibling = NULL;
p->l_child = NULL;
//如果树为空,则该顶点作为树的树根
if (!(*t))
{
(*t) = p;
}
else
{
q->nextSibling = p;
}
q = p;
bfsTree(g,t);
}
}
}
void preOrderTraverse(Tree t)
{
if (t)
{
printf("%d ",t->data);
preOrderTraverse(t->l_child);
preOrderTraverse(t->nextSibling);
}
}
int main(int argc, char* argv[])
{
MGraph g;
createDN(&g);
Tree t;
bfsForest(&g,&t);
preOrderTraverse(t);
return 0;
}
|