﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-★Snowhill ★-随笔分类-data structure</title><link>http://www.cppblog.com/snowhill/category/3286.html</link><description>&lt;font color=#5555aa &gt;&lt;b&gt; 颠沛必于是，造次必于是！&lt;/b&gt;&lt;/font&gt;</description><language>zh-cn</language><lastBuildDate>Tue, 20 May 2008 00:23:49 GMT</lastBuildDate><pubDate>Tue, 20 May 2008 00:23:49 GMT</pubDate><ttl>60</ttl><item><title>quicksort</title><link>http://www.cppblog.com/snowhill/archive/2006/12/31/17101.html</link><dc:creator>snowhill</dc:creator><author>snowhill</author><pubDate>Sun, 31 Dec 2006 11:55:00 GMT</pubDate><guid>http://www.cppblog.com/snowhill/archive/2006/12/31/17101.html</guid><wfw:comment>http://www.cppblog.com/snowhill/comments/17101.html</wfw:comment><comments>http://www.cppblog.com/snowhill/archive/2006/12/31/17101.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/snowhill/comments/commentRss/17101.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/snowhill/services/trackbacks/17101.html</trackback:ping><description><![CDATA[
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<span style="COLOR: #000000">#include </span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">iostream.h</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">
						<br />
				</span>
				<span style="COLOR: #0000ff">void</span>
				<span style="COLOR: #000000"> quick_sort(</span>
				<span style="COLOR: #0000ff">int</span>
				<span style="COLOR: #000000"> list[],</span>
				<span style="COLOR: #0000ff">int</span>
				<span style="COLOR: #000000"> left,</span>
				<span style="COLOR: #0000ff">int</span>
				<span style="COLOR: #000000"> right)<br />{<br />    </span>
				<span style="COLOR: #0000ff">int</span>
				<span style="COLOR: #000000"> i</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">left,j</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">right,temp</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">list[i];<br />    </span>
				<span style="COLOR: #0000ff">while</span>
				<span style="COLOR: #000000">(i</span>
				<span style="COLOR: #000000">&lt;</span>
				<span style="COLOR: #000000">j)<br />    {<br />    </span>
				<span style="COLOR: #0000ff">while</span>
				<span style="COLOR: #000000">((i</span>
				<span style="COLOR: #000000">&lt;</span>
				<span style="COLOR: #000000">j)</span>
				<span style="COLOR: #000000">&amp;&amp;</span>
				<span style="COLOR: #000000">(list[j]</span>
				<span style="COLOR: #000000">&gt;</span>
				<span style="COLOR: #000000">temp)) j</span>
				<span style="COLOR: #000000">--</span>
				<span style="COLOR: #000000">    ;<br />        list[i]</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">list[j];<br />    </span>
				<span style="COLOR: #0000ff">while</span>
				<span style="COLOR: #000000">((i</span>
				<span style="COLOR: #000000">&lt;</span>
				<span style="COLOR: #000000">j)</span>
				<span style="COLOR: #000000">&amp;&amp;</span>
				<span style="COLOR: #000000">(list[i]</span>
				<span style="COLOR: #000000">&lt;=</span>
				<span style="COLOR: #000000">temp)) i</span>
				<span style="COLOR: #000000">++</span>
				<span style="COLOR: #000000">;<br />        list[j]</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">list[i];<br />    }<br />    list[i]</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">temp;<br />    </span>
				<span style="COLOR: #0000ff">if</span>
				<span style="COLOR: #000000">(left</span>
				<span style="COLOR: #000000">&lt;</span>
				<span style="COLOR: #000000">i</span>
				<span style="COLOR: #000000">-</span>
				<span style="COLOR: #000000">1</span>
				<span style="COLOR: #000000">)quick_sort(list,left,i</span>
				<span style="COLOR: #000000">-</span>
				<span style="COLOR: #000000">1</span>
				<span style="COLOR: #000000">);<br />    </span>
				<span style="COLOR: #0000ff">if</span>
				<span style="COLOR: #000000">(i</span>
				<span style="COLOR: #000000">+</span>
				<span style="COLOR: #000000">1</span>
				<span style="COLOR: #000000">&lt;</span>
				<span style="COLOR: #000000">right)quick_sort(list,i</span>
				<span style="COLOR: #000000">+</span>
				<span style="COLOR: #000000">1</span>
				<span style="COLOR: #000000">,right);<br />}</span>
		</div>
<img src ="http://www.cppblog.com/snowhill/aggbug/17101.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/snowhill/" target="_blank">snowhill</a> 2006-12-31 19:55 <a href="http://www.cppblog.com/snowhill/archive/2006/12/31/17101.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>插入排序</title><link>http://www.cppblog.com/snowhill/archive/2006/12/27/16920.html</link><dc:creator>snowhill</dc:creator><author>snowhill</author><pubDate>Wed, 27 Dec 2006 14:20:00 GMT</pubDate><guid>http://www.cppblog.com/snowhill/archive/2006/12/27/16920.html</guid><wfw:comment>http://www.cppblog.com/snowhill/comments/16920.html</wfw:comment><comments>http://www.cppblog.com/snowhill/archive/2006/12/27/16920.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/snowhill/comments/commentRss/16920.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/snowhill/services/trackbacks/16920.html</trackback:ping><description><![CDATA[
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<span style="COLOR: #008080"> 1</span> <span style="COLOR: #000000">#include </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">iostream.h</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 2</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 3</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> insertsort(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> a[],</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> m)<br /></span><span style="COLOR: #008080"> 4</span> <span style="COLOR: #000000">{<br /></span><span style="COLOR: #008080"> 5</span> <span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> temp,j;<br /></span><span style="COLOR: #008080"> 6</span> <span style="COLOR: #000000">        <br /></span><span style="COLOR: #008080"> 7</span> <span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;j</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">m;j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br /></span><span style="COLOR: #008080"> 8</span> <span style="COLOR: #000000">        {<br /></span><span style="COLOR: #008080"> 9</span> <span style="COLOR: #000000">            <br /></span><span style="COLOR: #008080">10</span> <span style="COLOR: #000000">            </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(a[j</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">a[j])<br /></span><span style="COLOR: #008080">11</span> <span style="COLOR: #000000">            {<br /></span><span style="COLOR: #008080">12</span> <span style="COLOR: #000000">                temp</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">a[j];<br /></span><span style="COLOR: #008080">13</span> <span style="COLOR: #000000">                    </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">j</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;a[i]</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">temp</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">i</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">)<br /></span><span style="COLOR: #008080">14</span> <span style="COLOR: #000000">                    {<br /></span><span style="COLOR: #008080">15</span> <span style="COLOR: #000000">                    a[i</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">a[i];<br /></span><span style="COLOR: #008080">16</span> <span style="COLOR: #000000">                    }<br /></span><span style="COLOR: #008080">17</span> <span style="COLOR: #000000">                a[i</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">temp;<br /></span><span style="COLOR: #008080">18</span> <span style="COLOR: #000000">            }<br /></span><span style="COLOR: #008080">19</span> <span style="COLOR: #000000">        }<br /></span><span style="COLOR: #008080">20</span> <span style="COLOR: #000000">        <br /></span><span style="COLOR: #008080">21</span> <span style="COLOR: #000000">    <br /></span><span style="COLOR: #008080">22</span> <span style="COLOR: #000000">}</span></div>
<img src ="http://www.cppblog.com/snowhill/aggbug/16920.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/snowhill/" target="_blank">snowhill</a> 2006-12-27 22:20 <a href="http://www.cppblog.com/snowhill/archive/2006/12/27/16920.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>广度优先搜索</title><link>http://www.cppblog.com/snowhill/archive/2006/12/17/16539.html</link><dc:creator>snowhill</dc:creator><author>snowhill</author><pubDate>Sun, 17 Dec 2006 03:32:00 GMT</pubDate><guid>http://www.cppblog.com/snowhill/archive/2006/12/17/16539.html</guid><wfw:comment>http://www.cppblog.com/snowhill/comments/16539.html</wfw:comment><comments>http://www.cppblog.com/snowhill/archive/2006/12/17/16539.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/snowhill/comments/commentRss/16539.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/snowhill/services/trackbacks/16539.html</trackback:ping><description><![CDATA[
		<p>广度优先搜索</p>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000"> date:2006-12-15 by:snowhill</span>
				<span style="COLOR: #008000">
						<br />
				</span>
				<span style="COLOR: #000000">#include </span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">iostream.h</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">
						<br />
				</span>
				<span style="COLOR: #0000ff">#define</span>
				<span style="COLOR: #000000">   elemtype int</span>
				<span style="COLOR: #000000">
						<br />
				</span>
				<span style="COLOR: #0000ff">#define</span>
				<span style="COLOR: #000000">   MAX_VERTEX_NUM   20         </span>
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000">最大顶点数   </span>
				<span style="COLOR: #000000">
						<br />
				</span>
				<span style="COLOR: #0000ff">#define</span>
				<span style="COLOR: #000000">   MAX_EDGE_NUM   40           </span>
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000">最大边数   </span>
				<span style="COLOR: #000000">
						<br />
				</span>
				<span style="COLOR: #0000ff">int</span>
				<span style="COLOR: #000000">   visited[MAX_VERTEX_NUM];       </span>
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000">访问标志数组</span>
				<span style="COLOR: #008000">
						<br />
				</span>
				<span style="COLOR: #0000ff">struct</span>
				<span style="COLOR: #000000"> ArcNode{<br />    </span>
				<span style="COLOR: #0000ff">int</span>
				<span style="COLOR: #000000"> adjvex;<br />    ArcNode </span>
				<span style="COLOR: #000000">*</span>
				<span style="COLOR: #000000">NextArc;<br />};<br /></span>
				<span style="COLOR: #0000ff">struct</span>
				<span style="COLOR: #000000"> VNode{<br />    elemtype data;<br />    ArcNode </span>
				<span style="COLOR: #000000">*</span>
				<span style="COLOR: #000000">firstArc;<br />};<br />typedef VNode AdjList[MAX_VERTEX_NUM]; <br /></span>
				<span style="COLOR: #0000ff">struct</span>
				<span style="COLOR: #000000"> ALGraph<br />{<br />    AdjList vertices;<br />    </span>
				<span style="COLOR: #0000ff">int</span>
				<span style="COLOR: #000000"> vexnum,arcnum;<br />};<br />ALGraph G;<br />    <br /></span>
				<span style="COLOR: #0000ff">void</span>
				<span style="COLOR: #000000"> CreateDG(ALGraph </span>
				<span style="COLOR: #000000">&amp;</span>
				<span style="COLOR: #000000">G)<br />{<br />    </span>
				<span style="COLOR: #0000ff">int</span>
				<span style="COLOR: #000000"> i,j,k;<br />    ArcNode </span>
				<span style="COLOR: #000000">*</span>
				<span style="COLOR: #000000">p;<br />    cout</span>
				<span style="COLOR: #000000">&lt;&lt;</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">创建一个有向图:</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">&lt;&lt;</span>
				<span style="COLOR: #000000">endl;<br />    cout</span>
				<span style="COLOR: #000000">&lt;&lt;</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">请输入顶点数:</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000"> ;    cin</span>
				<span style="COLOR: #000000">&gt;&gt;</span>
				<span style="COLOR: #000000">G.vexnum; cout</span>
				<span style="COLOR: #000000">&lt;&lt;</span>
				<span style="COLOR: #000000">endl;<br />    cout</span>
				<span style="COLOR: #000000">&lt;&lt;</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">请输入边数:</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">;     cin</span>
				<span style="COLOR: #000000">&gt;&gt;</span>
				<span style="COLOR: #000000">G.arcnum; cout</span>
				<span style="COLOR: #000000">&lt;&lt;</span>
				<span style="COLOR: #000000">endl;<br />    <br />    </span>
				<span style="COLOR: #0000ff">for</span>
				<span style="COLOR: #000000">(i</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">1</span>
				<span style="COLOR: #000000">;i</span>
				<span style="COLOR: #000000">&lt;=</span>
				<span style="COLOR: #000000">G.vexnum;i</span>
				<span style="COLOR: #000000">++</span>
				<span style="COLOR: #000000">)<br />    {<br />     G.vertices[i].data</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">i;<br />     G.vertices[i].firstArc</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">NULL;<br />     }<br />    </span>
				<span style="COLOR: #0000ff">for</span>
				<span style="COLOR: #000000">(k</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">1</span>
				<span style="COLOR: #000000">;k</span>
				<span style="COLOR: #000000">&lt;=</span>
				<span style="COLOR: #000000">G.arcnum;k</span>
				<span style="COLOR: #000000">++</span>
				<span style="COLOR: #000000">)<br />    {<br />    cout</span>
				<span style="COLOR: #000000">&lt;&lt;</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">请输入第</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">&lt;&lt;</span>
				<span style="COLOR: #000000">k</span>
				<span style="COLOR: #000000">&lt;&lt;</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">条边:</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">&lt;&lt;</span>
				<span style="COLOR: #000000">endl;<br />    cin</span>
				<span style="COLOR: #000000">&gt;&gt;</span>
				<span style="COLOR: #000000">i</span>
				<span style="COLOR: #000000">&gt;&gt;</span>
				<span style="COLOR: #000000">j;<br />    p</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #0000ff">new</span>
				<span style="COLOR: #000000"> ArcNode;<br />    p</span>
				<span style="COLOR: #000000">-&gt;</span>
				<span style="COLOR: #000000">adjvex</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">j;<br />    p</span>
				<span style="COLOR: #000000">-&gt;</span>
				<span style="COLOR: #000000">NextArc</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">G.vertices[i].firstArc;<br />    G.vertices[i].firstArc</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">p;<br />    }<br />}<br /></span>
				<span style="COLOR: #0000ff">void</span>
				<span style="COLOR: #000000"> disp(ALGraph G)<br />{<br />    </span>
				<span style="COLOR: #0000ff">int</span>
				<span style="COLOR: #000000"> i;<br />    ArcNode </span>
				<span style="COLOR: #000000">*</span>
				<span style="COLOR: #000000">p;<br />    cout</span>
				<span style="COLOR: #000000">&lt;&lt;</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">建立的图为:</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">&lt;&lt;</span>
				<span style="COLOR: #000000">endl;<br />    </span>
				<span style="COLOR: #0000ff">for</span>
				<span style="COLOR: #000000">(i</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">1</span>
				<span style="COLOR: #000000">;i</span>
				<span style="COLOR: #000000">&lt;=</span>
				<span style="COLOR: #000000">G.vexnum;i</span>
				<span style="COLOR: #000000">++</span>
				<span style="COLOR: #000000">)<br />        {<br />        p</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">G.vertices[i].firstArc;<br />        </span>
				<span style="COLOR: #0000ff">while</span>
				<span style="COLOR: #000000">(p</span>
				<span style="COLOR: #000000">!=</span>
				<span style="COLOR: #000000">NULL)<br />            {<br />            cout</span>
				<span style="COLOR: #000000">&lt;&lt;</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">(</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">&lt;&lt;</span>
				<span style="COLOR: #000000">i</span>
				<span style="COLOR: #000000">&lt;&lt;</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">,</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">&lt;&lt;</span>
				<span style="COLOR: #000000">p</span>
				<span style="COLOR: #000000">-&gt;</span>
				<span style="COLOR: #000000">adjvex</span>
				<span style="COLOR: #000000">&lt;&lt;</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">)</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">; <br />            p</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">p</span>
				<span style="COLOR: #000000">-&gt;</span>
				<span style="COLOR: #000000">NextArc;<br />            }<br />        cout</span>
				<span style="COLOR: #000000">&lt;&lt;</span>
				<span style="COLOR: #000000">endl;<br />    }<br />    cout</span>
				<span style="COLOR: #000000">&lt;&lt;</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">*******************************</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">&lt;&lt;</span>
				<span style="COLOR: #000000">endl;<br />}<br /></span>
				<span style="COLOR: #0000ff">void</span>
				<span style="COLOR: #000000"> bfs(</span>
				<span style="COLOR: #0000ff">int</span>
				<span style="COLOR: #000000"> v)<br />{<br />  ArcNode </span>
				<span style="COLOR: #000000">*</span>
				<span style="COLOR: #000000">p;<br />  </span>
				<span style="COLOR: #0000ff">int</span>
				<span style="COLOR: #000000"> queue[MAX_VERTEX_NUM];<br />  </span>
				<span style="COLOR: #0000ff">int</span>
				<span style="COLOR: #000000"> front</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000">,rear</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000">,w;<br />  </span>
				<span style="COLOR: #0000ff">for</span>
				<span style="COLOR: #000000">(</span>
				<span style="COLOR: #0000ff">int</span>
				<span style="COLOR: #000000"> i</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000">;i</span>
				<span style="COLOR: #000000">&lt;=</span>
				<span style="COLOR: #000000">MAX_VERTEX_NUM;i</span>
				<span style="COLOR: #000000">++</span>
				<span style="COLOR: #000000">)<br />    visited[i]</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000">;<br />  cout</span>
				<span style="COLOR: #000000">&lt;&lt;</span>
				<span style="COLOR: #000000">v;<br />  visited[v]</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">1</span>
				<span style="COLOR: #000000">;<br />  rear</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">(rear</span>
				<span style="COLOR: #000000">+</span>
				<span style="COLOR: #000000">1</span>
				<span style="COLOR: #000000">)</span>
				<span style="COLOR: #000000">%</span>
				<span style="COLOR: #000000">MAX_VERTEX_NUM;<br />  queue[rear]</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">v;<br />  </span>
				<span style="COLOR: #0000ff">while</span>
				<span style="COLOR: #000000">(front</span>
				<span style="COLOR: #000000">!=</span>
				<span style="COLOR: #000000">rear)<br />    {<br />        front</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">(front</span>
				<span style="COLOR: #000000">+</span>
				<span style="COLOR: #000000">1</span>
				<span style="COLOR: #000000">)</span>
				<span style="COLOR: #000000">%</span>
				<span style="COLOR: #000000">MAX_VERTEX_NUM;<br />        w</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">queue[front];<br />        p</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">G.vertices[w].firstArc;<br />        </span>
				<span style="COLOR: #0000ff">while</span>
				<span style="COLOR: #000000">(p</span>
				<span style="COLOR: #000000">!=</span>
				<span style="COLOR: #000000">NULL)<br />            {<br />            </span>
				<span style="COLOR: #0000ff">if</span>
				<span style="COLOR: #000000">(visited[p</span>
				<span style="COLOR: #000000">-&gt;</span>
				<span style="COLOR: #000000">adjvex]</span>
				<span style="COLOR: #000000">==</span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000">)<br />                {visited[p</span>
				<span style="COLOR: #000000">-&gt;</span>
				<span style="COLOR: #000000">adjvex]</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">1</span>
				<span style="COLOR: #000000">;<br />                 cout</span>
				<span style="COLOR: #000000">&lt;&lt;</span>
				<span style="COLOR: #000000">p</span>
				<span style="COLOR: #000000">-&gt;</span>
				<span style="COLOR: #000000">adjvex;<br />                 rear</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">(rear</span>
				<span style="COLOR: #000000">+</span>
				<span style="COLOR: #000000">1</span>
				<span style="COLOR: #000000">)</span>
				<span style="COLOR: #000000">%</span>
				<span style="COLOR: #000000">MAX_VERTEX_NUM;<br />                 queue[rear]</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">p</span>
				<span style="COLOR: #000000">-&gt;</span>
				<span style="COLOR: #000000">adjvex;<br />                 }<br />            p</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">p</span>
				<span style="COLOR: #000000">-&gt;</span>
				<span style="COLOR: #000000">NextArc;<br />        }<br />    }<br />}<br />    <br /></span>
				<span style="COLOR: #0000ff">void</span>
				<span style="COLOR: #000000"> main()<br />{<br /> </span>
				<span style="COLOR: #0000ff">int</span>
				<span style="COLOR: #000000"> v;<br /> CreateDG(G);<br /> disp(G);<br /> cout</span>
				<span style="COLOR: #000000">&lt;&lt;</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">请输入广度优先遍历的顶点:</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">;<br /> cin</span>
				<span style="COLOR: #000000">&gt;&gt;</span>
				<span style="COLOR: #000000">v;  cout</span>
				<span style="COLOR: #000000">&lt;&lt;</span>
				<span style="COLOR: #000000">endl;<br /> cout</span>
				<span style="COLOR: #000000">&lt;&lt;</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">广度优先遍历为:</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">&lt;&lt;</span>
				<span style="COLOR: #000000">endl;<br /> bfs(v);  cout</span>
				<span style="COLOR: #000000">&lt;&lt;</span>
				<span style="COLOR: #000000">endl;<br /> }</span>
		</div>
