[ create a new paste ] login | about

Link: http://codepad.org/Ihdr5woD    [ raw code | output | fork ]

C++, pasted on Aug 29:
#include <string>
#include <list>
#include <queue>
#include <vector> 
#include <string>

using namespace std;

class directedGraph
{
private:
	class vertex
	{
	public:
		string data;
		list<vertex*> neighbors;
		bool visited; 
		vertex* predecesor; 
		float dist; 
		vertex*path; 

		vertex(string x)
		{
			data = x;
		}
		
	};

	list<vertex*> vertexList;

	//locate vertex containg value x
	vertex * findVertex(string x)
	{
		for each(vertex * v in vertexList)
		{
			if (v->data == x)
				return v;
		}
		return NULL;
	}

public:

	directedGraph()
	{
	}

	void addVertex(string x)
	{
		vertexList.push_back(new vertex(x));
	}

	//add directed edge going from x to y
	void addDirectedEdge(string x, string y)
	{
		vertex * xVert = findVertex(x);
		vertex * yVert = findVertex(y);

		xVert->neighbors.push_back(yVert); //I would think that this should only add y to x's neighbors, but when I try to display I  get x as one of y's neighbors
	}

	void addEdge(string x, string y)
	{
		addDirectedEdge(x, y);
		addDirectedEdge(y, x);
	}

	//display all vertices and who they connect to
	void testDisplay()                                     //Could the problem be in the display function? 
	{
		for each(vertex * v in vertexList)
		{
			cout << v->data << ": ";
			for each(vertex * u in v->neighbors)
			{
				cout << u->data << ", ";
			}
			cout << endl;
		}
	}

	}

};

int main()
{
	directedGraph graph;

	graph.addVertex("a");
	graph.addVertex("b");
	graph.addVertex("c");
	graph.addVertex("d");
	graph.addVertex("e");
	graph.addVertex("f");

	graph.addEdge("a", "b");
	graph.addEdge("a", "c");
	graph.addEdge("b", "d");
	graph.addEdge("c", "d");
	graph.addEdge("c", "e");
	graph.addEdge("e", "d");
	graph.addEdge("c", "e");
	graph.addEdge("e", "f");


	graph.testDisplay();
	

	return 0;
}


Output:
1
2
3
t.cpp: In member function 'directedGraph::vertex* directedGraph::findVertex(std::string)':
Line 34: error: expected `(' before 'each'
compilation terminated due to -Wfatal-errors.


Create a new paste based on this one


Comments: