C++图:极简版的图类(邻接链表)

本文介绍了一个基于邻接链表实现的极简版图类,包括边和节点的定义,以及添加边和显示图的方法。通过示例展示了如何创建图并添加无向边。

建立一个(极简版的)图类(邻接链表)。

该图类是通过一个极简版的单向链表类  修改得到的。

代码如下:


图类头文件

//graph.h
//边
struct Edge
{
	int startVertex;
	int endVertex;
	int weightEdge;//边的权重
	Edge * nextEdge;
	Edge(int s, int e, int w):startVertex(s),endVertex(e),weightEdge(w),nextEdge(NULL){}
};
//节点
struct Vertex
{
	int neighborNum;//邻居节点个数
	Edge * headEdge;
	Vertex():neighborNum(0),headEdge(NULL){}
};
//图
class Graph
{
public:
	int vertexNum;//总节点个数
	Vertex * V;//节点
	Graph():vertexNum(0),V(NULL){}
	Graph(int n)
	{
		if (n <= 0)
		{
			vertexNum = 0;
			V = NULL;
		}
		else
		{
			vertexNum = n;
			V = new Vertex[vertexNum];
		}
	}
	Graph(const Graph &g)//复制构造函数
	{
		vertexNum = g.vertexNum;
		if (vertexNum > 0)
		{
			V = new Vertex[vertexNum];
			for (int i = 0; i < vertexNum; i++)
			{
				V[i].neighborNum = g.V[i].neighborNum;
				Edge *pEg = g.V[i].headEdge;
				if (pEg != NULL)  
				{  
					V[i].headEdge = new Edge(pEg->startVertex,pEg->endVertex,pEg->weightEdge);  
					pEg = pEg->nextEdge;  
				}  
				Edge *pE = V[i].headEdge;  
				while (pEg != NULL)  
				{  
					pE->nextEdge = new Edge(pEg->startVertex,pEg->endVertex,pEg->weightEdge);  
					pE = pE->nextEdge;  
					pEg = pEg->nextEdge;  
				}
			}
		}
		else
			V = NULL;
	}
	~Graph()
	{
		if (V != NULL)
		{
			for (int i = 0; i < vertexNum; i++)
			{
				Edge * pE = V[i].headEdge;
				V[i].headEdge = NULL;
				while (pE != NULL)
				{
					Edge * pDel = pE;
					pE = pE->nextEdge;
					delete pDel;
				}
			}
			delete [] V;
			V = NULL;
		}
	}

	void addSingleEdge(int s, int e, int w);
	void showGraph();
};

图类源文件

//graph.cpp
#include <iostream>
#include "graph.h"

//添加单向边
void Graph::addSingleEdge(int s, int e, int w)
{
	if (s == e || s >= vertexNum || e >= vertexNum || s < 0 || e < 0)
		return;//舍弃边界外的边,舍弃自环的边

	Edge * pE = new Edge(s,e,w);

	if (V[s].headEdge == NULL || V[s].headEdge->endVertex >= e)//从小到大排序
	{
		pE->nextEdge = V[s].headEdge;
		V[s].headEdge = pE;
	} 
	else
	{
		Edge * pH = V[s].headEdge;
		while (pH->nextEdge != NULL && pH->nextEdge->endVertex < e)//从小到大排序
			pH = pH->nextEdge;
		pE->nextEdge = pH->nextEdge;
		pH->nextEdge = pE;
	}

	V[s].neighborNum++;
	return;
}
//显示图
void Graph::showGraph()
{
	using namespace std;
	if (vertexNum > 0)
	{
		for (int i = 0; i < vertexNum; i++)
		{
			cout << i << " :";
			if (V[i].headEdge != NULL)
			{
				Edge * pH = V[i].headEdge;
				while (pH != NULL)
				{
					cout << "   -> " << pH->endVertex << " (" << pH->weightEdge << ")";
					pH = pH->nextEdge;
				}
			}
			cout << endl;
		}
	}
	else
		cout << "This Graph is empty!" << endl;
	return;
}

调用主函数

//2_4_Graph
//无向图
#include <iostream>
#include "graph.h"


int main()
{
	using namespace std;
	int nums,rows,s,e,w;

	cin >> nums >> rows;//输入图的节点总数,输入边的总行数
	Graph G0(nums);//构造一个 nums 个节点的图

	for (int i = 0; i < rows; i++)
	{
		cin >> s >> e >> w;//输入边的 始节点,终节点,边权重
		G0.addSingleEdge(s,e,w);//无向图,添加单向边
		G0.addSingleEdge(e,s,w);//无向图,添加单向边
	}
	Graph G1 = G0;//调用复制构造函数
	G1.showGraph();//显示图

	return 0;
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值