<img src ="http://www.cppblog.com/snowhill/aggbug/16539.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/snowhill/" target="_blank">snowhill</a> 2006-12-17 11:32 <a href="http://www.cppblog.com/snowhill/archive/2006/12/17/16539.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>图的邻接表</title><link>http://www.cppblog.com/snowhill/archive/2006/12/03/15928.html</link><dc:creator>snowhill</dc:creator><author>snowhill</author><pubDate>Sun, 03 Dec 2006 08:42:00 GMT</pubDate><guid>http://www.cppblog.com/snowhill/archive/2006/12/03/15928.html</guid><wfw:comment>http://www.cppblog.com/snowhill/comments/15928.html</wfw:comment><comments>http://www.cppblog.com/snowhill/archive/2006/12/03/15928.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/snowhill/comments/commentRss/15928.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/snowhill/services/trackbacks/15928.html</trackback:ping><description><![CDATA[
		<p>//DATE:2006-12-03    BY:snowhill<br /><font color="#ff00cc">/* 1---2   以邻接表存放应为: 1-&gt;5-&gt;3-&gt;2<br />     | \  / |       2-&gt;4-&gt;3-&gt;1<br />     |  3  |       3-&gt;5-&gt;4-&gt;2-&gt;1<br />     | /  \ |        4-&gt;5-&gt;3-&gt;2<br />    5---4       5-&gt;4-&gt;3-&gt;1<br /> */</font><br /><font color="#0000ff">/*  图的邻接表  以a[1]为起始地起址存放 */</font><br />#include "iostream.h"<br />const int n=5;<br />const int e=8;<br />int visited[n+1]; <font color="#0000ff">//访问标志数组<br /></font>struct link<br />{<br /> int data;<br /> link *next;<br />};<br />struct node<br />{<br /> int v;   <font color="#0000ff">  //顶点信息<br /></font> link *next;<br />}a[n+1];</p>
		<p>
				<font color="#0000ff">/* 无向图的建立 */<br /></font>void createlink()<br />{<br /> int i,j,k;<br /> link *s;<br /><font color="#0000ff"> //初始化</font><br /> for(i=1;i&lt;=n;i++)<br /> {<br />  a[i].v=i;<br />  a[i].next=NULL;<br /> }</p>
		<p>
				<font color="#0000ff"> /*以头插法建立 */</font>
		</p>
		<p>  for(k=1;k&lt;=e;k++)<br /> {<br />  cout&lt;&lt;"请输入一条边:";<br />  cin&gt;&gt;i&gt;&gt;j;<br />  cout&lt;&lt;endl;<br />  s=new link;<br />  s-&gt;data=j;<br />  s-&gt;next=a[i].next;<br />  a[i].next=s;<br />  s=new link;<br />  s-&gt;data=i;<br />  s-&gt;next=a[j].next;<br />  a[j].next=s;<br /> }<br />}</p>
		<p>
				<font color="#0000ff">/* 深度优先搜索 */</font>
				<br />void dfs(int i)<br />{<br />  link *p;<br />  cout&lt;&lt;a[i].v&lt;&lt;" ";<br />  visited[i]=1;<br />  p=a[i].next;<br />  while(p!=NULL)<br />  {<br />   if(!visited[p-&gt;data])<br />   dfs(p-&gt;data);<br />   p=p-&gt;next;<br />  }<br />}</p>
		<p>
				<br />void main()<br />{  <br />    link *p;char yn='y';int k;<br /> createlink();<br /> while(yn=='y')<br /> {   <br />  cout&lt;&lt;"你建立的图为:"&lt;&lt;endl;<br />  for(int i=1;i&lt;=n;i++)<br />  {<br />  p=a[i].next;<br />  cout&lt;&lt;a[i].v&lt;&lt;"-&gt;";<br />  while(p-&gt;next!=NULL)<br />   {<br />    cout&lt;&lt;p-&gt;data&lt;&lt;"-&gt;";<br />    p=p-&gt;next;<br />   }<br />  cout&lt;&lt;p-&gt;data&lt;&lt;endl;<br />  }<br />    for(i=1;i&lt;=n;i++)<br /> visited[i]=0; <br /> cout&lt;&lt;"请输入深度优先搜索的顶点:";<br /> cin&gt;&gt;k;<br /> cout&lt;&lt;endl;<br />  <br /> dfs(k);<br /> cout&lt;&lt;"要继续遍历吗(Y/N)?";<br /> cin&gt;&gt;yn;<br /> }<br /> <br />    <br />}</p>
