Implementación de la estructura Grafo en C++

Graf.h

class CGraf {

private:
	int NumNodes;
	int *array;
		
public:
	inline CGraf() { array=NULL; }
	inline ~CGraf() { delete []array; }
	CGraf(int dim);
	CGraf(const CGraf &copia); //Constructor de copia
	CGraf& operator=(CGraf &copia); //Sobrecarrega operador =
	int &operator()(int i, int j); //Sobrecarrega operador ()
	friend istream& operator>>(istream& is, CGraf &copia); //Sobrecarrega operador >>
	inline int GetNumNodes() { return NumNodes; } //Retorna num de nodes
 	CNodeList GetNeighbourgs(int Index);

};


Graf.cpp

#include "Graf.h"

CGraf::CGraf(int dim) {

	NumNodes=dim;
	array=new int[NumNodes*NumNodes];
	for(int i=0; i<(NumNodes*NumNodes); i++) {
		array[i]=0;
	}
}

CGraf::CGraf(const CGraf &copia) {
	this->NumNodes=copia.NumNodes;
	this->array=new int[NumNodes*NumNodes];
	
	for(int i=0; i<(NumNodes*NumNodes); i++) {
		this->array[i]=copia.array[i];
	}
}

CGraf& CGraf::operator=(CGraf &copia) {
    if(this!=&copia) {
	delete []array;

	NumNodes=copia.NumNodes;
	array=new int[NumNodes*NumNodes];
	for(int i=0; i<(NumNodes*NumNodes); i++) {
		array[i]=copia.array[i];
	}
	}
	return *this;
}

istream& operator>>(istream &is, CGraf &copia) {
        delete []copia.array;
	is >> copia.NumNodes;
	copia.array=new int[copia.NumNodes*copia.NumNodes];
	for(int i=0; i<(copia.NumNodes*copia.NumNodes); i++) {
		is >> copia.array[i];
	}
	return is;
}

int& CGraf::operator()(int i, int j) {
	return (array[NumNodes*i+j]);
}

CNodeList CGraf::GetNeighbourgs(int Index) {
	CNodeList llista;

	for(int i=0; i<NumNodes; i++) {
		if(this->operator()(i,Index)>0) {
			//recorrem la columna Index
			llista.PushBack(i);
		}
	}
	return llista;
}



 


Trackback

Trackback URL for this entry:
http://www.gra2.com/trackback.php/20040620203410384

No trackback comments for this entry.

Comments

Post a comment

Search



About

newton.gra2.com is a blog about technology, opinion and random thoughts written by Daniel Alvarez, a computer engineer currently living in Zurich, Switzerland.

Topics

News (20/0)
Manuals (24/0)
Security (7/0)
Music (3/0)
Weeklog (1/0)
Personal (34/0)
Photos (3/0)
Opinion (14/0)
Windows (5/0)

Blogroll

Pros i contres (Jordi)
Entrepa de fusta (Oriol)
Spaghetti Code (Isaac)
Made in net (Eric)
Nogare (Juan)
Blog de Isaac Jimenez
Web d'en Jaume Benet
Montcada Wireless (Fran)
Blog d'en Ricard Forniol
Angela Fabregues
in.solit.us

Libertad Digital
FOX News
The Wall Street Journal
The Washington Times
The Jerusalem Post

Michelle Malkin
Eurabian News
Nihil Obstat
Barcepundit
Expose the left
Davids Medienkritik
Johan Norberg
Ayaan Hirsi Ali

User Functions

:

:


Lost your password?

Latest posts

Stories

No new stories

Comments last 2 days


Trackbacks last 2 days

No new trackback comments

Links last 2 weeks

No recent new links