Topological sort using DFS without recursion
I know the common way to do a topological sort is using DFS with recursion. But how would you do it using stack<int> instead of recursion? I need to obtain the reversed post-order but I'm kinda stuck: The graph is a vector<vector<int> > adjacency list The following is the DFS which I want to use for topological sort bool visited[MAX]={0}; stack<int> dfs, postOrder; vector<int> newVec; vector<int>::iterator it; for(int i=0;i<MAX;i++){ if(visited[i]==false){ dfs.push(i); } while(!dfs.empty()){ int node=dfs.top(); dfs.pop(); visited[node]=true; newVec=graph[node]; //vector of neighboors for(it