findEdges()在第一次工作后返回空集?

时间:2016-07-16 21:11:32

标签: c++ oop graph

我正在创建一个实现findEdges()函数的图形类。我使用std :: list<>使用邻接列表创建了图类。出于某种原因,当我运行我的驱动程序时,它会从文件中读取并正确创建图形。但是当我尝试选择一个点并使用findEdges()时,它适用于第一个测试用例,但是在我之后读入的任何情况都会因某种原因返回一个空白集。

我将在下面发布一些代码:

class Graph
{
  public:
   // Non-Default Constructor                                                                                                                            
   inline Graph(int num) : cap(num), numItems(0) { adjList = new list<Vertex[num]; }

   // Copy constructor                                                                                                                                   
   inline Graph(const Graph & rhs) throw (const char *)    { *this = rhs; }

  // Destructor                                                                                                                                         
  inline ~Graph() {delete [] adjList;};

  // Assignment operator                                                                                                                                
  inline const Graph & operator = (const Graph & rhs) throw (const char *);

  inline bool isEdge(const Vertex v1, const Vertex v2) const;
  inline Set<Vertex> findEdges(CourseVertex theVertex);

  inline void add(Vertex v1, Vertex v2);
  inline void add(Vertex v1, Set<Vertex> groupV);

  inline int size() const { return cap; }

  inline void clear() {numItems=0;}

 private:                                                                                                                                             
  list<Vertex> * adjList;
  int cap;
  int numItems;
};

查找边缘功能:

Set<Vertex> Graph::findEdges(CourseVertex theVertex)
{
  Set<Vertex> edges;

  // Loop through adjList array                                                                                                                                                                                                                                     
  for(int i = 0; i < cap; i++)
  {
     // Find v1's adjacency list                                                                                                                                                                                                                                    
     if (*adjList[i].begin() == theVertex)
     {
        //cerr << adjList[i].getText() << endl;                                                                                                                                                                                                                     
        // Loop through adjacency list and find vertices                                                                                                                                                                                                            
        // containg and edge with theVertex                                                                                                                                                                                                                         
        for(list<Vertex>::const_iterator it = adjList[i].begin();
           it != adjList[i].end(); ++it)
           {
              ++it;
              edges.insert(*it);
           }
     }

     // if have not found v2, return false (or if v1 is not in the list)                                                                                                                                                                                            
     return edges;
  }
}

驱动程序功能:*

void testFindAll()
{

  try
  {
     Graph g1(28);
     CourseVertex vFrom;
     CourseVertex vTo;

     {
        Graph g2(g1.size());

        // read the class dependencies from a file                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
        // CS124 CS165 CIT225 ECEN160 |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
        ifstream fin("/home/cs235/week13/cs.txt");
        assert(fin.good());
        while (fin >> vFrom)  // read the first vertex, the class                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
        {
           cerr << "First Vertex:  " << vFrom.getText() << endl;
           while (fin >> vTo){ // keep reading until the "|" is encountered                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
              cerr << "Prerequisites: " << vTo.getText() << endl;
             g2.add(vFrom, vTo);
        }
           fin.clear();       // clear the error state which came from the "|"                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
           fin.ignore();
        }
        fin.close();
        g1 = g2;

        //TESTING CORRECT INPUT GRAPHS - DELETE                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
     /**********************************************************************/
        for (int i = 0; i < g2.size(); i++){
           list<Vertex>::const_iterator it;
           for( it = g2.adjList[i].begin(); it != g2.adjList[i].end(); ++it)
              cout << *it << " ";
           cout << endl;
     }

        for (int i = 0; i < g1.size(); i++){
           list<Vertex>::const_iterator it;
           for( it = g1.adjList[i].begin(); it != g1.adjList[i].end(); ++it)
              cout << *it << " ";
           cout << endl;
     }
     /**********************************************************************/
        g2.clear();
     }
     // g2 is destroyed                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
     // prompt for the next class                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
     cout << "For the given class, the prerequisites will be listed:\n";
     cout << "> ";
     while (cin >> vFrom)
     {
        Set <Vertex> s = g1.findEdges(vFrom);
        cerr << s << endl;
        for (SetConstIterator <Vertex> it = s.cbegin(); it != s.cend(); ++it)
        {
           cout << '\t' << (vTo = *it) << endl;
        }

        cout << "> ";
     }
  }
  catch (const char * error)
  {
     cout << error << endl;
  }
}

这是我的输出。大括号之间的输出是边缘之前有多少个点。

所以我读的文件应该是这样的:

CS165 CS124 |
CS235 CS165 |
CS237 CS165 |
CS238 CS237 |
CS246 CS235 |
CS306 CS235 CS237 |

这就是我的输出:

For the given class, the prerequisites will be listed:
> CS165
    { CS124 }
        CS124
> CS235
    { }

这是我的输出应该是这样的:

For the given class, the prerequisites will be listed:
> CS165
    { CS124 }
        CS124
> CS235
    { CS165 }
        CS165

由于某些cerr,我能够看到它正确地创建我的图形,具有正确的边缘和一切。出于某种原因,当我试图找到超出图形第一行的任何边缘时,我的findEdges()函数返回一个空集。任何帮助表示赞赏。

作业运营商:

const Graph & Graph::operator = (const Graph & rhs) throw (const char *)
{
  if (this != &rhs) // check that not self-assignment                                                                                                                               
  {
     //--Allocate a new array if necessary                                                                                                                                          
     if (cap != rhs.cap)
     {
        //delete [] adjList;                                                                                                                                                        
        cap = rhs.cap;
        adjList = new std::list<Vertex>[cap];

        if (adjList == 0)  // Check if memory available                                                                                                                             
        {
           std::cerr << "ERROR: Unable to allocate a new buffer for Vector\n";
        }
     }
     //--- Copy rightHandSide's list elements into this new array                                                                                                                   
     cap = rhs.cap;
     for (int i = 0; i < cap; i++)
     {
     //std::cerr<<adjList[i]<<std::endl;                                                                                                                                         
     adjList[i] = rhs.adjList[i];
     }
  }
  return *this;
}

0 个答案:

没有答案