Esempio n. 1
0
int main()
{
	Stack2 s;
	s.push(1);
	s.push(2);
	s.push(3);
	while(!s.empty()){
		cout << s.top() << endl;
		s.pop();
	}
}
Esempio n. 2
0
void DFS_M::dfs(int iRoot)
{
  Stack2< Pair<int,int> > fringe;
  //Queue< Pair<int,int> > fringe;

  fringe.push(Pair<int,int>(-1,iRoot));
  int precount = 0; // pre-order counter
  int poscount = 0; // post-order counter
  int inocount = 0; // in-order counter
  int P = -1;
  int forest=0;
  int cyclecount = 0; // number of cycles found so far
  
  while (precount < _graph.getVertexSize())
  { 
    while ( !fringe.empty() )
    {
      Pair<int,int> pair = fringe.getTop(); fringe.pop();
      int v0 = pair.first();
      int v1 = pair.second();
      
      if (v0==-2) {
        _postorder[v1]=poscount;
        ++poscount;
        continue;
      }

      if ( !_visited[v1] )
      {
        // first time we see this vertex, push a dummy edge on the fringe
        // before pushing all its children. When the dummy edge will be popped
        // that means all the subtrees rooted under this vertex v1, will have been
        // explored completely, and we can assign the postorder index to the vertex
        fringe.push(Pair<int,int>(-2,v1));
        _preorder[v1]=precount; ++precount;
        _visited[v1]=1;
        _parent[v1]=v0;
        _forest[v1]=forest;
  
        if ( v0 > -1 )
        {
          _matrix[v0][v1]='T';
        }
        //std::cout << "(" << v1 << ")" << std::endl;
    
        for ( GraphEdgeIter it = _graph.getEdgeIter(v1); !it.end(); ++it )
        {
          int v2 = *it;
          
          if (!_visited[v2]) {
            fringe.push(Pair<int,int>(v1,v2));
          } else { // already visited
            char edgeType = tagEdge(v1,v2);
            cycleCheck(edgeType, v1, v2, cyclecount);
          }
        }
      } else { // already visited
        char edgeType = tagEdge(v0,v1);
        cycleCheck(edgeType, v0, v1, cyclecount);
      }

    }

    if (fringe.empty() && precount<_graph.getVertexSize())
    {
      for (int i=0; i<_graph.getVertexSize(); ++i)
      {
          if (!_visited[i])
          {
            fringe.push(Pair<int,int>(-1,i));
            break;
          }
      }
      ++forest;
    }
  }
}