图的邻接矩阵实现----修正版

本文补充了上次邻接矩阵实现中缺失的print()函数,经过测试已正常工作。后续将结合邻接表和多重表进行整合介绍。
摘要由CSDN通过智能技术生成

上次的那个邻接矩阵的实现点击打开链接没有给出print()函数等,这次都加上了,测试了,可以了。草草结束,写完邻接表和多重表之后再整合一下。:)

#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);
	T getVertex(int v);
	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);
	void print() const;
	
//	list<T>& BFS(const T& vertex);
//	void DFS();
//	void DFS(const int v, int visited[]);
};
template <class T>
void Graph<T>::print() const
{
	list<T>::const_iterator iter = vertexList.begin();//这里特别注意,之前写过,const函数,迭代器也要用const_iterator
	for(;iter != vertexList.end(); iter++)
	{
		cout << *iter << "   ";
	}
	cout << endl;
	for(int i =0;i<MaxSize; ++i)
	{
		for(int j = 0; j< MaxSize; ++j)
			cout << edge[i][j] << "   ";
		cout << endl;
	}
}
template <class T>
Graph<T>::Graph(void)
{
	for(int i =0;i<MaxSize; ++i)
	{
		for(int j = 0; j< MaxSize; ++j)
			edge[i][j] = 0;
	}

	numofVertex = 0;
	numofArcs   = 0;
}
template <class T>
int Graph<T>::getVertexPos(const T& vertex)
{
	list<T>::iterator iter = vertexList.begin();
	int count = 1;
	while(iter != vertexList.end() && *iter++ != vertex)
		count++;
	return count;
}
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>
T& Graph<T>::getNeighbors(const T& vertex)
{
	list<T> temp;
	cout << endl;
	return temp;
}
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;
	}
	edge[pos1][pos2] = weight;
}
template <class T>
void Graph<T>::insertVertex(const T& vertex)
{
	if(numofVertex + 1 > MaxSize)
	{
		cerr << "Error2!" << endl;
		exit(1);
	}
	vertexList.push_back(vertex);
	numofVertex++;
}
template <class T>
T Graph<T>::getVertex(int v)
{
	list<T> temp;

	cout << endl;
	return temp;
}
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)
{	
	Graph<int> g;
	g.insertVertex(0);
	g.insertVertex(1);
	g.insertVertex(2);

	g.insertEdge(0,1,2);
	g.insertEdge(1,2,3);
	g.insertEdge(0,2,1);
	g.print();

	return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值