无向图图的两种数据储存结构的C++代码的实现

 无向图图的两种数据储存结构的C++代码的实现(都是无向图的实现)

 一、邻接矩阵

#include
using namespace std;
template 
class dd
{
private:
	T data;            //顶点数据域
public:
	dd(){}
	dd(T data1) :data(data1)/*, firstedge(a)*/
	{}
	void set(T data)              //设置结点的相关属性
	{
		this->data=data;
	}
};
template 
class ljjz
{
private:              
	dd  *point;        //顶点数组       //连接矩阵
	T **arcb;
public:
	ljjz() {};             //默认构造一个空的图
	ljjz(dd n[],T *m[]):point(n),arcb(m)
	{
	}
	void serchdu()
	{
		int k;
		cout << "请输入你要查找的点的下标" << endl;
		cin >> k;
		int g = arcb[k][0] + arcb[k][1] + arcb[k][2] + arcb[k][3];
		cout << "该点的度为" << g << endl;
	}
	void srechljd()
	{
		int k;
		cout << "请输入你要查找的点的下标" << endl;
		cin >> k;
		cout << "v1的邻接点有:";
		for (int i=0; i < 4; i++)
		{
			if (arcb[k][i] == 1)
				cout << "v"< k[4]{ dd(1),dd(2),dd(3),dd(4) };                //对象数组的初始化方式
	int **l = new int*[4];                      //邻接数组
	for (int i = 0; i < 4;i++)            //动态二维数组的创建方法
	{
		l[i] = new int[4];
	}
	for (int i = 0; i < 4; i++)

		for (int j = 0; j < 4; j++)

		{

			cout << "a[" << i << "]" << "[" << j << "]" << "=";
            cin >> l[i][j];

		}
	ljjz map(k, l);
	map.serchdu();
	map.srechljd();
	for (int i = 0; i < 4; i++)            //动态二维数组的创建方法
	{
		delete l[i];
	}
	delete *l;
}

二、邻接表

//图的数据结构
#include
using namespace std;
class edge;                            //类的预定义
template 
class dd
{
private:
	T data;            //顶点数据域;ll+-/1q*
	edge* firstedge;  //指向一t条边集数组的对象
public:
	dd()/*,firstedge(nullptr)*/{}
	dd(T data1 ,edge* firstedge1=nullptr/*这里最好要给赋一个空指针*/) :data(data1),firstedge(firstedge1)
	{}
	void set(T data)              //设置结点的相关属性
	{
		this->data=data;
	}
	edge* getadress()const
	{
		return this->firstedge;
	}
	void setedgr(edge* firstedge1)
	{
		this->firstedge = firstedge1;
	}
};
class edge                    //边集数组
{
private:
	int adjvex;
	edge *next;
public:
	edge() :adjvex(0), next(nullptr) {}    //默认构造函数
	edge(int a, edge* b = nullptr) :adjvex(a), next(b)
	{}
	void setdate(int z)
	{
		this->adjvex=z;
	}
	void setpoint(edge* b)
	{
		this->next = b;
	}
	edge* nextdress()const
	{
		return this->next;
	}
	int getvalue()const
	{
		return this->adjvex;
	}
};
template 
class ljjz
{
private:              
	dd* point;        //顶点数组用一个一维数组表示
public:
	ljjz():point(nullptr){};             //默认构造一个空的图
	ljjz(dd* a) :point(a)
	{

	}
	void create_array()
	{
		cout << "Please  input  the number  of the edge you  need to create" << endl;
		int a;
		cin >> a;        
		edge* ak;//录入用户需要的边数
		for (int j=0; j < a; j++)
		{                                      
			cout << "Please input the edge points" << endl;           //这里创建链表的方式是头插法
			int  m, z;
			cout << "起点:";
			cin >> m;
			cout << "终点:";
			cin >> z;
			ak = new edge;
			ak->setdate(z);
			ak->setpoint(point[m].getadress());
			point[m].setedgr(ak);
			ak = new edge;
			ak->setdate(m);
			ak->setpoint(point[z].getadress());
			point[z].setedgr(ak);
		}
	}
	void serchdu()                               //计算点的度
	{
		cout << "请输入你要计算度的下标:";
		int a,b=0;
		cin >> a;
		edge* k;
		k = point[a].getadress();
		while (k!=nullptr)                  //遍历链表操作
		{
			k = k->nextdress();
			b++;
		}
		cout << "顶点" << a << "的度为:" << b << endl;
	}
	void srechljd()                                 //搜索顶点的邻接点
	{
		cout << "请输入你要查找的点的下标:";
		int a;
		cin >> a;
		edge* k;
		k = point[a].getadress();
		cout << "顶点" << a << "的邻接点有:";
		while (k != nullptr)                  //遍历链表操作
		{
			cout <<"v"<< k->getvalue()<<"  ";
			k = k->nextdress();
		}
		endl(cout);
	}
	~ljjz()
	{
		for (int a = 0; point[a].getadress() == nullptr; a++)
		{
			edge* k = point[a].getadress();
			edge* z = point[a].getadress();
		    while(k->nextdress()!= nullptr)                  //遍历链表操作
			{
				delete z;
				k->setpoint(k->nextdress()->nextdress());
				z = k;
				a++;
			}
		 }
	}
};
int main()
{

	dd k[4]={ dd(1),dd(2),dd(3),dd(4) };                //对象数组的初始化方式
	ljjz map(k);
	map.create_array();
	map.serchdu();
	map.srechljd();
	return 0;
}

 

你可能感兴趣的:(数据结构)