text cover

Data Abstraction and Problem Solving with C++

Walls and Mirrors

by Frank M. Carrano

Addison Wesley Logo

isPathStack.cpp

Go to the documentation of this file.
00001 
00027 bool Map::isPath(int originCity, int destinationCity)
00028 {
00029    Stack aStack;
00030    int   topCity, nextCity;
00031    bool  success;
00032 
00033    unvisitAll();  // clear marks on all cities
00034 
00035    // push origin city onto aStack, mark it visited
00036    try
00037    {  aStack.push(originCity);
00038       markVisited(originCity);
00039 
00040       aStack.getTop(topCity);
00041       while (!aStack.isEmpty() && (topCity != destinationCity))
00042       {  // Loop invariant: The stack contains a directed path
00043     // from the origin city at the bottom of the stack to
00044     // the city at the top of the stack
00045 
00046     // find an unvisited city adjacent to the city on the
00047     // top of the stack
00048     success = getNextCity(topCity, nextCity);
00049 
00050     if (!success)
00051        aStack.pop();  // no city found; backtrack
00052 
00053     else                // visit city
00054     {  aStack.push(nextCity);
00055             markVisited(nextCity);
00056     }  // end if
00057 
00058     if(!aStack.isEmpty())
00059        aStack.getTop(topCity);
00060       }  // end while
00061 
00062       if (aStack.isEmpty())
00063     return false;  // no path exists
00064       else
00065     return true;   // path exists
00066    }
00067    catch (StackException e)
00068    {  cout << e.what() << endl;
00069    }  // end try
00070 }  // end isPath

Generated on Sun Aug 27 16:41:03 2006 for AWLogo by  doxygen 1.4.6