| | |
A little graph help please marking a visited node
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Sep 2008
Posts: 31
Reputation:
Solved Threads: 0
The function that I am having a problem with is in the middle of this seperated from the rest of the code.
The problem function
How do I mark the node as visited?
c++ Syntax (Toggle Plain Text)
#ifndef GRAPH_H #define GRAPH_H #include <string> #include "queue.h" using namespace std; class FileOpenError // Exception class -- cannot open input file { }; struct EdgeNode // Structure representing an edge { int destination; // Index of destination vertex int weight; // Edge weight EdgeNode* nextPtr; // Next edge pointer }; struct VertexNode // Structure representing a vertex { string vname; // Name of vertex bool mark; // Marked flag EdgeNode* edgePtr; // Pointer to list of edges }; class Graph // Graph ADT using adjacency list representation { private: //***** Private class members below *****// VertexNode* vertices; // Array of vertex nodes int numV; // Number of vertices public: //***** Public members below *****// Graph(int num); // Constructor - creates graph with num vertices ~Graph(); // Destructor - deallocates all edge nodes and the vertex list void DeleteAllEdges(); // Deallocates all edge nodes from all vertices bool IsEmpty(); // Returns true if graph empty, false otherwise bool IsFull(); // Returns true if graph full, false otherwise void AddVertex(string v); // Adds vertex to graph assuming vertex not already present void AddEdge(string s, string d, int w); // Adds edge from source S to destination D with specified weight W int IndexIs(string v); // Returns index of edge from S to D; -1 if not present int WeightIs(string s, string d); // Returns weight of vertex V -- assumes V is in graph void GetToVertices(string s, Queue& q); // Returns a queue Q of vertices adjacent to vertex V void ClearMarks(); // Clears vertex marks void MarkVertex(string v); // Marks vertex V bool IsMarked(string v); // Returns true if vertex V is marked, false otherwise void Print(); // Write graph to stdout Queue* DepthFirstSearch(string startVertex, string endVertex); // Returns ptr to queue containing path or NULL if none }; void Load(string filename, Graph*& g, string& startVertex, string& endVertex); // Load graph from named file and values of start and end vertices // Note: this file attempts to open the named file for input and input all information. // If the file fails to open, Load throws the FileOpenError exception #endif using namespace std; Graph::Graph(int num):vertices(NULL),numV(num) { if (num) { vertices = new VertexNode[num]; } } Graph::~Graph() { delete [] vertices; } void Graph::DeleteAllEdges() { VertexNode* temp = NULL; } bool Graph::IsEmpty() { return(numV == 0); } bool Graph::IsFull() { } void Graph::AddVertex(string v) { for(int numNodes = 0; numNodes <= numV; numNodes++) { if (vertices[numNodes].vname.empty()) { vertices[numNodes].vname = v; vertices[numNodes].edgePtr = NULL; vertices[numNodes].mark = false; break; } //Graph::AddVertex(string v); } } void Graph::AddEdge(string s, string d, int w) { int sV = IndexIs(s); int dV = IndexIs(d); EdgeNode *edge = vertices[sV].edgePtr; vertices[sV].edgePtr = new EdgeNode; vertices[sV].edgePtr->nextPtr = edge; vertices[sV].edgePtr->weight = w; vertices[sV].edgePtr->destination = dV; } int Graph::IndexIs(string v) { for(int numNodes = 0; numNodes <= numV; numNodes++) { if (vertices[numNodes].vname == v) { return numNodes; } } return -1; } int Graph::WeightIs(string s, string d) { int sV = IndexIs(s); int dV = IndexIs(d); EdgeNode *edge = vertices[sV].edgePtr; while (edge) { if (edge->destination == dV) { return edge->weight; } edge = edge->nextPtr; } return -1; } void Graph::GetToVertices(string s, Queue& q) { int sV = IndexIs(s); EdgeNode *edge = vertices[sV].edgePtr; while (edge) { q.Enqueue(s); edge = edge->nextPtr; } } void Graph::ClearMarks() { }
The problem function
How do I mark the node as visited?
c++ Syntax (Toggle Plain Text)
void Graph::MarkVertex(string v) { bool marked = v; }
c++ Syntax (Toggle Plain Text)
bool Graph::IsMarked(string v) { } void Graph::Print() { for (int node = 0; node <+ numV; node++){ cout << vertices[node].vname; if (!vertices[node].edgePtr){ cout << endl; continue; } EdgeNode *edge = vertices[node].edgePtr; while (edge) { cout << vertices[edge->destination].vname << edge->weight; edge = edge->nextPtr; } cout << endl; } }
•
•
Join Date: Jul 2005
Posts: 1,688
Reputation:
Solved Threads: 266
C++ Syntax (Toggle Plain Text)
void Graph::MarkVertex(string v) { if(v == vname) marked = true; }
Last edited by Lerner; Nov 15th, 2008 at 12:18 pm.
Klatu Barada Nikto
![]() |
Other Threads in the C++ Forum
- Previous Thread: Problem with fread and malloc
- Next Thread: problem with class static member.
| Thread Tools | Search this Thread |
api array arrays based binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






