View Single Post
Old 04-10-2005, 03:10 PM   #3 (permalink)
MealMan401
Registered User
 
MealMan401's Avatar
 
Join Date: Apr 2004
Posts: 10
MealMan401 is on a distinguished road
OK i am reposting the code because I changed the layout a bit.

///////////structs.h/////////////

Code:
#ifndef STRUCTS_H
#define STRUCTS_H

struct EdgeData
{
	int weight;
	struct Node* dest; 
};
struct Edge
{
	EdgeData data;
	Edge* nextedge;
};

struct NodeData
{
	int nodenumber;
};

struct Node
{
	NodeData data;//container holding node data
	
	Edge* edges;//list of directed edges connecting vertex (node) to other vertices
};


struct Item
{
	struct Node data;
	Item* prev;
	Item* next;
};

#endif



////////myGraph.h////////////

Code:
#ifndef MYGRAPH_H
#define MYGRAPH_H

#include <ostream>
#include <iostream>
#include <fstream>
#include "structs.h"


using namespace std;



class Mygraph
{
	private:
	Node* vlist;
	int size;
	
	
	public:
	Mygraph();
	~Mygraph();
	
	int getsize();
	Node getnode(int index);
	void printlist(ostream& out);
	void deletelist();
	bool inorderpath(int source, int dest, bool *visited ,ostream& out);
	//breadthprint(ostream& out);
	void file_createlist(const char* filename);
	void string_createedges(int v, string* str_edges);
	
	
};

#endif


////Mygraph.cpp////////////

Code:
#include "Mygraph.h"


Mygraph::Mygraph()
{
vlist = 0;
size = 0;
}

Mygraph::~Mygraph()
{
	deletelist();//BIG PROBLEM!!!!!!!!!!!!
}

void Mygraph::printlist(ostream& out)
{
	Edge* edge = 0;
	out<<"Adjacency List has "<<size<<" vertices"<<endl;
	for(int i = 0; i < size; i++)
	{
		out<<vlist[i].data.nodenumber<<"\t";
		edge = vlist[i].edges;
		
		while(edge)
		{
			out<<edge->data.dest->data.nodenumber<<" ";
			edge = edge->nextedge;
		}
		out<<endl;
	
	
	}
	

}

int Mygraph::getsize()
{
	return size;
}

void Mygraph::deletelist()
{
	Edge *nextedge;
	Edge *doomed;
	for(int i = 0; i < size; i++)
	{	
		doomed = vlist[i].edges;
		while(doomed)
		{	cout<<"deleting..."<<endl;
			nextedge = doomed->nextedge;
			delete doomed;
			doomed = nextedge;
		}
	}
	
	delete [] vlist;
}
bool Mygraph::inorderpath(int source, int dest, bool *visited ,ostream& out)
{
	Edge* edge = 0;
	bool found = false;
	//check to see if this node has been visited
	if( !visited[source] )
	{	
		//check to see if the this node is the destination node
		if(vlist[source].data.nodenumber == dest)
		{
			found = true;
			cout<<"Cool..we found a path to "<<dest<<endl;
			//stack.push(source);
		}
		//mark it as visited
		visited[source] = true;
		
		//go through all nodes connected to this one
		edge = vlist[source].edges;
		
		while(edge && !found)
		{
			found = inorderpath(edge->data.dest->data.nodenumber,dest, visited,out);
			if(found)//last node visited was the destination node
			{
				//stack.push(source);
			cout<<"Currently visiting "<<vlist[source].data.nodenumber<<" on path back from  "<<dest<<endl;
			}
			edge = edge->nextedge;
		}	
	}
	return found;
}
//bool Mygraph::breadthpath(int source, int dest, bool* visited,ostream& out)
//{

//}
void Mygraph::file_createlist(const char* filename)
{
	string stringbuf;
	fstream file;
	int vindex = 0;
		
	file.open(filename,ios::in);
	if(file.fail())
	{
		vlist = 0;
		return;
	}
	
	
	//matrix is n by n so find number
	//of vertices to make array.
	getline(file,stringbuf);
	size = stringbuf.length() - 31;
	vlist = new Node[size];
	string_createedges(vindex,&stringbuf);
	
	
	
	for(vindex = 1; !file.eof(); vindex++)
	{
		getline(file,stringbuf);
		if(file.eof())
			break;
		vlist[vindex].data.nodenumber = vindex;
		string_createedges(vindex,&stringbuf);
		
	}
	
	file.close();
	return;
}
void Mygraph::string_createedges(int v, string* str_edges)
{
	Edge** stitch = &(vlist[v].edges);
	*stitch = 0;
	
	int strlength = str_edges->length();
	
	for(int i = 0; i < strlength; i+=2)
	{
		if( (*str_edges)[i] == '1')
		{
			*stitch = new Edge;
			(**stitch).data.dest = &(vlist[i/2]);
			stitch = &((**stitch).nextedge);
			*stitch = 0;//tie up end
		}
	}
}

Node Mygraph::getnode(int index)
{
	
	return vlist[index];


}

/////////main.cpp/////////////

Code:
#ifndef MAIN_CPP
#define MAIN_CPP

#include <iostream>
#include <string>
#include <fstream>
#include "structs.h"
#include "Mygraph.h"

using namespace std;


int main(int argc, char **argv)
{

	Mygraph graph;
	
	graph.file_createlist("input_graph.txt");
	graph.printlist(cout);
	
	
	
return 0;
}

#endif
///////////input_graph.txt/////////// is 31*31 wide with entries seperated with tabs.
Code:
0	1	0	0	0	0	1	1	0	0	0	0	0	0	0	1	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	
0	0	1	1	0	0	0	1	1	0	0	0	0	0	0	1	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0
0	0	0	1	1	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0
0	0	0	0	1	0	0	1	0	0	0	0	0	0	1	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0
1	0	0	0	0	1	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0
0	1	0	0	0	0	1	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0
1	0	0	0	1	1	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0
0	0	0	0	1	1	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	1	0	0
1	0	0	0	0	0	0	0	0	0	0	1	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0
0	0	1	0	0	0	0	0	0	0	0	0	0	0	1	0	0	0	0	0	0	0	0	0	0	0	0	0	1	0	0
0	0	0	0	0	0	0	0	0	1	0	1	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0
0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0
0	0	0	0	0	0	0	0	0	0	0	0	0	0	1	0	0	0	1	0	0	0	0	0	0	0	0	0	0	0	0
0	0	0	0	0	0	0	0	0	0	0	0	0	0	1	1	0	0	1	0	0	0	0	0	0	0	0	0	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