📄 图遍历应用_数据结构与算法_数据结构算法_c语言_c 语言之家.htm
字号:
<TD>
<FORM action=Readnews.asp?newsid=1294&id2=1294
method=post name=form1>
<CENTER><!-- <input type=submit name=aa value="点击关闭浮动图标" width=20 title="点击广告支持本站">--></CENTER></FORM></TD></TR>
<TR>
<TD align=middle bgColor=#dddddd height=20
style="FONT-SIZE: 18px" vAlign=bottom
width="85%"><STRONG><FONT color=#003399 size=4><B>图遍历应用
</B></FONT></STRONG></TD><BR></TR>
<TR>
<TD align=middle width="100%"><BR></TD></TR>
<TR>
<TD align=middle style="FONT-SIZE: 9pt"
width="100%">发表日期:2003年6月9日 已经有3131位读者读过此文</TD></TR>
<TR>
<TD align=middle width="100%"><!--下面的这一句是设置阅读文本区的宽度-->
<TABLE align=center border=0 cellPadding=0 cellSpacing=0
style="TABLE-LAYOUT: fixed" width="90%">
<TBODY>
<TR>
<TD align=middle width="100%"></TD></TR>
<TR>
<TD style="WORD-WRAP: break-word"><FONT
class=news><BR>
<P>/* ========================================
*/<BR>/*
图形的遍历
*/<BR>/* ========================================
*/<BR>#include <stdlib.h><BR>#define
MAXQUEUE
70
/* 伫列的最大容量
*/</P>
<P>struct
node
/* 图形顶点结构宣告
*/<BR>{<BR> int
vertex;
/*
顶点资料
*/<BR> struct node
*nextnode;
/* 指下一顶点的指标
*/<BR>};<BR>typedef struct node
*graph; /*
图形的结构新型态 */<BR>struct node
head[61];
/* 图形顶点结构数组 */<BR>int
visited[61];
/*
遍历记录数组
*/</P>
<P>int
queue[MAXQUEUE];
/* 伫列的数组宣告
*/<BR>int front =
-1;
/*
伫列的前端
*/<BR>int rear =
-1;
/*
伫列的后端
*/</P>
<P>/* ----------------------------------------
*/<BR>/*
建立图形
*/<BR>/* ----------------------------------------
*/<BR>void creategraph(int *node,int
num)<BR>{<BR> graph
newnode;
/*
新顶点指标
*/<BR> graph ptr;<BR> int
from;
/*
边线的起点
*/<BR> int
to;
/*
边线的终点
*/<BR> int i;</P>
<P> for ( i = 0; i < num; i++
) /*
读取边线的回路
*/<BR>
{<BR> from =
node[i*2];
/*
边线的起点
*/<BR> to =
node[i*2+1];
/*
边线的终点
*/<BR> /* 建立新顶点记忆体
*/<BR> newnode = (
graph ) malloc(sizeof(struct
node));<BR>
newnode->vertex =
to; /*
建立顶点内容
*/<BR>
newnode->nextnode = NULL; /*
设定指标初值
*/<BR> ptr =
&(head[from]);
/*
顶点位置
*/<BR> while (
ptr->nextnode != NULL ) /*
遍历至链表尾
*/<BR>
ptr =
ptr->nextnode;
/* 下一个顶点
*/<BR>
ptr->nextnode =
newnode;
/*
插入结尾
*/<BR> }<BR>}</P>
<P>/* ----------------------------------------
*/<BR>/*
伫列资料的存入
*/<BR>/* ----------------------------------------
*/<BR>int enqueue(int value)<BR>{<BR>
if ( rear >= MAXQUEUE
) /*
检查伫列是否全满
*/<BR> return
-1;
/*
无法存入
*/<BR>
rear++;
/* 后端指标往前移
*/<BR> queue[rear] =
value;
/*
存入伫列
*/<BR>}</P>
<P>/* ----------------------------------------
*/<BR>/*
伫列资料的取出
*/<BR>/* ----------------------------------------
*/<BR>int dequeue()<BR>{<BR> if (
front == rear
)
/* 检查伫列是否是空
*/<BR> return
-1;
/*
无法取出
*/<BR>
front++;
/* 前端指标往前移
*/<BR> return
queue[front];
/*
伫列取出
*/<BR>}</P>
<P>/* ----------------------------------------
*/<BR>/*
图形的广度优先搜寻法
*/<BR>/* ----------------------------------------
*/<BR>void bfs(int current)<BR>{<BR>
graph ptr;</P>
<P> /* 处理第一个顶点 */<BR>
enqueue(current);
/* 将顶点存入伫列
*/<BR> visited[current] =
1;
/*
记录已遍历过
*/<BR>
printf("[%d]
",current); /*
印出遍历顶点值
*/<BR> while ( front != rear
) /*
伫列是否是空的
*/<BR>
{<BR> current =
dequeue();
/* 将顶点从伫列取出
*/<BR> ptr =
head[current].nextnode; /*
顶点位置
*/<BR> while ( ptr
!= NULL
)
/* 遍历至链表尾
*/<BR>
{<BR>
if ( visited[ptr->vertex] == 0 ) /* 如过没遍历过
*/<BR>
{<BR>
enqueue(ptr->vertex);
/* 递回遍历呼叫
*/<BR>
visited[ptr->vertex] = 1; /*
记录已遍历过
*/<BR>
/* 印出遍历顶点值 */<BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -