Blame SOURCES/dfs.cc

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