友情提示:如果本网页打开太慢或显示不完整,请尝试鼠标右键“刷新”本网页!阅读过程发现任何错误请告诉我们,谢谢!! 报告错误
狗狗书籍 返回本书目录 我的书架 我的书签 TXT全本下载 进入书吧 加入书签

VB2008从入门到精通(PDF格式英文版)-第56章

按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!




pointless。 The AndAlso operator used in place of And says if a does not equal b; then stop processing  

and move to the next piece of code。 

     The real question is why make the difference between AndAlso and And? I can only postu

late; but in the early; early days of Visual Basic; programmers used to write code like this: 



If a = b And b = code。NextStatement() Then 



     The method NextStatement() not only verifies state; but also modifies state。 If a did not  

equal b; then the state was not modified。 And that was a problem; since some programmers  

relied on the fact that the other piece of code would be called regardless of how it was used。  

Think about this logically; and you’ll see it is not incorrect。 After all; what is the difference  

between the following two pieces of code? 


…………………………………………………………Page 132……………………………………………………………

110       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 



          If b = code。NextStatement() and a = b Then 



          and 



          If  b = code。NextStatement() Then 



               The answer is nothing; but if And behaved like AndAlso by default; then you would get an  

          inconsistent state。 



          Preventing Repetition in the Route 



          The  FindNextLeg() method calls the CanContinue() method; which is designed to halt the  

          search。 In the case of our depth…first search algorithm; the purpose is to not fly to the same city  

          twice。 Contained within the function is code similar to FindNextLeg(): 



          Private Function CanContinueSearch(ByVal returnArray As Node(); _ 

          ByVal city As Node) As Boolean 

            For c1 As Integer = 0 To  returnArray。Length 1 

              If returnArray(c1) IsNot Nothing Then 

                If returnArray(c1)。CityName。pareTo(city。CityName) = 0 Then 

                  Return False 

                End If 

              End If 

            Next 

            Return True 

          End Function 



               The logic is that CanContinueSearch() will iterate through the returnArray and see if the  

          city being considered (variable city) is already in the found path。 If the city is in the path; then  

          we stop searching that part of the tree; otherwise; we continue searching。 



          Running the Depth…First Search Algorithm 



          Everything has been implemented; including tests; so we are ready to run the test of finding the  

          flight between Montreal and Seattle。 Looking at Figure 4…2; you can see two paths: Montreal  

          to Los Angeles to Seattle; or Montreal to Toronto to Seattle。 However; running the algorithm  

          generates the following peculiar result (you have not seen how to display the results; but that is  

          done easily enough with a For loop that iterates over foundRoute): 



              o  Montreal 



              o  New York 



              o  Houston 



              o  Miami 



              o  Toronto 



              o  Seattle 


…………………………………………………………Page 133……………………………………………………………

                       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 111 



     Looking at the result; you are probably thinking that the algorithm does not work; because  

the proposed flight includes every city except Los Angeles。 If a travel agent were to propose  

such a flight route to you; you would probably have a panic attack。 

     The algorithm did not fail; rather; the CanContinueSearch() function did not include func

tionality to optimize the flight。 Right now; the algorithm says to perform a depth…first search;  

meaning to go down the tree before backtracking。 So let’s go through the structure in the Node  

shared constructor again。 

     We started our route in Montreal; which had the following  Connections definitions: 



montreal。Connections = New Node() { newyork; toronto; losangeles } 



     Applying our depth…first algorithm; it means the first array element of the tree is consid

ered a connection; and thus our route takes us to New York。 New York has the following flight  

connections: 



newyork。Connections = New Node() { montreal; houston; miami } 



     The first connection from New York is Montreal; which is already in the flight route。 Thus;  

the second array element is searched; which is Houston。 Houston has the following flight  

connections: 



houston。Connections = New Node() { miami; seattle; newyork } 



     Following the flight route from Houston; we travel to Miami; which has the following  

connections: 



miami。Connections = New Node() { toronto; houston; newyork } 



     Following the flight route from Miami; we travel to Toronto; which has the following  

connections: 



toronto。Connections = New Node() { miami; seattle; montreal } 



     At Toronto; the first connection is Miami; where we have already been。 The second  

connection is Seattle; and that is our end destination。 

     So from the perspective of the algorithm; everything worked。 From the perspective of the  

traveler; it’s not ideal。 This demonstrates yet again how important it is to write test routines; as  

algorithms might be correct; but they will generate responses that you might not have antici

pated。 Improving the example is one of the exercises at the end of the chapter。 



The Important Stuff to Remember 



In this chapter; you learned about data structures and algorithms。 Here are the key points to  

keep in mind: 



     o  When developing a program; you need to think of the data structures and algorithms  

        that are involved。 



     o  A single best data structure and a single best algorithm do not exist。 Every data structure  

        and algorithm has promises。 You need to choose the data structure and algorithm  

        that best s
返回目录 上一页 下一页 回到顶部 0 0
未阅读完?加入书签已便下次继续阅读!
温馨提示: 温看小说的同时发表评论,说出自己的看法和其它小伙伴们分享也不错哦!发表书评还可以获得积分和经验奖励,认真写原创书评 被采纳为精评可以获得大量金币、积分和经验奖励哦!