c语言node程序,DFS和BFS完整c语言可运行程序

二、广度优先算法(BFS)

#include

#include

#define MAX_VEXTEX_NUM 9

#define ARC_NUM 12

#define MAX_QUEUEMEM (MAX_VEXTEX_NUM+1)

typedef struct{

int

queuemem[MAX_QUEUEMEM];

int header;

int rear;

}QUEUE;

int GraphEdge[ARC_NUM * 2][2] =

{{0,1},{1,0},{1,2},{2,1},{2,3},{3,2},{3,4},

{4,3},{4,5},{5,4},{5,0},{0,5},{0,6},{6,0},{6,8},

{8,6},{6,7},{7,6},{7,8},{8,7},{7,3},{3,7},{8,4},{4,8}};

int

visited[MAX_VEXTEX_NUM]={0,0,0,0,0,0,0,0,0};

typedef struct ArcNode{

int

adjvex;

struct ArcNode *

nextarc;

}ArcNode;

typedef struct VNode{

int data;

struct ArcNode *

firstarc;

}VNode,AdjList[MAX_VEXTEX_NUM];

typedef struct {

VNode vertices[MAX_VEXTEX_NUM];

int vexnum;

int arcnum;

}ALGraph;

void InitQueue(QUEUE *queue)

{

queue->header = 0;

queue->rear = 0;

}

void EnQueue(QUEUE *queue,int v)

{

queue->queuemem[queue->rear] = v;

queue->rear++;

}

int DelQueue(QUEUE *queue)

{

return queue->queuemem[queue->header++];

}

int EmptyQueue(QUEUE *queue)

{

if(queue->header

== queue->rear)

return 1;

return 0;

}

void CreateGraph(ALGraph * alGraph)

{

int i,j;

ArcNode * newnode;

ArcNode * vexNode;

alGraph->vexnum = MAX_VEXTEX_NUM;

alGraph->arcnum = ARC_NUM;

for(i=0;i

{

alGraph->vertices[i].data = i;

alGraph->vertices[i].firstarc =

NULL;

}

for(j=0;j<2*ARC_NUM;j++)

{

i = GraphEdge[j][0];

if(alGraph->vertices[i].firstarc==NULL)

{

newnode = ( ArcNode *

) malloc (sizeof(ArcNode));

newnode->adjvex =

GraphEdge[j][1];

newnode->nextarc =

NULL;

alGraph->vertices[i].firstarc

= newnode;

}

else

{

vexNode =

alGraph->vertices[i].firstarc;

while(vexNode->nextarc

!= NULL)

{

vexNode = vexNode->nextarc;

}

newnode = ( ArcNode *

) malloc (sizeof(ArcNode));

newnode->adjvex =

GraphEdge[j][1];

newnode->nextarc =

NULL;

vexNode->nextarc =

newnode;

}

}

}

void OutputGraph(ALGraph * alGraph)

{

int i;

ArcNode * vexNode;

printf("The graph dedicated by adjacency list

is:\n");

for(i=0;i

{

printf("the header is: [%d] ->

",alGraph->vertices[i].data);

vexNode =

alGraph->vertices[i].firstarc;

while(vexNode != NULL)

{

printf("[%d] ->

",vexNode->adjvex);

vexNode=vexNode->nextarc;

}

printf("[END]\n");

}

}

void BFSTraverse(ALGraph * alGraph)

{

int i;

int w;

ArcNode * vexNode;

QUEUE queue;

InitQueue(&queue);

for(i=0;i

{

visited[i] = 0;

}

printf("\n");

puts("********************************************");

puts("* the function

BFSTraverse will traverse *");

puts("*

the graph by Breadth First Search

*");

puts("********************************************");

puts("the result of BFS is:");

for(i=0;i

{

if(visited[i] == 0)

{

visited[i] = 1;

printf("[%d] -> ",i);

EnQueue(&queue,i);

while(!EmptyQueue(&queue))

{

w = DelQueue(&queue);

vexNode = alGraph->vertices[w].firstarc;

while(vexNode != NULL)

{

w = vexNode->adjvex;

if(visited[w]==0)

{

visited[w] = 1;

printf("[%d] -> ",w);

EnQueue(&queue,w);

}

vexNode = vexNode->nextarc;

}

}

}

}

printf("[end]\n");

}

int main()

{

ALGraph alGraph;

CreateGraph(&alGraph);

OutputGraph(&alGraph);

BFSTraverse(&alGraph);

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值