ÓÑÇéÌáʾ£ºÈç¹û±¾ÍøÒ³´ò¿ªÌ«Âý»òÏÔʾ²»ÍêÕû£¬Çë³¢ÊÔÊó±êÓÒ¼ü¡°Ë¢Ð¡±±¾ÍøÒ³£¡
VB2008´ÓÈëÃŵ½¾«Í¨(PDF¸ñʽӢÎÄ°æ)-µÚ25²¿·Ö
¿ì½Ý²Ù×÷: °´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·ҳ °´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ °´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿! Èç¹û±¾ÊéûÓÐÔĶÁÍ꣬ÏëÏ´μÌÐø½Ó×ÅÔĶÁ£¬¿ÉʹÓÃÉÏ·½ "Êղص½ÎÒµÄä¯ÀÀÆ÷" ¹¦ÄÜ ºÍ "¼ÓÈëÊéÇ©" ¹¦ÄÜ£¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡demonstrates¡¡that¡¡when¡¡you¡¡call¡¡a¡¡method£»¡¡you¡¡are¡¡assigning¡¡the¡¡parameters¡¡of¡¡the¡¡method¡¡to¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡variables¡¡in¡¡the¡¡called¡¡method¡£¡¡Looking¡¡back¡¡at¡¡Table¡¡4¡1£»¡¡you¡¡can¡¡see¡¡that¡¡when¡¡you¡¡assign¡¡a¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡value¡¡to¡¡a¡¡value¡¡type£»¡¡manipulating¡¡the¡¡assigned¡¡instance¡¡does¡¡not¡¡change¡¡the¡¡original¡¡instance¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡However£»¡¡you¡¡can¡¡gain¡¡some¡¡control¡¡over¡¡how¡¡parameters¡¡are¡¡handled¡£¡¡When¡¡Method£¨£©¡¡is¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡declared£»¡¡the¡¡assumption¡¡is¡¡made¡¡that¡¡all¡¡of¡¡the¡¡parameters¡¡are¡¡passed¡¡to¡¡the¡¡method¡¡imple
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡mentation¡¡as¡¡value¡¡types¡£¡¡It¡¡is¡¡possible¡¡to¡¡pass¡¡the¡¡parameters¡¡to¡¡the¡¡method¡¡as¡¡reference¡¡types£»¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡as¡¡determined¡¡by¡¡one¡¡of¡¡the¡¡following¡¡keywords£º¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ByVal£º¡¡When¡¡a¡¡method¡¡has¡¡parameters¡¡declared¡¡with¡¡the¡¡ByVal¡¡keyword£»¡¡the¡¡parameter¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡has¡¡its¡¡value¡¡copied¡¡from¡¡the¡¡caller¡¡variable¡¡to¡¡method¡¡implementation¡¡variable£»¡¡even¡¡if¡¡it¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡is¡¡a¡¡reference¡¡type¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ByRef£º¡¡When¡¡a¡¡method¡¡has¡¡parameters¡¡declared¡¡with¡¡the¡¡ByRef¡¡keyword£»¡¡the¡¡caller¡¡variable¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡is¡¡not¡¡copied¡¡to¡¡the¡¡parameter¡¡variable£»¡¡rather£»¡¡the¡¡variable¡¡used¡¡by¡¡the¡¡caller¡¡and¡¡the¡¡method¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡variable¡¡are¡¡one¡¡and¡¡the¡¡same¡£¡¡Thus£»¡¡changes¡¡to¡¡the¡¡method¡¡variable£»¡¡as¡¡in¡¡the¡¡previous¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡example£»¡¡would¡¡be¡¡visible¡¡to¡¡the¡¡caller¡¡of¡¡the¡¡method¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Figure¡¡4¡6¡¡illustrates¡¡how¡¡to¡¡use¡¡the¡¡ByRef¡¡keyword¡¡so¡¡that¡¡a¡¡modified¡¡value¡¡type¡¡will¡¡be¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡visible¡¡to¡¡the¡¡caller¡¡of¡¡the¡¡method¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Now¡¡that¡¡you¡¡have¡¡an¡¡understanding¡¡of¡¡the¡¡depth¡first¡¡search¡¡algorithm¡¡and¡¡how¡¡the¡¡data¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡structure¡¡will¡¡be¡¡defined¡¡as¡¡a¡¡user¡defined¡¡value¡¡type£»¡¡let¡¯s¡¡get¡¡started¡¡building¡¡the¡¡search¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡algorithm¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡109¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡4¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡I¡¡N¡¡G¡¡¡¡¡¡A¡¡B¡¡OU¡¡T¡¡¡¡¡¡D¡¡AT¡¡A¡¡¡¡S¡¡TR¡¡U¡¡CT¡¡U¡¡R¡¡E¡¡S£»¡¡¡¡¡¡DE¡¡CI¡¡SI¡¡ON¡¡S£»¡¡¡¡¡¡A¡¡N¡¡D¡¡¡¡¡¡L¡¡O¡¡OP¡¡S¡¡87¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡ByRef¡¡is¡¡associated¡¡with¡¡the¡¡parameter¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡indicating¡¡that¡¡the¡¡value¡¡variable¡¡can¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡be¡¡modified¡¡in¡¡the¡¡method¡¡and¡¡the¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡result¡¡will¡¡be¡¡visible¡¡to¡¡the¡¡caller¡¡
¡¡Sub¡¡Method£¨ByRef¡¡value¡¡As¡¡MyValueType£»¡¡ByVal¡¡reference¡¡As¡¡MyReferenceType£©¡¡
¡¡¡¡¡¡¡¡¡¡value¡£value¡¡=¡¡10¡¡
¡¡¡¡¡¡¡¡¡¡reference¡£value¡¡=¡¡10¡¡
¡¡End¡¡Sub¡¡
¡¡Dim¡¡value¡¡As¡¡MyValueType¡¡
¡¡Dim¡¡reference¡¡As¡¡MyReferenceType¡¡=¡¡New¡¡MyReferenceType£¨£©¡¡
¡¡Method£¨value£»¡¡reference£©¡¡
¡¡Console¡£WriteLine£¨¡¨value¡¡value=¡¨¡¡&¡¡value¡£value¡¡&¡¡_¡¡
¡¡¡¡¡¡¡¡¡¡¡¨¡¡reference¡¡value=¡¨¡¡&¡¡reference¡£value£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡variable¡¡value¡£value¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡will¡¡contain¡¡the¡¡value¡¡of¡¡10¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡assigned¡¡in¡¡the¡¡method¡¡
Figure¡¡4¡6¡£¡¡¡¡Using¡¡the¡¡ByRef¡¡keyword¡¡
Organizing¡¡the¡¡Search¡¡Algorithm¡¡
The¡¡search¡¡algorithm¡¡we¡¡will¡¡write¡¡in¡¡this¡¡chapter¡¡deals¡¡with¡¡the¡¡problem¡¡of¡¡planning¡¡a¡¡flight¡¡¡¡
from¡¡point¡¡A¡¡to¡¡point¡¡B¡£¡¡The¡¡first¡¡step¡¡is¡¡to¡¡figure¡¡out¡¡the¡¡features¡¡we¡¡need¡¡to¡¡implement¡£¡¡Here¡¯s¡¡¡¡
a¡¡summary£º¡¡
¡¡¡¡¡¡¡¡¡¡o¡¡¡¡A¡¡data¡¡structure¡¡implements¡¡the¡¡node¡£¡¡
¡¡¡¡¡¡¡¡¡¡o¡¡¡¡A¡¡node¡¡can¡¡contain¡¡references¡¡to¡¡other¡¡nodes¡£¡¡
¡¡¡¡¡¡¡¡¡¡o¡¡¡¡Each¡¡node¡¡has¡¡a¡¡description¡¡and¡¡unique¡¡identifier¡¡to¡¡distinguish¡¡it¡¡from¡¡other¡¡nodes¡£¡¡
¡¡¡¡¡¡¡¡¡¡o¡¡¡¡All¡¡of¡¡the¡¡nodes¡¡have¡¡flight¡¡information¡£¡¡
¡¡¡¡¡¡¡¡¡¡o¡¡¡¡An¡¡algorithm¡¡will¡¡traverse¡¡the¡¡nodes¡¡and¡¡keep¡¡track¡¡of¡¡its¡¡path¡£¡¡
¡¡¡¡¡¡¡¡¡¡o¡¡¡¡The¡¡path¡¡is¡¡returned¡¡as¡¡a¡¡list¡¡of¡¡nodes¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡The¡¡data¡¡structure¡¡is¡¡based¡¡on¡¡the¡¡problem¡¡of¡¡planning¡¡a¡¡flight¡¡between¡¡two¡¡places£»¡¡as¡¡¡¡
illustrated¡¡in¡¡Figure¡¡4¡7¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡110¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
88¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡4¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡OU¡¡T¡¡¡¡¡¡D¡¡AT¡¡A¡¡¡¡S¡¡TR¡¡U¡¡CT¡¡U¡¡R¡¡E¡¡S£»¡¡¡¡¡¡DE¡¡CI¡¡SI¡¡ON¡¡S£»¡¡¡¡¡¡A¡¡N¡¡D¡¡¡¡¡¡L¡¡O¡¡OP¡¡S¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Figure¡¡4¡7¡£¡¡Planning¡¡flight¡¡routes¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡As¡¡shown¡¡in¡¡Figure¡¡4¡7£»¡¡three¡¡main¡¡attributes¡¡describe¡¡an¡¡individual¡¡node¡¡in¡¡the¡¡flight¡¡route£º¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡City¡¡name£º¡¡A¡¡description¡¡that¡¡will¡¡be¡¡used¡¡as¡¡a¡¡key¡¡when¡¡a¡¡user¡¡defines¡¡a¡¡starting¡¡and¡¡end¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡point¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡Coordinates£º¡¡An¡¡illustrative¡¡approach¡¡used¡¡to¡¡describe¡¡how¡¡cities¡¡are¡¡located¡¡in¡¡relation¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡to¡¡each¡¡other¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡Connections£º¡¡A¡¡representative¡¡connection¡¡between¡¡two¡¡cities¡£¡¡As¡¡in¡¡real¡¡life£»¡¡not¡¡all¡¡cities¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡connect¡¡to¡¡other¡¡cities¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡For¡¡the¡¡scope¡¡of¡¡this¡¡chapter£»¡¡there¡¡are¡¡only¡¡two¡¡projects£º¡¡a¡¡class¡¡library¡¡that¡¡contains¡¡the¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡depth¡first¡¡search¡¡algorithm¡¡and¡¡the¡¡testing¡¡application¡£¡¡The¡¡project¡¡structure¡¡looks¡¡like¡¡Figure¡¡4¡8¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡As¡¡with¡¡the¡¡examples¡¡in¡¡the¡¡previous¡¡chapters£»¡¡remember¡¡to¡¡add¡¡a¡¡reference¡¡to¡¡the¡¡class¡¡library¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡£¨SearchSolution£©¡¡and¡¡to¡¡set¡¡the¡¡test¡¡project¡¡£¨TestSearchSolution£©¡¡as¡¡the¡¡startup¡¡project¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Figure¡¡4¡8¡£¡¡Solution¡¡project¡¡structure¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡111¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡4¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡I¡¡N¡¡G¡¡¡¡¡¡A¡¡B¡¡OU¡¡T¡¡¡¡¡¡D¡¡AT¡¡A¡¡¡¡S¡¡TR¡¡U¡¡CT¡¡U¡¡R¡¡E¡¡S£»¡¡¡¡¡¡DE¡¡CI¡¡SI¡¡ON¡¡S£»¡¡¡¡¡¡A¡¡N¡¡D¡¡¡¡¡¡L¡¡O¡¡OP¡¡S¡¡89¡¡
Writing¡¡the¡¡Depth¡First¡¡Search¡¡Code¡¡
We¡¡will¡¡implement¡¡the¡¡depth¡first¡¡search¡¡algorithm¡¡in¡¡three¡¡main¡¡steps¡£¡¡The¡¡first¡¡step¡¡is¡¡to¡¡¡¡
define¡¡and¡¡implement¡¡the¡¡data¡¡structure¡£¡¡The¡¡second¡¡step¡¡is¡¡to¡¡implement¡¡the¡¡algorithm¡¡and¡¡¡¡
tests¡£¡¡Finally£»¡¡we¡¯ll¡¡run¡¡the¡¡algorithm¡¡and¡¡see¡¡what¡¡route¡¡has¡¡been¡¡found¡£¡¡
Defining¡¡and¡¡Implementing¡¡the¡¡Data¡¡Structure¡¡
As¡¡I¡¡mentioned¡¡earlier£»¡¡for¡¡the¡¡most¡¡part£»¡¡developers¡¡use¡¡the¡¡Class¡¡keyword¡¡to¡¡define¡¡a¡¡data¡¡¡¡
structure¡¡as¡¡a¡¡reference¡¡type£»¡¡because¡¡of¡¡the¡¡constraints¡¡of¡¡using¡¡a¡¡value¡¡type¡£¡¡However£»¡¡for¡¡this¡¡¡¡
example£»¡¡we¡¡will¡¡start¡¡out¡¡by¡¡using¡¡the¡¡Structure¡¡keyword¡¡to¡¡define¡¡Node¡¡as¡¡a¡¡value¡¡type¡£¡¡The¡¡¡¡
depth¡first¡¡search¡¡algorithm¡¡has¡¡two¡¡distinct¡¡implementation¡¡details£º¡¡data¡¡structure¡¡and¡¡algo
rithm¡£¡¡Because¡¡each¡¡detail¡¡is¡¡separate£»¡¡it¡¡seems¡¡appropriate¡¡to¡¡define¡¡Node¡¡as¡¡a¡¡value¡¡type¡£¡¡So£»¡¡¡¡
least¡¡let¡¯s¡¡try¡¡it¡¡and¡¡see¡¡what¡¡happens¡£¡¡
¡¡¡¡¡¡¡¡¡¡As¡¡per¡¡the¡¡attributes¡¡illustrated¡¡in¡¡Figure¡¡4¡7£»¡¡the¡¡data¡¡structure¡¡that¡¡is¡¡added¡¡to¡¡the¡¡¡¡
SearchSolution¡¡project¡¡is¡¡implemented¡¡as¡¡shown¡¡in¡¡Figure¡¡4¡9¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Name¡¡of¡¡the¡¡city¡¡
¡¡Public¡¡Structure¡¡Node¡¡
¡¡¡¡¡¡¡¡¡¡Public¡¡CityName¡¡As¡¡String¡¡
¡¡¡¡¡¡¡¡¡¡Public¡¡X¡¡As¡¡Double¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡X¡¡and¡¡Y¡¡coordinates¡¡
¡¡¡¡¡¡¡¡¡¡Public¡¡Y¡¡As¡¡Double¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡of¡¡the¡¡city¡¡
¡¡¡¡¡¡¡¡¡¡Public¡¡Connections¡¡As¡¡Node£¨£©¡¡
¡¡End¡¡Structure¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Array¡¡of¡¡cities¡¡that¡¡are¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡reachable¡¡from¡¡the¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡current¡¡city¡¡
Figure¡¡4¡9¡£¡¡¡¡The¡¡data¡¡structure¡¡for¡¡the¡¡depth¡first¡¡search¡¡
¡¡¡¡¡¡¡¡¡¡The¡¡data¡¡structure¡¡is¡¡declared¡¡as¡¡a¡¡Structure£»¡¡with¡¡the¡¡connections¡¡represented¡¡as¡¡an¡¡array¡¡¡¡
of¡¡Node¡¡elements¡£¡¡An¡¡array¡¡of¡¡¡¡Node¡¡elements¡¡is¡¡formed¡¡when¡¡one¡¡¡¡Node¡¡contains¡¡a¡¡list¡¡of¡¡refer
ences¡¡to¡¡other¡¡Node¡¡elements¡£¡¡Think¡¡of¡¡an¡¡array¡¡as¡¡a¡¡collection¡¡of¡¡sticky¡¡notes¡¡that¡¡say£»¡¡¡°Here¡¡is¡¡¡¡
a¡¡reference¡¡to¡¡A£»¡¡B£»¡¡C£»¡¡and¡¡so¡¡on¡£¡±¡¡By¡¡having¡¡one¡¡node¡¡reference¡¡another¡¡node£»¡¡a¡¡sort¡¡of¡¡never
ending¡¡tree¡¡is¡¡created£»¡¡because¡¡it¡¡is¡¡possible¡¡to¡¡travel¡¡back¡¡and¡¡forth¡¡between¡¡two¡¡cities¡£¡¡Thus£»¡¡¡¡
the¡¡depth¡first¡¡search¡¡algorithm¡¡will¡¡need¡¡to¡¡avoid¡¡repeating¡¡itself¡£¡¡
¡¡¡¡¡¡¡¡¡¡The¡¡Connections¡¡data¡¡member¡¡is¡¡an¡¡array¡¡used¡¡to¡¡define¡¡cities¡¡that¡¡are¡¡the¡¡next¡¡connection¡£¡¡¡¡
To¡¡reference¡¡another¡¡city£»¡¡you¡¡can¡¡create¡¡the¡¡reference¡¡as¡¡an¡¡array¡¡of¡¡Node¡¡elements£»¡¡as¡¡in¡¡the¡¡¡¡
declaration¡¡shown¡¡in¡¡Figure¡¡4¡9¡£¡¡An¡¡alternative¡¡is¡¡to¡¡use¡¡an¡¡array¡¡of¡¡strings¡¡that¡¡contain¡¡the¡¡¡¡
name¡¡of¡¡the¡¡next¡¡city£»¡¡like¡¡this£º¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡112¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
90¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡4¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡OU¡¡T¡¡¡¡¡¡D¡¡AT¡¡A¡¡¡¡S¡¡TR¡¡U¡¡CT¡¡U¡¡R¡¡E¡¡S£»¡¡¡¡¡¡DE¡¡CI¡¡SI¡¡ON¡¡S£»¡¡¡¡¡¡A¡¡N¡¡D¡¡¡¡¡¡L¡¡O¡¡OP¡¡S¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Structure¡¡Node¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡CityName¡¡As¡¡String¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡X¡¡As¡¡Double¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Y¡¡As¡¡Double¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Connections¡¡As¡¡String£¨£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Structure¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡In¡¡this¡¡declaration£»¡¡Connections¡¡is¡¡an¡¡array¡¡of¡¡strings¡¡that¡¡references¡¡other¡¡city¡¡names¡¡and¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡is¡¡what¡¡humans¡¡would¡¡see¡¡when¡¡they¡¡look¡¡at¡¡a¡¡table¡¡showing¡¡all¡¡of¡¡the¡¡connections¡¡for¡¡a¡¡partic
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ular¡¡city¡£¡¡The¡¡problem¡¡with¡¡using¡¡strings¡¡is¡¡that¡¡it¡¡is¡¡inefficient¡¡from¡¡a¡¡puting¡¡perspective¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡To¡¡traverse¡¡a¡¡tree¡¡of¡¡cities£»¡¡you¡¡would¡¡first¡¡traverse¡¡the¡¡city¡¡names£»¡¡resolve¡¡the¡¡city¡¡name¡¡to¡¡a¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Node¡¡object£»¡¡and¡¡then¡¡traverse¡¡the¡¡node¡£¡¡The¡¡string¡¡array¡¡approach¡¡requires¡¡an¡¡extra£»¡¡unneces
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡sary¡¡step¡£¡¡So¡¡the¡¡more¡¡efficient¡¡and¡¡programmatic¡¡approach¡¡is¡¡to¡¡have¡¡an¡¡array¡¡of¡¡Node¡¡instances¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡By¡¡using¡¡the¡¡declaration¡¡where¡¡Connections¡¡is¡¡an¡¡array¡¡of¡¡Node¡¡instances£»¡¡you¡¡have¡¡both¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡city¡¡name¡¡and¡¡available¡¡connections¡¡in¡¡one¡¡cohesive¡¡object¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Instantiating¡¡and¡¡Initializing¡¡a¡¡Node¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡In¡¡previous¡¡code£»¡¡you¡¡have¡¡seen¡¡how¡¡objects¡¡can¡¡be¡¡instantiated¡¡using¡¡the¡¡New¡¡keyword¡£¡¡To¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡instantiate¡¡a¡¡type£»¡¡you¡¡always¡¡use¡¡the¡¡New¡¡keyword¡£¡¡After¡¡the¡¡New¡¡keyword¡¡is¡¡the¡¡type¡¡that¡¡you¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡want¡¡to¡¡instantiate£»¡¡followed¡¡by¡¡a¡¡set¡¡of¡¡parentheses¡£¡¡A¡¡node¡¡is¡¡instantiated¡¡using¡¡the¡¡following¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡code£º¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡city¡¡As¡¡Node¡¡=¡¡New¡¡Node£¨£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡you¡¡look¡¡only¡¡at¡¡the¡¡identifier¡¡Node¡¡with¡¡parentheses£»¡¡you¡¡would¡¡get¡¡the¡¡impression¡¡that¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡you¡¡are¡¡calling¡¡a¡¡method¡¡that¡¡has¡¡no¡¡parameters¡£¡¡The¡¡impression¡¡is¡¡correct£»¡¡but¡¡it¡¡is¡¡a¡¡special¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡type¡¡of¡¡method¡¡call£»¡¡and¡¡that¡¡is¡¡made¡¡apparent¡¡by¡¡the¡¡use¡¡of¡¡the¡¡New¡¡keyword¡£¡¡The¡¡method¡¡that¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡is¡¡being¡¡called¡¡is¡¡known¡¡as¡¡a¡¡constructor¡£¡¡Every¡¡type¡¡has¡¡a¡¡constructor£»¡¡and¡¡it¡¡can¡¡be¡¡used¡¡to¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡initialize¡¡the¡¡state¡¡of¡¡the¡¡object¡¡before¡¡being¡¡returned¡¡to¡¡the¡¡caller¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡In¡¡the¡¡declaration¡¡of¡¡Node£»¡¡there¡¡is¡¡no¡¡defined¡¡constructor£»¡¡and¡¡thus¡¡a¡¡default¡¡constructor¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡is¡¡provided¡¡by¡¡the¡¡CLR¡£¡¡The¡¡default¡¡constructor¡¡does¡¡nothing¡¡and¡¡has¡¡no¡¡parameters¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡After¡¡having¡¡instantiated¡¡a¡¡node£»¡¡we¡¡can¡¡assign¡¡the¡¡data¡¡members£»¡¡as¡¡in¡¡the¡¡following¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡code¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡city¡£CityName¡¡=¡¡¡¨Montreal¡¨¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡city¡£X¡¡=¡¡0¡£0¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡city¡£Y¡¡=¡¡0¡£0¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Assigning¡¡the¡¡data¡¡members¡¡results¡¡in¡¡setting¡¡the¡¡city¡¡name¡¡to¡¡Montreal¡¡and¡¡the¡¡coordi
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡nates¡¡to¡¡£¨0£»0£©¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡This¡¡is¡¡all¡¡fine£»¡¡but¡¡shouldn¡¯t¡¡we¡¡need¡¡to¡¡provide¡¡some¡¡data¡¡members¡¡when¡¡creating¡¡a¡¡city¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡node£¿¡¡Does¡¡it¡¡make¡¡sense¡¡to¡¡instantiate¡¡a¡¡node¡¡without¡¡defining¡¡the¡¡name¡¡and¡¡coordinates¡¡of¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡city£¿¡¡Technically£»¡¡a¡¡node¡¡does¡¡not¡¡need¡¡to¡¡be¡¡assigned£»¡¡but¡¡logically¡¡speaking£»¡¡an¡¡unassigned¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡node¡¡is¡¡quite¡¡useless¡£¡¡And¡¡remember¡¡that¡¡we¡¡are¡¡working¡¡on¡¡defining¡¡an¡¡intelligent¡¡data¡¡struc
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ture£»¡¡thus¡¡a¡¡Node¡¡instance¡¡without¡¡city¡¡name¡¡and¡¡coordinates¡¡is¡¡logically¡¡not¡¡a¡¡valid¡¡¡¡Node¡£¡¡We¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡also¡¡need¡¡to¡¡have¡¡a¡¡root¡¡for¡¡the¡¡Node¡¡structure£»¡¡which¡¡we¡¡will¡¡set¡¡up¡¡as¡¡an¡¡array¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡
¿ì½Ý²Ù×÷: °´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·ҳ °´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ °´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿!
ÎÂÜ°Ìáʾ£º ο´Ð¡ËµµÄͬʱ·¢±íÆÀÂÛ£¬Ëµ³ö×Ô¼ºµÄ¿´·¨ºÍÆäËüС»ï°éÃÇ·ÖÏíÒ²²»´íŶ£¡·¢±íÊéÆÀ»¹¿ÉÒÔ»ñµÃ»ý·ÖºÍ¾Ñé½±Àø£¬ÈÏÕæдԴ´ÊéÆÀ ±»²ÉÄÉΪ¾«ÆÀ¿ÉÒÔ»ñµÃ´óÁ¿½ð±Ò¡¢»ý·ÖºÍ¾Ñé½±ÀøŶ£¡