Blame SOURCES/dfs.cc

345a7b
/*	--*- c++ -*--
345a7b
 * Copyright (C) 2010 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
345a7b
 *
345a7b
 * This program is free software; you can redistribute it and/or modify
345a7b
 * it under the terms of the GNU General Public License as published by
345a7b
 * the Free Software Foundation; version 3 of the License.
345a7b
 *
345a7b
 * This program is distributed in the hope that it will be useful,
345a7b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
345a7b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
345a7b
 * GNU General Public License for more details.
345a7b
 *
345a7b
 * You should have received a copy of the GNU General Public License
345a7b
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
345a7b
 */
345a7b
345a7b
#ifdef HAVE_CONFIG_H
345a7b
#  include <config.h>
345a7b
#endif
345a7b
345a7b
#include <string>
345a7b
#include <list>
345a7b
#include <map>
345a7b
#include <iostream>
345a7b
345a7b
class node {
345a7b
    public:
345a7b
	node(std::string const &name) : name_(name) {}
345a7b
	void	add_child(class node *n)
345a7b
	{
345a7b
		children_.push_back(n);
345a7b
	}
345a7b
345a7b
	void	dfs(std::list<std::string> &res;;
345a7b
	bool				visited;
345a7b
345a7b
    private:
345a7b
	std::string const		name_;
345a7b
	std::list<class node *>		children_;
345a7b
};
345a7b
345a7b
void node::dfs(std::list<std::string> &res)
345a7b
{
345a7b
	std::list<class node *>::iterator	i;
345a7b
345a7b
	visited = true;
345a7b
345a7b
	for (i = children_.begin(); i != children_.end(); ++i) {
345a7b
		if ((*i)->visited)
345a7b
			continue;
345a7b
345a7b
		(*i)->dfs(res);
345a7b
	}
345a7b
345a7b
	res.push_front(name_);
345a7b
}
345a7b
345a7b
int main(void)
345a7b
{
345a7b
	std::map<std::string, class node *>		nodes;
345a7b
345a7b
	for (;;) {
345a7b
		std::string		name;
345a7b
		class node		*cur_node;
345a7b
345a7b
		std::cin >> name;
345a7b
		if (!std::cin.good())
345a7b
			break;
345a7b
345a7b
		if (nodes.find(name) == nodes.end())
345a7b
			nodes[name] = new node(name);
345a7b
345a7b
		cur_node = nodes[name];
345a7b
345a7b
		while (std::cin.good()) {
345a7b
			std::cin >> name;
345a7b
345a7b
			if (name == "##")
345a7b
				break;
345a7b
345a7b
			if (nodes.find(name) == nodes.end())
345a7b
				nodes[name] = new node(name);
345a7b
345a7b
			cur_node->add_child(nodes[name]);
345a7b
		}
345a7b
	}
345a7b
345a7b
	typedef std::map<std::string, class node *>::iterator	node_iterator;
345a7b
345a7b
	for (node_iterator n = nodes.begin(); n != nodes.end(); ++n) {
345a7b
		for (node_iterator m = nodes.begin();
345a7b
		     m != nodes.end(); ++m)
345a7b
			m->second->visited = false;
345a7b
345a7b
		std::list<std::string>		res(nodes.size());
345a7b
		n->second->dfs(res);
345a7b
345a7b
		for (std::list<std::string>::const_iterator i = res.begin();
345a7b
		     i != res.end(); ++i)
345a7b
			std::cout << *i << " ";
345a7b
345a7b
		std::cout << std::endl;
345a7b
	}
345a7b
}