图的邻接矩阵实现

先写这么多,明天给例子和邻接表实现
 
#include <iostream>
#include <vector>
#include <list>

using namespace std;
const int MaxSize = 12;
template <class T>
class Graph
{
private:
	list<T> vertexList;
	int edge[MaxSize][MaxSize];
	int numofVertex,
		numofArcs;
	int findVertex(list<T> &l, const T& vertex);
	int getVertexPos(const T& vertex);
public:
	Graph(void);
	int numberOfVertices(void) const;
	int getWeight(const T& vertex1 , const T& vertex2);
	list<T> getVertex(int v);
	list<T>& getNeighbors(const T& vertex);

	void insertVertex(const T& vertex);
	void insertEdge(const T& vertex1, const T& vertex2, int weight);
	void deleteVertex(const T& vertex);
	void deleteEdge(const T& vertex1, const T& vertex2);
	
	list<T>& BFS(const T& vertex);
	void DFS();
	void DFS(const int v, int visited[]);
};
template <class T>
int Graph<T>::numberOfVertices(void) const
{
	return numofVertex;
}
template <class T>
int Graph<T>::getWeight(const T& vertex1, const T&vertex2)
{
	int pos1 = getVertexPos(vertex1),
		pos2 = getVertexPos(vertex2);
	if(pos1 == -1 || pos2 == -1)
	{
		cerr << "Error!" << endl;
		return -1;
	}
	return edge[pos1][pos2];
}
template <class T>
list<T>& getNeighbors(const T& vertex);
{
	;
}
template <class T>
void Graph<T>::insertEdge(const T& vertex1, const T& vertex2, int weight)
{
	int pos1 = getVertexPos(vertex1),
		pos2 = getVertexPos(vertex2);
	if(pos1 == -1 || pos2 == -1)
	{
		cerr << "Error!" << endl;
		return -1;
	}
	edge[pos1][pos2] = weight;
}
template <class T>
void Graph<T>::insertVertex(const T& vertex)
{
	if(numofVertex + 1 > MaxSize)
	{
		cerr << "Error2!" << endl;
		exit(1);
	}
	vertexList.insert(vertex);
	numofVertex++;
}
template <class T>
void Graph<T>::deleteVertex(const T& vertex)
{
	int pos = getVertexPos(vertex);
	int row,
		col;
	if(-1 == pos)
	{
		cerr << "Error3!" << endl;
		return;
	}
	list<T>::iterator iter = vertexList.begin();
	while(iter != vertexList.end() && *iter != vertex);
	vertexList.erase(vertex);
	numofVertex--;
	for(row =0; row < pos; row++)
	{
		for(col = pos+1; col < numofVertex; col++)
		{
			dege[row][col-1] = edge[row][col];
		}
	}
	for(row =pos+1; row < numofVertex; row++)
	{
		for(col = pos+1; col < numofVertex; col++)
		{
			dege[row-1][col-1] = edge[row][col];
		}
	}
	for(row =pos+1; row < numofVertex; row++)
	{
		for(col = 0; col < pos; col++)
		{
			dege[row-1][col] = edge[row][col];
		}
	}	
}
template <class T>
void Graph<T>::deleteEdge(const T& vertex1, const T& vertex2)
{
	insertEdge(vertex1, vertex2, 0);
}
int main(void)
{		
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值