<img src ="http://www.cppblog.com/snowhill/aggbug/15928.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/snowhill/" target="_blank">snowhill</a> 2006-12-03 16:42 <a href="http://www.cppblog.com/snowhill/archive/2006/12/03/15928.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>先序非递归遍历</title><link>http://www.cppblog.com/snowhill/archive/2006/12/03/15913.html</link><dc:creator>snowhill</dc:creator><author>snowhill</author><pubDate>Sun, 03 Dec 2006 01:13:00 GMT</pubDate><guid>http://www.cppblog.com/snowhill/archive/2006/12/03/15913.html</guid><wfw:comment>http://www.cppblog.com/snowhill/comments/15913.html</wfw:comment><comments>http://www.cppblog.com/snowhill/archive/2006/12/03/15913.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/snowhill/comments/commentRss/15913.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/snowhill/services/trackbacks/15913.html</trackback:ping><description><![CDATA[
		<font style="BACKGROUND-COLOR: #7fffd4">//先序非递归遍历</font>
		<br />void <strong>preorder</strong>(bitree *BST)<br />{<br /> bitree *p,*s[100];<br /> int top=0;<br /> p=BST;<br /> while((p!=NULL)||(top&gt;0))<br /> {<br />  while(p!=NULL)<br />  {<br />   cout&lt;&lt;p-&gt;data&lt;&lt;" "&lt;&lt;endl;<br />   s[++top]=p;<br />     //  cout&lt;&lt;"the value of top is "&lt;&lt;top&lt;&lt;endl;<br />   p=p-&gt;lchild;<br />  }<br />   p=s[top--];   <font color="#0000cc">//注意TOP值的变化<br /></font>   // cout&lt;&lt;"*******the value of top is "&lt;&lt;top&lt;&lt;endl;<br />   p=p-&gt;rchild;<br /> }<br />}<br /><img src ="http://www.cppblog.com/snowhill/aggbug/15913.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/snowhill/" target="_blank">snowhill</a> 2006-12-03 09:13 <a href="http://www.cppblog.com/snowhill/archive/2006/12/03/15913.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Binary Tree </title><link>http://www.cppblog.com/snowhill/archive/2006/12/02/15892.html</link><dc:creator>snowhill</dc:creator><author>snowhill</author><pubDate>Sat, 02 Dec 2006 11:31:00 GMT</pubDate><guid>http://www.cppblog.com/snowhill/archive/2006/12/02/15892.html</guid><wfw:comment>http://www.cppblog.com/snowhill/comments/15892.html</wfw:comment><comments>http://www.cppblog.com/snowhill/archive/2006/12/02/15892.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/snowhill/comments/commentRss/15892.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/snowhill/services/trackbacks/15892.html</trackback:ping><description><![CDATA[
		<p>/* DATE: 2006-12-02 BY:snowhill */</p>
		<p>#include "iostream.h"<br />/* definition */<br />struct bitree<br />{<br /> int data;<br /> bitree *lchild,*rchild;<br />};<br />//插入<br />void Insert(<font color="#ff0099">bitree *&amp;BST</font>,int x)<br />{   <br /> if(BST==NULL)<br /> {<br />  bitree *p=new bitree;<br />  p-&gt;data=x;<br />  <br />  p-&gt;lchild=p-&gt;rchild=NULL;<br />  BST=p;<br /> }<br /> else if(BST-&gt;data&gt;=x)<br />   Insert(BST-&gt;lchild,x);<br /> else <br />  Insert(BST-&gt;rchild,x);<br />}</p>
		<p>//create a binary Tree with n nodes</p>
		<p>bitree *create(bitree *BST,int n)<br />{   <br /> int x;<br /> BST=NULL;<br /> for(int i=1;i&lt;=n;i++)<br /> {  <br />  cin&gt;&gt;x;<br />  Insert(BST,x);<br /> }<br /> if(BST==NULL)<br />  cout&lt;&lt;"It must be fun!"&lt;&lt;endl;<br /> return BST;<br />  <br />}<br />//先序遍历<br />void preorder(bitree *BST)<br />{<br /> bitree *p;<br /> p=BST;<br /> if(p!=NULL)<br /> {<br />  <br /> cout&lt;&lt;p-&gt;data&lt;&lt;"-&gt;";<br /> preorder(p-&gt;lchild);<br /> preorder(p-&gt;rchild);<br /> }<br /> <br />}</p>
		<p>/*  主函数  */<br />void main()<br />{<br /> bitree *BST;<br /> BST=NULL;<br /> BST=create(BST,4);<br /> preorder(BST);<br />    <br />}</p>
<img src ="http://www.cppblog.com/snowhill/aggbug/15892.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/snowhill/" target="_blank">snowhill</a> 2006-12-02 19:31 <a href="http://www.cppblog.com/snowhill/archive/2006/12/02/15892.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>